我创造了一个修改版的吃豆人游戏,如何让它呼出火焰?

5

我创建了一个修改过的Pacman,但是我想在Pacman的嘴里加入一个火球。我的代码如下:

namespace TestingPacman
{
    class Firebolt
    {
        Bitmap firebolt0 = null;
        Bitmap firebolt1 = null;    

        public Point fireboltposition;
        int fireboltwidth = 0;
        int fireboltheight = 0;
        public Firebolt(int x, int y)
        {
            fireboltposition.X = x;
            fireboltposition.Y = y;    
            if (firebolt0 == null)
                firebolt0 = new Bitmap("firebolt0.gif");    
            if (firebolt1 == null)
                firebolt1 = new Bitmap("firebolt1.gif");    
            int fireboltwidth = firebolt0.Width;
            int fireboltheight = firebolt0.Height;
        }

        public Rectangle GetFrame()
        {
            Rectangle Labelrec = new Rectangle(fireboltposition.X, fireboltposition.Y, fireboltwidth, fireboltheight);
            return Labelrec;
        }

        public void Draw(Graphics g)
        {    
            Rectangle fireboltdecR = new Rectangle(fireboltposition.X, fireboltposition.Y, fireboltwidth, fireboltheight);
            Rectangle fireboltsecR = new Rectangle(0, 0, fireboltwidth, fireboltheight);

            g.DrawImage(firebolt0, fireboltdecR, fireboltsecR, GraphicsUnit.Pixel);
        }
    }

我该如何让火球朝向吃豆人的方向移动? 我有一个form1,当我按下“F”键时,它会发射一个火球,但似乎无法产生火球图像。为什么会这样?
namespace TestingPacman
{
    public partial class Form1 : Form
    {
        // int inc = 0;
        Eater TheEater = new Eater(100,100);
        TimeDisplay time = new TimeDisplay();
        int sec = 0;
        Score score = new Score();
        int countofeaten=0;
        Random r = new Random();
        private List<Label> redlabels = new List<Label>();
        private List<Label> bluelabels = new List<Label>();
        Firebolt firebolt;
        List<Firebolt> listfirebolt = new List<Firebolt>();

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.FillRectangle(Brushes.White, 0, 0, this.ClientRectangle.Width, ClientRectangle.Height);
            TheEater.Draw(g);

            foreach(Firebolt f in listfirebolt)
            f.Draw(g);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            timer1.Enabled = true;
            string result = e.KeyData.ToString();              
            Invalidate(TheEater.GetFrame());
            switch (result)
            {
                case "D1":
                    if (TheEater.eaterwidth >= 9 && TheEater.eaterheight >= 9)
                    {
                        TheEater.eaterwidth++;
                        TheEater.eaterheight++;
                    }
                    break;    
                case "F":
                    listfirebolt.Add(firebolt = new Firebolt(TheEater.Position.X, TheEater.Position.Y));
                    Invalidate(firebolt.GetFrame());                 
                    break;
                case "D2":
                    if (TheEater.eaterwidth > 10 && TheEater.eaterheight > 10)
                    {
                        TheEater.eaterwidth--;
                        TheEater.eaterheight--;
                    }
                    break;    
                case "D9": TheEater.inc=TheEater.inc+2;
                    break;
                case "D0": TheEater.inc=TheEater.inc-2;
                    break;
                case "Left":
                    TheEater.MoveLeft(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                case "Right":
                    TheEater.MoveRight(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                case "Up":
                    TheEater.MoveUp(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                case "Down":
                    TheEater.MoveDown(ClientRectangle);
                    Invalidate(TheEater.GetFrame());
                    break;
                default:
                    break;    
            } 
            RemoveifIntersected();
            }                
            label2.Text = score.Iskore.ToString();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            label1.Text = time.FormatTime(sec++);
        }
    }
}

2
你正在对游戏世界造成巨大的不公。 - BoltClock
3
我不确定为什么人们要投票关闭这个问题,而不是问你一些问题以便他们可以帮助你... - CaffGeek
这就是为什么它是一个修改版的吃豆人游戏,以练习编程技能。 - jeo
1
是的,这就是为什么我投票支持重新开放的原因。 - Ta01
2
这里有一个起点:http://www.dreamincode.net/forums/topic/99860-creating-games-in-c%23-part-i/。基本上,当你发射火球时,你需要以某种方式存储火球的方向,然后在每个游戏循环中以一定速率和碰撞检测的方式沿着该方向移动火球。一个简单的方法是存储火球的X和Y速度,所以如果它向左射击,它的Y=0,X=-1,然后通过设置PositionX += VelocityX和PositionY = VelocityY来定期更新火球的位置。 - deepee1
显示剩余7条评论
1个回答

8

Jeo,你的代码中缺少“时间”这个概念。据我所知,你的游戏只有在按键时才会有反应。你真正需要的是在游戏中传递时间的机制。这几乎总是通过对称地调用“游戏循环”来完成的。下面是一个可能适合你的游戏的游戏循环的快速示例:

class Mob
{
  float XPos;
  float YPos;
  float XVel;
  float YVel;
}
List<Mob> EveryThingMovable = new List<Mob>();

void GameLoop() //This loop is called 30 times every second... use a timer or whatever, there are far more sophisticated models, but for a first attaempt at a game it's easiest. 
{
  MoveEverybody(); //make a function that moves everything that can move
  //Later you will have to add collision detection to stop pacman from moving through walls
  CollideFireballs(); //Check if a fireball hits the bad guys
  //More game stuff...



}

void MoveEverybody()
{
  foreach(Mob dude in EverythingMovable)
  { 
     ifDoesntHitWall(dude)
     {
       dude.XPos += dude.XVel;
       dude.YPos += dude.YVel;
     }
  }
}

无论如何,阅读关于游戏循环的概念,我认为这是你在前进方面最大的障碍。


完全正确...我正在输入类似的答案。我只会添加一个Speed和Direction属性到你的Firebolt类,然后使用它们来确定你的Draw方法中的精灵位置。一旦你解决了这个问题,你可以考虑研究XNA,它是专门为这种工作而构建的:http://msdn.microsoft.com/en-us/aa937791。我个人认为这种问题对于SO来说绝对没问题,但你也可以查看这个新的堆栈交换:http://codegolf.stackexchange.com/。 - Chris B. Behrens
问题是我甚至不能在按下F键时产生火焰箭。如果我能够产生火焰箭,那么我会为火焰箭的运动使用时间。 - jeo
我怀疑你的代码直接跳过了Form1_KeyDown事件。这可能是它无法工作的最简单方法... 另外,fireBolt0.width和/或height为0。在上面添加一个调试器,那应该很快告诉你问题出在哪里。 - Chris B. Behrens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接