
- Código: Seleccionar todo
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <SDL.h>
typedef struct enemigo
{
SDL_Rect sprite;
int enemigo_vivo;
}Enemigo;
void Teclado();
bool colision( SDL_Rect A, SDL_Rect B );
const int PANTALLA_ANCHO = 800;
const int PANTALLA_ALTO = 600;
const int VEL_MAX = 3;
const int MAX_ENEMIGOS = 5;
SDL_Surface *pantalla;
SDL_Rect pantalla_rect;
SDL_Rect PJ;
//Enemigo enemigo[MAX_ENEMIGOS];
SDL_Event evento;
bool salir = false;
char TITULO[]="PROGRAMA DE MANEJO DE PROCESOS 23-DIC-2012";
int vel_x;
int vel_y;
using namespace std;
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_VIDEO );
SDL_WM_SetCaption( TITULO, NULL );
pantalla = SDL_SetVideoMode(PANTALLA_ANCHO,PANTALLA_ALTO,24,SDL_HWSURFACE);
srand(time(NULL));
pantalla_rect.x = 0;
pantalla_rect.y = 0;
pantalla_rect.w = PANTALLA_ANCHO;
pantalla_rect.h = PANTALLA_ALTO;
PJ.x = 0;
PJ.y = 0;
PJ.w = 50;
PJ.h = 50;
vector <Enemigo> enemigo;
for( int i = 0; i<MAX_ENEMIGOS;i++)
{
Enemigo temp;
temp.sprite.x = rand()%801;
temp.sprite.y = rand()%601;
temp.sprite.w = 50;
temp.sprite.h = 50;
temp.enemigo_vivo = 1;
enemigo.push_back(temp);
}
while( salir==false)
{
while( SDL_PollEvent( &evento ) )
{
Teclado();
if( evento.type == SDL_QUIT ){ salir = true; }
if(evento.key.keysym.sym == SDLK_ESCAPE ){ salir = true;}
}
PJ.x += vel_x;
PJ.y += vel_y;
for(int i = 0; i<MAX_ENEMIGOS;i++)
{
if(colision(PJ,enemigo[i].sprite))
{
enemigo.erase(enemigo.begin()+i);
cout << "El PJ colisiono con el enemigo # " << i << endl;
}
}
if(PJ.x<0)PJ.x=0;
if(PJ.x+50>PANTALLA_ANCHO)PJ.x-= vel_x;
if(PJ.y<0)PJ.y=0;
if(PJ.y+50>PANTALLA_ALTO)PJ.y-= vel_y;
SDL_FillRect(pantalla,&pantalla_rect,SDL_MapRGB(pantalla->format,255,255,255));
SDL_FillRect(pantalla,&PJ,SDL_MapRGB(pantalla->format,92,163,99));
for(int i = 0; i<enemigo.size();i++)
{
SDL_FillRect(pantalla,&enemigo[i].sprite,SDL_MapRGB(pantalla->format,241,80,88));
}
SDL_Flip( pantalla );
}
SDL_Quit ();
return 0;
}
void Teclado(){
if( evento.type == SDL_KEYDOWN )
{
switch( evento.key.keysym.sym )
{
case SDLK_UP: vel_y -= VEL_MAX; break;
case SDLK_DOWN: vel_y += VEL_MAX; break;
case SDLK_LEFT: vel_x -= VEL_MAX; break;
case SDLK_RIGHT: vel_x += VEL_MAX; break;
}
}
else if( evento.type == SDL_KEYUP )
{
switch( evento.key.keysym.sym )
{
case SDLK_UP: vel_y += VEL_MAX; break;
case SDLK_DOWN: vel_y -= VEL_MAX; break;
case SDLK_LEFT: vel_x += VEL_MAX; break;
case SDLK_RIGHT: vel_x -= VEL_MAX; break;
}
}
}
bool colision( SDL_Rect A, SDL_Rect B )
{
//Lados de los rectangulos
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calcular los lados del rectangulo A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//calcular los lados del rectangulo B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//Si alguno de los lados de A esta fuera de B
if( bottomA <= topB ){ return false; }
if( topA >= bottomB ){ return false; }
if( rightA <= leftB ){ return false; }
if( leftA >= rightB ){ return false; }
//Si ninguno de los lados de A esta fuera de B
return true;
}