我正在建立一个大富翁游戏。到目前为止,我设置了一个包含游戏板和几个按钮的界面。我想在他们掷骰子后在一个空间上画两个玩家的位置。我在起跑区吸引了球员的位置。我想将他们的位置(表示为实心圆圈)移动到新位置。但我不知道这么做。具体来说,如何让PaintEventHandler知道需要发生新的Paint事件。以下是我的代码。
namespace Monopoly
{
public partial class GameInterface : Form
{
// If the remainder of whichTurn is 0, player1's turn to play game
// Otherwise, player2's turn to play game
private int whichTurn = 0;
// Record the result of the dice rolled by players
private int result;
// 0 for beginning, 1 for playing
private int state = 0;
Game game = new Game();
Player player1 = new Player("Player1");
Player player2 = new Player("Player2");
public GameInterface()
{
InitializeComponent();
}
private void panelBoard_Paint(object sender, PaintEventArgs e)
{
if(state == 0)
{
DrawPlayerAtStart(e);
} else if(state == 1)
{
DrawMovement(e);
}
}
// Draw two circles at the beginning area
private void DrawPlayerAtStart(PaintEventArgs e)
{
// Draw first player's position
SolidBrush firca_dis = new SolidBrush(Color.FromArgb(192, 0, 192));
Rectangle recPlayer1 = new Rectangle(600, 500, 20, 20); //Size and location of the Circle
e.Graphics.FillEllipse(firca_dis, recPlayer1); //Draw a Circle and fill it
e.Graphics.DrawEllipse(new Pen(firca_dis), recPlayer1);
// Draw second player's position
SolidBrush secca_dis = new SolidBrush(Color.FromArgb(17, 0,255));
Rectangle recPlayer2 = new Rectangle(600, 540, 20, 20); //Size and location of the Circle
e.Graphics.FillEllipse(secca_dis, recPlayer2); //Draw a Circle and fill it
e.Graphics.DrawEllipse(new Pen(secca_dis), recPlayer2);
}
// Draw two circles after player rolled the dice
private void DrawMovement(PaintEventArgs e)
{
SolidBrush firca_dis = new SolidBrush(Color.FromArgb(192, 0, 192));
Rectangle recPlayer1 = new Rectangle(547, 520, 20, 20); //Size and location of the Circle
e.Graphics.FillEllipse(firca_dis, recPlayer1); //Draw a Circle and fill it
e.Graphics.DrawEllipse(new Pen(firca_dis), recPlayer1);
// Draw second player's position
SolidBrush secca_dis = new SolidBrush(Color.FromArgb(17, 0, 255));
Rectangle recPlayer2 = new Rectangle(547, 550, 20, 20); //Size and location of the Circle
e.Graphics.FillEllipse(secca_dis, recPlayer2); //Draw a Circle and fill it
e.Graphics.DrawEllipse(new Pen(secca_dis), recPlayer2);
}
// Let player roll dice roll dice
private void rollDiceButton_Click(object sender, EventArgs e)
{
state = 1;
if(whichTurn % 2 == 0)
{
result = player1.GetRollDiceResult();
textBox1.AppendText("Player 1 rolled "+result + "\n");
} else if(whichTurn % 2 == 1)
{
result = player2.GetRollDiceResult();
textBox1.AppendText("Player 2 rolled " + result + "\n");
}
}
}
}
答案 0 :(得分:1)
移动" recPlayer1"和" recPlayer2"升级到班级,就像你已经完成了" Player1"和" Player2"。现在只需更改矩形的位置,然后调用panelBoard.Invalidate();
使面板重新绘制,圆圈移动到新位置。