简体   繁体   English

吃豆子游戏-如何修复

[英]Pacman game - how to fix

I am creating really simple pacman game as a homework. 我正在创建非常简单的吃豆人游戏作为作业。 I am working in Visual Studio, in c#. 我在Visual Studio中的C#中工作。 The problem is, when I click run, only the winform shows with nothing on it. 问题是,当我单击运行时,仅显示winform,而没有任何显示。 Can someone tell me what have I done wrong? 有人可以告诉我我做错了什么吗?

namespace pacman
{
    public partial class Form1 : Form
    {
        Timer timer;
        Pacman pacman;
        static readonly int TIMER_INTERVAL = 250;
        static readonly int WORLD_WIDTH = 15;
        static readonly int WORLD_HEIGHT = 10;
        Image foodImage;
        bool[][] foodWorld;

        public Form1()
        {
            InitializeComponent();

            foodImage = Properties.Resources.orange_detoure;
            DoubleBuffered = true;
            newGame();
        }

        public void newGame()
        {
            pacman = new Pacman();
            this.Width = Pacman.radius * 2 * (WORLD_WIDTH + 1);
            this.Height = Pacman.radius * 2 * (WORLD_HEIGHT + 1);

            foodWorld = new bool[WORLD_WIDTH][];

            timer = new Timer();
            timer.Interval = TIMER_INTERVAL;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Interval = TIMER_INTERVAL - 1;
            if (TIMER_INTERVAL == 0)
            {
                timer.Stop();
            }
            pacman.Move(WORLD_WIDTH, WORLD_HEIGHT);
            Invalidate();
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keys.Up != 0) 
            {
                pacman.ChangeDirection("UP");
            }
            if (Keys.Down != 0)
            {
                pacman.ChangeDirection("DOWN");
            }
            if (Keys.Left != 0)
            {
                pacman.ChangeDirection("LEFT");
            }
            if (Keys.Right != 0)
            {
                pacman.ChangeDirection("RIGHT");
            }
            Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            for (int i = 0; i < foodWorld.Length; i++)
            {
                for (int j = 0; j < foodWorld[i].Length; j++)
                {
                    if (foodWorld[i][j])
                    {
                        g.DrawImageUnscaled(foodImage, j * Pacman.radius * 2 + (Pacman.radius * 2 - foodImage.Height) / 2, i * Pacman.radius * 2 + (Pacman.radius * 2 - foodImage.Width) / 2);
                    }
                }
            }
            pacman.Draw(g);
        }
    }
}

And here is the class Pacman: 这是Pacman类:

namespace pacman
{
    public class Pacman
    {
        public enum dir {UP, DOWN, RIGHT, LEFT}; 

        public float x { get; set; }
        public float y { get; set; }
        public string direction { get; set; }
        static public int radius = 20;
        public double speed { get; set; }
        public bool open { get; set; }
        public Brush brush = new SolidBrush(Color.Yellow);

        public Pacman() {
            this.x = 7;
            this.y = 5;
            this.speed = 20;
            this.direction = Convert.ToString(dir.RIGHT);
            this.open = false;
        }

        public void ChangeDirection(string direct)
        {
            // vasiot kod ovde
            for (int i = 0; i < 3; i++) { 

                if(direct == Convert.ToString(dir.RIGHT))
                {
                    direction = "RIGHT";
                }
                if (direct == Convert.ToString(dir.LEFT))
                {
                    direction = "LEFT";
                }
                if (direct == Convert.ToString(dir.UP))
                {
                    direction = "UP";
                }
                if (direct == Convert.ToString(dir.DOWN))
                {
                    direction = "DOWN";
                }
            }
        }

        public void Move(float width, float height)
        {
            if (direction == Convert.ToString(dir.RIGHT))
            {
                x = x + 1;
                if (x > width) {
                    x = 1;
                }
            }
            else if (direction == Convert.ToString(dir.LEFT))
            {
                x = x - 1;
                if (x < 0) {
                    x = 14;
                }
            }
            else if (direction == Convert.ToString(dir.UP))
            {
                y = y + 1;
                if (y > height) {
                    y = 1;
                }
            }
            else if (direction == Convert.ToString(dir.DOWN))
            {
                y = y - 1;
                if (y < 0) {
                    y = 14;
                }
            }
        }

        public void Draw(Graphics g)
        {
            if (!open) {
                g.FillEllipse(brush, 7, 5, 15, 15);
            }
        }   
    }
}

It should look like this http://prntscr.com/70e0jt . 它应该看起来像这样http://prntscr.com/70e0jt I will be grateful if someone can tell me what should I fix so it finally works.. 如果有人可以告诉我应该如何解决,以便最终解决,我将不胜感激。

Add this to your constructor: 将此添加到您的构造函数:

this.Paint += Form1_Paint;

What is happening is that you made a Paint handler, but never assigned the paint event to handle it (that I can see, its possible that the event was assigned in the other partial part of the class). 发生的事情是您创建了一个Paint处理程序,但从未分配paint事件来处理它(我可以看到,该事件可能是在类的其他部分中分配的)。

The other problem is that you did not fully define the "food world" variable, you initialized the first rank, but not the second. 另一个问题是您没有完全定义“ food world”变量,而是初始化了第一个等级,但没有初始化第二个等级。 You need to fully initialize this and set the booleans inside of it. 您需要完全初始化它并在其中设置布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM