domingo, 3 de marzo de 2013

PakEngine. Capitulo 8. Creando nuestro primer juego


Hola a todos,

Ya hemos empezado nuestro primer juego con el PakEngine!!. Un pong para variar :D.



El código del juego es el siguiente:

#include "..\PakEngine\PakEngine.h"
#include <stdlib.h>

int main(void)
{
  int bola,pala,campo;
  int x = 100,y= 100, xene = 400, yene= 200, xbola = 300, ybola = 300, xvel = 1, yvel = 1;

 //Creamos una ventana de tamaño determinado.
 PAKENGINE_Init("PakGame",640,480);

 bola  = PAKENGINE_LoadGraph("Resources\\bola.png");
 pala  = PAKENGINE_LoadGraph("Resources\\pala.png");
 campo = PAKENGINE_LoadGraph("Resources\\campo.png");

 //Loop de engine
 while(PAKENGINE_Render())
 {
    if(PAKENGINE_KeyIsPressed("up") && ( y > 10 ) )
y--;

if(PAKENGINE_KeyIsPressed("down") && ( y < 410 ) )
y++;


if(PAKENGINE_KeyIsPressed("left") && ( x > 10 ) )
x--;

if(PAKENGINE_KeyIsPressed("right") && (x < 310) )
x++;

xbola += xvel;
ybola += yvel;

if (xbola >= 620)
{
xbola = 619;
xvel = - (rand()%3 + 1);
yvel += (rand()%3 -1);
}

if (xbola <= 20)
{
xbola = 21;
xvel = (rand()%3 + 1);
yvel += (rand()%3 -1);
}

if (ybola <= 20)
{
ybola = 21;
yvel = (rand()%3 + 1);
xvel += (rand()%3 -1);
}

if (ybola >= 460)
{
ybola = 459;
yvel = - (rand()%3 + 1);
xvel += (rand()%3 -1);
}

if(xbola > xene)
{
if(xene < 630)
xene++;
}
else
{
if(xene > 320)
xene--;
}
if(ybola > yene)
{
if( yene < 410 )
yene++;
}
else
{
if ( yene > 10 )
yene--;
}

PAKENGINE_DrawGraf(0,0,100,campo);
PAKENGINE_DrawGraf(x,y,50,pala);
PAKENGINE_DrawGraf(xene,yene,50,pala);
PAKENGINE_DrawGraf(xbola,ybola,20,bola);
 }

 //Desinicializamos el motor
 PAKENGINE_DeInit();
 return 0;
}

Como podeis observar hay muchas repeticiones de código que se pueden implementar dentro del motor y algunas cosillas como la detección de colisiones que faltan por hacer, pero vamos por el buen camino.

En breve os colgaré mas avances.

Nos vemos

LordPakusBlog


<< capitulo anterior                                                                                         siguiente capitulo >>

0 comentarios :

Publicar un comentario

Entradas populares