C#中的吃豆人(地图和碰撞检测) [英] Pacman in C# (map & collision detection)

查看:114
本文介绍了C#中的吃豆人(地图和碰撞检测)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用C#编写Pacman进行编程.

对于地图(经典设计),我使用了图片框,但其中有50个,因此,Collison检测非常麻烦.

为什么制作地图并简化大肠菌病检测更容易?

Trying to code Pacman in C# for programming class.

For the map(classic design) I used picture boxes but there''s like 50 of them, so collison detection is a pain.

Is there an easier why to make the map and simplify the colision detection?

{
    public Form1()
    {
        InitializeComponent();
    }

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

    private void Up_Tick(object sender, EventArgs e)
    {
        pacman.Top += -1;
        pacman.Image = Image.FromFile(@"y:\\Pacman Up.png");
        if (pacman.Top <= pictureBox1.Bottom+1 && pacman.Right >=       pictureBox1.Left && pacman.Left<= pictureBox1.Right||
            pacman.Top <= pictureBox2.Bottom+1 && pacman.Right >= pictureBox2.Left && pacman.Left<= pictureBox2.Right)
        {
            Up.Enabled = false;
        }
    }
    private void Down_Tick(object sender, EventArgs e)
    {
        pacman.Top += 1;
        pacman.Image = Image.FromFile(@"y:\\Pacman Down.png");
    }
    private void Left_Tick(object sender, EventArgs e)
    {
        pacman.Left += -1;
        pacman.Image = Image.FromFile(@"y:\\Pacman Left.png");
        if (pacman.Left <= 13) { pacman.Location = new Point(470, 242); }
        if (pacman.Left <=pictureBox1.Right && pacman.Right>=pictureBox1.Left && pacman.Top <= pictureBox1.Bottom && pacman.Bottom >= pictureBox1.Top ||
            pacman.Left <=pictureBox2.Right && pacman.Right>=pictureBox2.Left && pacman.Top <= pictureBox2.Bottom && pacman.Bottom >= pictureBox2.Top)
            { Left.Enabled = false; }
    }
    private void Right_Tick(object sender, EventArgs e)
    {
        pacman.Left += 1;
        pacman.Image = Image.FromFile(@"y:\\Pacman Right.png");
        if (pacman.Right >= 470) { pacman.Location = new Point(12, 242); }
    }



我剪出了很多代码,但这只是其余30个框的冲突检测的其余部分.



I cut out a ton of code, but it was just the rest of the collision detection for the 30 other boxes.

推荐答案

完全不要使用图片框.处理您的窗体或面板的Paint事件,并在适当的位图或Blob上进行绘制.图片框似乎使它更容易使用,但是要使用很多控件,它会使事情变慢.

如果您自己绘制,则碰撞检测将变得更加容易:pac人位于x,y的正方形中,其中x和y可以很容易地从当前位置进行计算,只需除以像元的宽度/高度即可:每个鬼影都相同.由于整个单元格都是可输入的或不可输入的,因此您只需要计算一下吃了pac男人的食物就可以.
Don''t use picture boxes at all. Handle the Paint event for your form or panel, and paint on the appropriate bitmaps or blobs. Picture boxes may seem to make it easier, but that''s a lot of controls to play with - and it will slow things down.

If you paint it yourself, collision detection becomes easier: The pac man is in square x, y where x and y are easy to calculate from the current location with just a division by the cell width/height: same for each ghost. Since the whole of a cell is either enterable or not enterable, you only need to work out if the pac man is being eaten.


不要使用图片框!

创建图块类并存储图块类的集合(网格).每个人都知道如何绘制给定的图形对象(您将从面板的Paint事件中传递该对象-该面板将用于绘制所有图形)

然后,应通过循环映射图块(最好仅检查玩家附近的图块)和其他游戏对象(如鬼影,道具,点,奖励物品等)来进行碰撞检测.

不要像您一样在Ticks上执行碰撞逻辑.您应该旨在使游戏循环类似于以下内容

读取输入(按下按键)
流程逻辑(检测收集,对象移动等)
更新动画(如果有)
渲染图形(在面板上绘制所有对象)
播放声音(如果有)
同步帧速率(如果需要,可以延迟循环)
重复

如果必须执行winforms应用程序,则可以使用计时器来模拟游戏循环

因此,您将拥有一个tile类,并且可以使用2D数组将它们存储在网格中

Don''t use picture boxes!

Create a tile class and store a collection (Grid) of tile classes. Each one knows how to draw itself given a graphics object (which you will pass from the Paint event of your panel - the panel will be used to draw all graphics)

Collision detection should then be done by looping the map tiles (preferable only checking tiles near to the player) and other game objects (such as the ghosts, power-ups, dots, bonus items etc.)

Don''t perform collision logic on Ticks like you have. You should aim to have a game loop similar to something like follows

Read Input (Get key presses)
Process Logic (detect collection, object movement etc.)
Update Animation (if any)
Render Graphics (Draw all objects on panel)
Play Sounds (if any)
Sync Frame Rate (delay loop if needed)
Repeat

If you have to do winforms application then you can use a timer to simulate the game loop

So you would have a tile class and you can you a 2D array to store them in a grid

TileClass[,] tiles = new TileClass[15,15];



然后,您可以将pac man的位置转换为平铺,并检查周围是否有碰撞

因此,假设您的图块尺寸为25px x 25px,您可以执行以下操作



Then you can translate you pac man position to a tile and check the surrounding ones for collision

So say your tiles are 25px x 25px you can do something like

int x = (int)(pacMan.Position.X / 25);//Pacman position should be relative to grid point 0,0
int y = (int)(pacMan.Position.Y / 25);

//Then check collition with surrounding tiles
//tiles[x-1, y-1];
//tiles[x-1, y];
//tiles[x-1, y+1];
//tiles[x, y-1];
//tiles[x, y+1];
//tiles[x+1, y-1];
//tiles[x+1, y];
//tiles[x+1, y+1];



...无论如何,类似的东西.我没有时间在那一刻检查逻辑中的任何缺陷
//tiles [x,y];



...anyway, something like that. I don''t have the time at the minute to check for any flaws in that logic
//tiles[x, y];


这篇关于C#中的吃豆人(地图和碰撞检测)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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