首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查pacman运动

检查pacman运动
EN

Stack Overflow用户
提问于 2015-05-01 21:26:12
回答 1查看 485关注 0票数 0

我在做吃豆人的游戏,但我不知道该怎么做:当吃豆人撞到食物时,食物就会消失。我的代码不起作用。下面是初始化食品世界的功能:

代码语言:javascript
复制
 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.foodImage;
        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

        foodWorld = new bool[WORLD_WIDTH][];
        for (int i = 0; i < WORLD_WIDTH;i++ )
        {
            foodWorld[i] = new bool[WORLD_HEIGHT];
        }

        for (int i = 0; i < WORLD_WIDTH; i+=2) {
            for (int j = 0; j < WORLD_HEIGHT; j++) {

                foodWorld[i][j] = true;

            }
        }

        // овде кодот за иницијализација и стартување на тајмерот
        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();
        }

        for (int i = 0; i < WORLD_WIDTH; i++) {

            for (int j = 0; j < WORLD_HEIGHT; j++) {

                if (pacman.x == i && pacman.y == j && foodWorld[i][j])
                {

                    foodWorld[i][j] = false;
                    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
                    pacman.open = true;
                }
                else {

                    pacman.open = false;
                }
            }
        }

        pacman.Move(WORLD_WIDTH, WORLD_HEIGHT);
        Invalidate();
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        // не заборавајте да го додадете настанот на самата форма
        // вашиот код овде
        if (e.KeyCode == Keys.Up) {

            pacman.ChangeDirection("UP");
        }
        if (e.KeyCode == Keys.Down)
        {

            pacman.ChangeDirection("DOWN");
        }
        if (e.KeyCode == Keys.Left)
        {

            pacman.ChangeDirection("LEFT");
        }
        if (e.KeyCode == Keys.Right)
        {

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

    }

}
   }

我应该改变什么,使被帕克曼触摸的画面消失?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-01 21:32:48

当帕克曼吃了食物,你就有了这个:

代码语言:javascript
复制
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

此语法是对画图事件的订阅。你应该这样做一次而且只做一次。相反,请使用:

代码语言:javascript
复制
this.Invalidate();

调用无效告诉操作系统,“嘿,我需要重新绘制!”然后,操作系统将生成事件,该事件最终触发画图事件并执行您的画图代码。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29995378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档