viernes, 5 de abril de 2013

PakEngine. Capitulo 19. Interficie C++. Físicas dentro del modulo de sprites.

Artículo perteneciente a la sección del PakEngine

Hola a todos,

En el capitulo anterior introducimos el concepto de una clase nueva que englobaba la gestión de sprites a nivel gráfico.

Si nos remontamos al ejemplo del PakPong hay mucho código de juego que es de físicas (colisiones de pelota con las palas, con los extremos del campo, etc...) y que sería bastante bueno para el código introducirlo dentro del modulo de sprites a fin de tenerlo todo empaquetado en un mismo objeto.

Todo cuanto tenemos que hacer es introducir dos nuevas funciones dentro del modulo de sprite:


int Sprite::IsNotInside(Sprite *other)
{
return PHYSICS_NotInside(this->GetX(), this->GetY() , this->GetGraf(),   other->GetX(), other->GetY() , other->GetGraf() );
}

int Sprite::CollisionWith(Sprite *other)
{
return PHYSICS_Collision(this->GetX(), this->GetY() , this->GetGraf(),   other->GetX(), other->GetY() , other->GetGraf() );
}

Nos queda el código mucho más limpio y fácil de entender. Por ejemplo:

if ( bola->CollisionWith(player)  || bola->CollisionWith(foe) )

int mask = bola->IsNotInside(campo);


Aquí os dejo el código del PakPong. En próximos capítulos seguiremos trabajando para que el código del juego sea mínimo y esto nos permita mejorar nuestra calidad del código


void PlayerMovement(PakEngine *p, Sprite *player)
{
if(p->Input.KeyIsPressed("up") && ( player->GetY() > 10 ) )
player->Move(0,-1);

if(p->Input.KeyIsPressed("down") && (  player->GetY() < 410 ) )
player->Move(0,1);

if(p->Input.KeyIsPressed("left") && (  player->GetX() > 10 ) )
player->Move(-1,0);

if(p->Input.KeyIsPressed("right") && ( player->GetX() < 310 ) )
player->Move(1,0);
}

int main(void)
{
  PakEngine p("PakPong++",640,480);
  Sprite *bola = new Sprite(300,300,20,"Resources\\bola.png");
  Sprite *player = new Sprite(100,100,50,"Resources\\pala.png");
  Sprite *foe = new Sprite(400,200,50,"Resources\\pala.png");
  Sprite *campo  = new Sprite(0,0,100   ,"Resources\\campo.png");

  int xvel = 1, yvel = 1;
  int boing;

  boing = p.Sound.Load("Resources\\boing.wav");

 //Loop de engine
 while(p.Render())
 {
        PlayerMovement(&p,player);

bola->Move(xvel,yvel);

int mask = bola->IsNotInside(campo);

if(mask)
PAKENGINE_Play(boing);

if(mask&PHYSICS_RIGHT)
{
bola->Move(-1,0);
xvel = - (rand()%3 + 1);
yvel += (rand()%3 -1);
}

if(mask&PHYSICS_LEFT)
{
bola->Move(1,0);
xvel = (rand()%3 + 1);
yvel += (rand()%3 -1);
}

if(mask&PHYSICS_UP)
{
bola->Move(0,1);
xvel += (rand()%3 - 1);
yvel = (rand()%3 +1);
}

if(mask&PHYSICS_DOWN)
{
bola->Move(0,-1);
xvel += (rand()%3 - 1);
yvel = - (rand()%3 + 1);
}

if(bola->GetX() > foe->GetX())
{
if(foe->GetX() < 630)
foe->Move(1,0);
}
else
{
if(foe->GetX() > 320)
foe->Move(-1,0);
}

if(bola->GetY() > foe->GetY())
{
if( foe->GetY() < 410 )
foe->Move(0,1);
}
else
{
if ( foe->GetY() > 10 )
foe->Move(0,-1);
}

if ( bola->CollisionWith(player)  || bola->CollisionWith(foe) )
{
xvel =- xvel;
yvel += (rand()%3 -1);
}

p.Text.Draw(200,5,1,"PAK PONG");
campo->Draw();
player->Draw();
foe->Draw();
bola->Draw();
 }

 delete(&p);

 return 0;
}

Como podréis observar aún quedan bastantes cosas para ir arreglando pero poco a poco lo iremos mejorando.

Como siempre , podéis acceder al código completo desde el svn.

Espero que os haya gustado,

Nos vemos



LordPakusBlog

0 comentarios :

Publicar un comentario

Entradas populares