summaryrefslogtreecommitdiff
path: root/jeu-test
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2010-09-11 22:50:28 +0000
committerLudovic Pouzenc <ludovic@pouzenc.fr>2010-09-11 22:50:28 +0000
commiteecc234013c0edef66197837dc94d07d7002f783 (patch)
treedc8b94c9b1431a27bef8f93b3f554cdb0b3d311f /jeu-test
parent1d882c00f645623a756f4d7a290676ea1332fbb1 (diff)
download2010-netlemmings-eecc234013c0edef66197837dc94d07d7002f783.tar.gz
2010-netlemmings-eecc234013c0edef66197837dc94d07d7002f783.tar.bz2
2010-netlemmings-eecc234013c0edef66197837dc94d07d7002f783.zip
Correction des bugs des modifs de Dams :D Enfait il y avais une boucle de détection de collision qui comparait le ième alien avec le ième joueur... au lieu du jième joueur, du coup, hors tableau, tout ça... J'ai rendu générique sur le nbre de joueur une bonne partie du code, y compris la key map ! il reste un bug dans la collision entre les aliens et les joueurs et du coup le premier alien qui sos de l'écran par le bas plante le jeu. Ah, et les touches c'est flèches gauche, droite et HAUT (à la place de espace) et q,d,z.
git-svn-id: file:///var/svn/2010-netlemmings/trunk@8 077b3477-7977-48bd-8428-443f22f7bfda
Diffstat (limited to 'jeu-test')
-rw-r--r--jeu-test/aliens-1.0.2/aliens.c121
1 files changed, 57 insertions, 64 deletions
diff --git a/jeu-test/aliens-1.0.2/aliens.c b/jeu-test/aliens-1.0.2/aliens.c
index beade4f..05b6a0d 100644
--- a/jeu-test/aliens-1.0.2/aliens.c
+++ b/jeu-test/aliens-1.0.2/aliens.c
@@ -49,6 +49,13 @@
#define EXPLODE_TIME 4
#define NUMBER_PLAYER 2
+#define KEYS_COUNT 3
+#define KEY_IDX_LEFT 0
+#define KEY_IDX_RIGHT 1
+#define KEY_IDX_FIRE 2
+
+int keymap[NUMBER_PLAYER][KEYS_COUNT] = {{SDLK_LEFT,SDLK_RIGHT,SDLK_UP},{SDLK_q,SDLK_d,SDLK_z}};
+
typedef struct {
int alive;
int facing;
@@ -61,7 +68,7 @@ SDL_Surface *screen;
SDL_Surface *background;
/**/
object player[NUMBER_PLAYER];
-int reloading;
+int reloading[NUMBER_PLAYER];
object shots[MAX_SHOTS];
/**/
object aliens[MAX_ALIENS];
@@ -184,9 +191,6 @@ void FreeData(void)
/* Free graphics */
SDL_FreeSurface(player[0].image);
- //FIXME: ligne utile ?
- //SDL_FreeSurface(player[1].image);
-
SDL_FreeSurface(shots[0].image);
SDL_FreeSurface(aliens[0].image);
SDL_FreeSurface(explosions[0].image);
@@ -308,6 +312,14 @@ void WaitFrame(void)
next_tick = this_tick + (1000/FRAMES_PER_SEC);
}
+int players_alive(object *p) {
+ int i;
+ for (i=0;i<NUMBER_PLAYER;i++) {
+ if (player[i].alive==0) { return 0; }
+ }
+ return 1;
+}
+
/* This of course can be optimized :-) */
void RunGame(void)
{
@@ -332,7 +344,6 @@ void RunGame(void)
for (i=0;i<NUMBER_PLAYER;++i){
player[i].alive = 1;
- //TODO : verif les bornes et les positions
player[i].x = (screen->w - player[i].image->w)/(NUMBER_PLAYER+1)*(i+1);
player[i].y = (screen->h - player[i].image->h) - 1;
player[i].facing = 0;
@@ -350,8 +361,7 @@ void RunGame(void)
DrawObject(&aliens[0]);
UpdateScreen();
- //FIXME faire pour tous les player
- while ( player[0].alive && player[1].alive ) {
+ while ( players_alive(player) ) {
/* Wait for the next frame */
WaitFrame();
@@ -396,47 +406,32 @@ void RunGame(void)
}
/* Create new shots */
- if ( ! reloading ) {
- if ( keys[SDLK_SPACE] == SDL_PRESSED ) {
- for ( i=0; i<MAX_SHOTS; ++i ) {
- if ( ! shots[i].alive ) {
- break;
+ for (j=0;j<NUMBER_PLAYER;++j){
+ if ( ! reloading[j] ) {
+ if ( keys[keymap[j][KEY_IDX_FIRE]] == SDL_PRESSED ) {
+ for ( i=0; i<MAX_SHOTS; ++i ) {
+ if ( ! shots[i].alive ) {
+ break;
+ }
+ }
+ if ( i != MAX_SHOTS ) {
+ //FIXME : shots[] doit être par joueur
+ shots[i].x = player[j].x + (player[j].image->w-shots[i].image->w)/2;
+ shots[i].y = player[j].y - shots[i].image->h;
+ shots[i].alive = 1;
+ Mix_PlayChannel(SHOT_WAV, sounds[SHOT_WAV], 0);
}
- }
- if ( i != MAX_SHOTS ) {
- //FIXME : c pour tester => je fais tirer les 2 joueurs en même temps
- // on va rester sur le player 0 ...
- shots[i].x = player[0].x + (player[0].image->w-shots[i].image->w)/2;
- shots[i].y = player[0].y - shots[i].image->h;
-
- /*shots[i].x = player[1].x +
- (player[1].image->w-shots[i].image->w)/2;
- shots[i].y = player[1].y -
- shots[i].image->h;*/
- shots[i].alive = 1;
- Mix_PlayChannel(SHOT_WAV,
- sounds[SHOT_WAV], 0);
}
}
+ reloading[j] = (keys[keymap[j][KEY_IDX_FIRE]] == SDL_PRESSED );
}
- reloading = (keys[SDLK_SPACE] == SDL_PRESSED);
- /* Move the player */
- player[0].facing = 0;
- player[1].facing = 0;
- if ( keys[SDLK_RIGHT] ) {
- ++player[0].facing;
- }
- if ( keys[SDLK_LEFT] ) {
- --player[0].facing;
- }
- if ( keys[SDLK_UP] ) {
- ++player[1].facing;
- }
- if ( keys[SDLK_DOWN] ) {
- --player[1].facing;
- }
+ /* Move players */
for(i=0;i<NUMBER_PLAYER;++i){
+ player[i].facing=0;
+ if ( keys[keymap[i][KEY_IDX_LEFT]] ) player[i].facing--;
+ if ( keys[keymap[i][KEY_IDX_RIGHT]] ) player[i].facing++;
+
player[i].x += player[i].facing*PLAYER_SPEED;
if ( player[i].x < 0 ) {
player[i].x = 0;
@@ -490,26 +485,23 @@ void RunGame(void)
}
}
}
- for ( i=0; i<MAX_ALIENS; ++i ) {
- if ( aliens[i].alive && Collide(&player[i], &aliens[i]) ) {
- aliens[i].alive = 0;
- explosions[i].x = aliens[i].x;
- explosions[i].y = aliens[i].y;
- explosions[i].alive = EXPLODE_TIME;
- //FIXME lol hey les aliens doivent exploser sur la case des players :p
- // suicide collectif
- // sinon faire pour tous les players
- player[0].alive = 0;
- player[1].alive = 1;
- explosions[MAX_ALIENS].x = player[0].x;
- explosions[MAX_ALIENS].y = player[0].y;
-
- explosions[MAX_ALIENS].x = player[1].x;
- explosions[MAX_ALIENS].y = player[1].y;
-
- explosions[MAX_ALIENS].alive = EXPLODE_TIME;
- Mix_PlayChannel(EXPLODE_WAV,
- sounds[EXPLODE_WAV], 0);
+
+ // FIXME : Visiblement cette détection de collision chie car l'alien passe à travers les players puis fais planter le jeu quan dil sors de l'écran (erreur X car le sprite est hors de l'écran)
+ for ( j=0; i<NUMBER_PLAYER; ++j ) {
+ for ( i=0; i<MAX_ALIENS; ++i ) {
+ if ( aliens[i].alive && Collide(&player[j], &aliens[i]) ) {
+ aliens[i].alive = 0;
+ explosions[i].x = aliens[i].x;
+ explosions[i].y = aliens[i].y;
+ explosions[i].alive = EXPLODE_TIME;
+ player[i].alive = 0;
+
+ explosions[MAX_ALIENS].x = player[i].x;
+ explosions[MAX_ALIENS].y = player[i].y;
+
+ explosions[MAX_ALIENS].alive = EXPLODE_TIME;
+ Mix_PlayChannel(EXPLODE_WAV, sounds[EXPLODE_WAV], 0);
+ }
}
}
@@ -549,8 +541,9 @@ void RunGame(void)
/* Check for keyboard abort */
if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
- player[0].alive = 0;
- player[1].alive = 0;
+ for(i=0;i<NUMBER_PLAYER;++i){
+ player[i].alive = 0;
+ }
}
}