开发者

i created a modified pacman, how to make him breath fire?

I created a modified Pacman, but I want to add a firebolt shooting out from the mouth of the Pacman. My code is:

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

How can I make a firebolt move in the direction the pacman is facing? I have a form1 that when I press "F" it will fire a firebolt but it cant seem to produce the firebolt image. Why is that?

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.eate开发者_开发技巧rwidth > 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, what you are missing in your code is the concept of "Time" as far as I can tell your game only reacts when you press keys. What you really need is a mechanism to pass time in your game. This is almost always done in games with repetitive calls to something called "A Game Loop". Here's a quick example of a game loop that might work for you

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

anyways, read up on the idea of a Game Loop, I think it's the biggest hurtle you haven't passed in order to move ahead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜