我创造了一个修改过的pacman,如何让他喘不过气来?

时间:2011-08-29 14:58:40

标签: c# .net winforms

我创造了一个改良的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);
        }
    }

我怎样才能使挡风玻璃沿着pacman面向的方向移动? 我有一个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++);
        }
    }
}

1 个答案:

答案 0 :(得分: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;
     }
  }
}

无论如何,阅读游戏循环的想法,我认为这是你没有通过的最大的冲击,以便继续前进。