在数组中移动pacman(picturebox),Visual C# [英] Moving pacman(picturebox) in an array, Visual C#

查看:92
本文介绍了在数组中移动pacman(picturebox),Visual C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让pacman在网格中移动,只能通过一个,在他到达0(墙)时停止.有四个单独的计时器,用于在按键时激活的方向.现在只开始于他向右移动,由于某种原因,他向后移动了4个空格(int move = 4),然后停止了,将不再向右移动.

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.

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

推荐答案

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.
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.


这篇关于在数组中移动pacman(picturebox),Visual C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆