我创造了一个改良的吃豆人,如何让他喷火

本文关键字:创造 一个 | 更新日期: 2023-09-27 18:07:01

我创建了一个修改后的吃豆人,但我想添加一个从吃豆人嘴里射出的火弩箭。我的代码是:

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

我如何让火弩箭朝着吃豆人面对的方向移动?我有一个表单,当我按"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++);
        }
    }
}

我创造了一个改良的吃豆人,如何让他喷火

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

无论如何,阅读游戏循环的理念,我认为这是你为了继续前进而没有通过的最大障碍。