Gabs86
11/09/2008, 01:58
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
// Macro definitions
enum direccion{izquierda, derecha};
// Structure definitions
struct barraPong{
SDL_Rect area;
int vel;
int aceleracion;
} jugador, enemigo;
struct pelota{
SDL_Rect punto;
}pelota;
// Function definitions
void moverBarra(barraPong *barra, direccion dir){
if(dir==izquierda&&barra->area.x-10>0){
barra->area.x-=10;
}else if(dir==derecha&&barra->area.x+barra->area.w+10<800){
barra->area.x+=10;
}
barra->area.x+=(barra->aceleracion);
}
void iniRect(SDL_Rect &rect,int x, int y, int w, int h){
rect.x=x;
rect.y=y;
rect.w=w;
rect.h=h;
}
// Main program
int main(int argc, char *argv[])
{
// Declare variables
int done=0;
// Initialise game data
SDL_Surface *screen;
Uint8 *keys=SDL_GetKeyState(NULL);
SDL_Rect dest={0,0,800,480};
SDL_Event event;
// Parse command-line arguments
// Initialise SDL
if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO<0)){
printf("No se pude iniciar SDL: %s\n", SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(800,480,24,SDL_HWSURFACE|SDL_DOUB LEBUF|SDL_FULLSCREEN);
if(screen==NULL){
printf("No se ha podido iniciar el modo gráfico");
exit(1);
}
iniRect(jugador.area,50,445,90,15);
iniRect(enemigo.area,50,25,90,15);
direccion dir;
while(done==0){
// (Re)draw the screen
SDL_FillRect(screen, &dest,SDL_MapRGB(screen->format,0,0,0));
SDL_FillRect(screen, &jugador.area, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_FillRect(screen, &enemigo.area, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_Flip(screen);
if(SDL_PollEvent(&event)){
// Handle events (keyboard input, mainly)
if(event.type==SDL_KEYDOWN){
if(keys[SDLK_ESCAPE]==1){
done=1;
}
if(keys[SDLK_LEFT]==1){
dir=izquierda;
}else if(keys[SDLK_RIGHT]==1){
dir=derecha;
}
}
// Give the CPU a break
}
moverBarra(&jugador,dir);
SDL_Delay(20);
}
return 0;
}
Lo vuelvo a poner, esque lo he borrado antes de que nadie hubiese contestado, y como yo solo he encontrado la solución he pensado que sería mejor borrarlo para no tener basura en el foro :P.
#include <stdlib.h>
#include <SDL/SDL.h>
// Macro definitions
enum direccion{izquierda, derecha};
// Structure definitions
struct barraPong{
SDL_Rect area;
int vel;
int aceleracion;
} jugador, enemigo;
struct pelota{
SDL_Rect punto;
}pelota;
// Function definitions
void moverBarra(barraPong *barra, direccion dir){
if(dir==izquierda&&barra->area.x-10>0){
barra->area.x-=10;
}else if(dir==derecha&&barra->area.x+barra->area.w+10<800){
barra->area.x+=10;
}
barra->area.x+=(barra->aceleracion);
}
void iniRect(SDL_Rect &rect,int x, int y, int w, int h){
rect.x=x;
rect.y=y;
rect.w=w;
rect.h=h;
}
// Main program
int main(int argc, char *argv[])
{
// Declare variables
int done=0;
// Initialise game data
SDL_Surface *screen;
Uint8 *keys=SDL_GetKeyState(NULL);
SDL_Rect dest={0,0,800,480};
SDL_Event event;
// Parse command-line arguments
// Initialise SDL
if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO<0)){
printf("No se pude iniciar SDL: %s\n", SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(800,480,24,SDL_HWSURFACE|SDL_DOUB LEBUF|SDL_FULLSCREEN);
if(screen==NULL){
printf("No se ha podido iniciar el modo gráfico");
exit(1);
}
iniRect(jugador.area,50,445,90,15);
iniRect(enemigo.area,50,25,90,15);
direccion dir;
while(done==0){
// (Re)draw the screen
SDL_FillRect(screen, &dest,SDL_MapRGB(screen->format,0,0,0));
SDL_FillRect(screen, &jugador.area, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_FillRect(screen, &enemigo.area, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_Flip(screen);
if(SDL_PollEvent(&event)){
// Handle events (keyboard input, mainly)
if(event.type==SDL_KEYDOWN){
if(keys[SDLK_ESCAPE]==1){
done=1;
}
if(keys[SDLK_LEFT]==1){
dir=izquierda;
}else if(keys[SDLK_RIGHT]==1){
dir=derecha;
}
}
// Give the CPU a break
}
moverBarra(&jugador,dir);
SDL_Delay(20);
}
return 0;
}
Lo vuelvo a poner, esque lo he borrado antes de que nadie hubiese contestado, y como yo solo he encontrado la solución he pensado que sería mejor borrarlo para no tener basura en el foro :P.