2017-12-04 7 views
2

私は独占的なゲームを自分で構築しています。これまでは、ゲームボードといくつかのボタンを含むインターフェースを設定しました。私は彼らがサイコロを振ると、スペース上に2人のプレイヤーのポジションを描きたい。私はスタートエリアにプレーヤーのポジションを描いています。私は自分の位置(黒丸で表される)を新しい位置に移動したい。しかし、私はそれをするために熱い知らない。特に、PaintEventHandlerに、新しいPaintイベントが発生する必要があることを知らせる方法。以下は私のコードです。新しい位置に円を描く

namespace Monopoly 
{ 
    public partial class GameInterface : Form 
    { 
     /* if the remider of whichTurn is 0, palyer1's turn to play game 
     * otherwise, palyer2's turn to play game 
     */ 
     private int whichTurn = 0; 
     private int result; //record the result of the dice rolled by players 
     private int state = 0; //0 for begin, 1 for playing 
     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 the begainning 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"); 
      } 

     } 
    } 
} 

答えて

1

"player1"と "player2"のように、 "recPlayer1"と "recPlayer2"をクラスレベルに移動します。今度は矩形の位置を変更してからpanelBoard.Invalidate();を呼び出してパネルを再描画し、円を新しい位置に移動させます。

+0

これは完全に機能します。どうもありがとうございます!もう1つの質問は、panelBoard.Invalidate()が再描画イベントを送信した後、PaintEventHandlerがpanelBoard_Paint()を呼び出したかどうかです。 –

+0

これは正しいです、Invalidate()は、コントロールにPaint()イベントが生成されるようにコントロールを再描画する必要があることをシステムに通知します。 –