【发布时间】:2021-12-24 13:47:14
【问题描述】:
我的代码循环遍历面板的多维数组以在其上绘制棋盘格。我在循环内实现控制结构时遇到问题,以确保仅在前三行上绘制红色棋盘格,然后跳过一行并在其余三行上绘制黑色棋盘格。代码应保持在具有白色背景的面板上绘制检查器。我已经有一个循环,可以确保每个具有白色背景的面板上都有一个棋盘格,我的问题是控制绘图以模拟现实生活中的棋盘格......
代码
/*This code block contains the class for
the Checkerpiece object drawn on the panel
*/
public class Checkerpiece
{
//colors of the rounded pieces
Color color;
//specify where the checker is drawn
Panel target_square;
//specify the center of the circle
float center_x;
float center_y;
//specify the radius of the checker piece
float radii;
//fill the details inside the constructor
public Checkerpiece(Panel mypanel,Color color)
{
this.color = color;
this.target_square = mypanel;
this.center_x = mypanel.Width / 2;
this.center_y = mypanel.Height / 2;
this.radii = mypanel.Width / 2;
}
//this method draws the checkerpiece on the target panel
public void draw()
{
this.target_square.Paint += Target_square_Paint;
}
//this event will redraw the circles as needed
private void Target_square_Paint(object sender, PaintEventArgs e)
{
SolidBrush mybrush = new SolidBrush(color);
fillCircle(e.Graphics, mybrush, this.center_x, this.center_y,(float) radius);
}
//ellipse(checker) filler method
public static void fillCircle(Graphics g, Brush b, float centerX, float centerY, float radius)
{
g.FillEllipse(b, centerX - radius, centerY - radius,
radius + radius, radius + radius);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Checkers
{
public partial class Form1 : Form
{
private Panel[,] _chessBoardPanels;
public Form1()
{
InitializeComponent();
}
}
/*Form1 class contains the loops that is responsible for drawing checkers on the board*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Checkers
{
public partial class Form1 : Form
{
//declare the panel array
private Panel[,] _chessBoardPanels;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//controls the size of a single square on the checkerboard
const int tileSize = 100;
//controls the number of squares on either side of the board
const int gridSize = 8;
//this is fr applying different colors to the board so the pattern is realized
var clr1 = Color.DarkGray;
var clr2 = Color.White;
// initialize the "chess board"
_chessBoardPanels = new Panel[gridSize, gridSize];
// double for loop to handle all rows and columns
for (var n = 0; n < gridSize; n++)
{
for (var m = 0; m < gridSize; m++)
{
// create new Panel control which will be one
// chess board tile
var newPanel = new Panel
{
Size = new Size(tileSize, tileSize),
Location = new Point(tileSize * n, tileSize * m)
};
// add to Form's Controls so that they show up
Controls.Add(newPanel);
// add to our 2d array of panels for future use
_chessBoardPanels[n, m] = newPanel;
// color the backgrounds
if (n % 2 == 0)
newPanel.BackColor = m % 2 != 0 ? clr1 : clr2;
else
newPanel.BackColor = m % 2 != 0 ? clr2 : clr1;
//draw a new checker piece if this is true
if (newPanel.BackColor == Color.White)
new CheckerPiece(newPanel,Color.Red).draw();
else
;
/*I need to add a loop to make sure the red checkers are drawn on the first three rows of each panel that has a white background*/
}
}
}
}
}
【问题讨论】:
-
改用 PictureBox 数组?
-
好的,这是一个好主意,但首先建立在基本的棋盘启动器逻辑上,我会尝试一下,以便能够将艺术应用于不同的方块。编程是最先进的实践。
-
是的,如果有人发明了一个面板,只需设置一个属性就知道如何在自己身上绘制图片,我肯定会使用最先进的技术..
-
是的,这是个大人物,它归结为用户与计算机,我需要对 Min-Max 算法进行编程,以告诉计算机要采取什么行动才能击败用户。大声笑,电脑真是个了不起的人。我将面板位置来移动碎片
-
PictureBoxes 有一个 Location 属性..