Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to get pacman to move in grid, only can move through ones, stops when he hits a 0(the wall). Have four separate timers for directions that activate on keypress. Have only started with him moving right at the moment, for some reason he moves 4 spaces(int move = 4), then stops and will no longer move right.

C#
public partial class Form1 : Form
    {
        int xg = 1, yg = 1;
        int move = 4;
        // 28 x 31 grid which functions as the map
        int[,] map = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                            {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
                            {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
                            {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
                            {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
                            {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
                            {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
                            {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
                            {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
                            {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
                            {1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1},
                            {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
                            {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
                            {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
                            {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
                            {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
                            {0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0},
                            {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
                            {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
                            {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
                            {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
                            {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
                            {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
                            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
        public Form1()
        {
            InitializeComponent();
        }

        private void Up_Tick(object sender, EventArgs e)
        { pacman.Top -= move;
            pacman.Image = Image.FromFile(@"y:\\Pacman Up.png"); }

        private void Down_Tick(object sender, EventArgs e)
        {  pacman.Top += move;
            pacman.Image = Image.FromFile(@"y:\\Pacman Down.png"); }

        private void Left_Tick(object sender, EventArgs e)
        { pacman.Left -= move;
            pacman.Image = Image.FromFile(@"y:\\Pacman Left.png");  }

        private void Right_Tick(object sender, EventArgs e)
        {
            if (map[xg ,yg] == 1) { pacman.Left += move; xg += 1; }
            else { pacman.Top -= move; }
            pacman.Image = Image.FromFile(@"y:\\Pacman Right.png");}

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {   string a;
            a = e.KeyData.ToString();
            if (e.KeyData.ToString() == "Escape") Application.Exit();

            if (e.KeyData.ToString() == "Up") { Up.Enabled = true;      Right.Enabled = false; Down.Enabled = false; Left.Enabled = false; }

            if (e.KeyData.ToString() == "Down") { Down.Enabled = true; Up.Enabled = false; Right.Enabled = false; Left.Enabled = false; }

            if (e.KeyData.ToString() == "Left") { Left.Enabled = true; Right.Enabled = false; Up.Enabled = false; Down.Enabled = false; }

            if (e.KeyData.ToString() == "Right") { Right.Enabled = true; Up.Enabled = false; Left.Enabled = false; Down.Enabled = false; }
        }
Posted

1 solution

First this is you do not use PictureBox to do your drawing for you. Scrap it and start handling the Paint event of a control, like a Panel, to hold your game image. You'll have far better control over the rendering than if you relied on PictureBox's to do it for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900