2011-08-29 9 views
5

Creé un Pacman modificado, pero quiero agregar un firebolt disparándole desde la boca del Pacman. Mi código es:creé un pacman modificado, ¿cómo hacerlo respirar?

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

¿Cómo puedo hacer que un firebolt se mueva en la dirección en que se encuentra el pacman? Tengo un formulario1 que cuando presiono "F" disparará un firebolt pero parece que no puede producir la imagen firebolt. ¿Porqué es eso?

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

otra cosa, no puedo hacer los códigos en la misma caja para facilitar la lectura. ¿porqué es eso? – jeo

+2

Estás haciendo del mundo del juego una gran injusticia. – BoltClock

+3

No estoy seguro de por qué las personas votan para cerrar esto en lugar de hacerle preguntas para que puedan ayudar ... – CaffGeek

Respuesta

8

Jeo, lo que se echa en falta en su código es el concepto de "tiempo" por lo que yo puedo decir que su juego sólo reacciona al pulsar las teclas. Lo que realmente necesitas es un mecanismo para pasar el tiempo en tu juego. Esto casi siempre se hace en juegos con llamadas repetitivas a algo llamado "A Game Loop". Aquí hay un ejemplo rápido de un bucle de juego que podría funcionar para usted

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

de todos modos, lea sobre la idea de un bucle de juego, creo que es el más grande hurtle usted no ha pasado con el fin de seguir adelante.

+0

Exactamente ... Estuve en el proceso de tipear una respuesta similar. Solo agregaría que podrías crear una propiedad de Velocidad y Dirección en tu clase Firebolt, y luego usarlas para determinar la posición de los sprites para tu método Draw. Una vez que tenga esto en cuenta, puede considerar buscar en XNA, que está especialmente diseñado para este tipo de trabajo: http://msdn.microsoft.com/en-us/aa937791. Personalmente, creo que este tipo de pregunta está absolutamente bien para SO, pero también puedes consultar este nuevo intercambio de stack: http://codegolf.stackexchange.com/. –

+0

el problema es que ni siquiera puedo producir el firebolt cuando presiono la tecla F. si puedo producir el firebolt, entonces usaré el tiempo para el movimiento del firebolt. – jeo

+0

Sospecho que su código está cayendo directamente a través de Form1_KeyDown. Esa sería la forma más simple de que esto no funcionaría ... alternativamente fireBolt0.width y/o height es 0. Pegue un depurador y eso le indicará rápidamente cuál es el problema. –

Cuestiones relacionadas