我正在開發一個在線跳棋程式,我成功地在每個方塊上繪制了棋盤和跳棋。現在唯一的問題是對點擊事件進行編程,我有代碼可以回應對單個面板的點擊,將背景顏色更改為藍色,將棋盤格顏色更改為白色,當用戶單擊同一塊面板時,顏色是反轉回原來的樣子。現在我的問題是實作這種效果:如果用戶單擊 panelA 然后應用顏色沖突,當單擊不同的 panelB 時,應用于 panelA 的著色應該像雙擊效果一樣恢復到原始狀態。
我嘗試的解決方案
我初始化了一個List<Panel> clicked_objects物件并用它來存盤Panel被單擊的元素,我希望串列物件的計數隨著每次連續單擊而增加,但是當我執行 a 時MessageBox.Show(clicked_objects.Count.ToString()),它總是顯示 1。我需要這樣做,以便立即一秒鐘clickedPanel被添加到這個串列中,我可以檢查面板的計數和顏色,并對面板進行index 1(new Panel)脫色index 0(previously clicked Panel)。我怎樣才能做到這一點?單擊一個面板會洗掉先前單擊的元素上的著色(如果有)?
代碼
public class Checkerpiece
{
//declare a panel array for storing clicked Panels
List<Panel> clicked_squares=new List<Panel>();
//The object above can only hold two panel elements
//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;
//define some foreground color that will be used to access the color of the checker
Color foreground;
//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;
//register an onclick listener for our checkerpiece object
this.target_square.Click = Target_square_Click;
// this.target_square.MouseClick = Target_square_MouseClick;
}
private void Target_square_Click(object sender, EventArgs e)
{
clicked_squares.Add(target_square);
//this keeps showing 1 no mater what panel I click
MessageBox.Show(clicked_squares.Count.ToString());
}
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);
}
//implement a getter for the color used to used to draw the checker
public Color getCheckerColor()
{
return this.color;
}
}
棋盤類
public partial class Form1 : Form
{
private Panel[,] _chessBoardPanels;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
const int tileSize = 100;
const int gridSize = 8;
var clr1 = Color.DarkGray;
var clr2 = Color.White;
// initialize the "chess board"
_chessBoardPanels = new Panel[gridSize, gridSize];
int count = 0;
// 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)
};
//register an event listener for each square
// 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;
//draw only the red checkers on top of the board
// color the backgrounds
if (n % 2 ==0)
newPanel.BackColor = m % 2 != 0 ? clr1 : clr2;
//new Checkerpiece(newPanel, Color.Red).draw();
else
newPanel.BackColor = m % 2 != 0 ? clr2 : clr1;
//draw a new checker piece
if (newPanel.BackColor==Color.White &&m<=3)
new Checkerpiece(newPanel, Color.Red).draw();
;
if (newPanel.BackColor == Color.White && m > 4)
new Checkerpiece(newPanel, Color.Black).draw();
}
}
}
}
輸出
當我點擊一件作品時,每種顏色都會保留在之前點擊過的物品上,這不應該是這種情況

目標
如何存盤單擊面板的狀態并在單擊新面板時恢復?
uj5u.com熱心網友回復:
您應該從 Board 類管理棋子,可以通過參考檢查棋子串列,當您通過 clicked 事件擁有物件時,無需在集合中“查找”匹配的棋子。使用 Board (Form) 類來管理當前選定的部分,并在設定這些屬性時處理選擇/取消選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357986.html
