C.Mi
C.Mi

Reputation: 37

Draw a circle at a new position

I am building a Monopoly game myself. So far I set up a interface containing the game board and several button. I want to draw two player's positions on a space once they roll their dice. I have drawn players' positions at the start area. I want to move their position(represented as filled circle) to a new position. But I don't know hot to do it. Specifically, how to let PaintEventHandler know that a new Paint event needs to be happened. Below is my code.

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");
            }

        }
    }
}

Upvotes: 2

Views: 189

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39132

Move "recPlayer1" and "recPlayer2" out to Class level, just like you've done with "Player1" and "Player2". Now simply change the location of the rectangle(s) and then call panelBoard.Invalidate(); to make the panel repaint and the circles move to their new position.

Upvotes: 1

Related Questions