/* * PlaceAliens * * Create the aliens. */ void PlaceAliens () { int i; typUnit *alien; /* --- Create each of the aliens --- */ for (i = 0; i < START_ALIENS; i++) { /* --- Create one alien --- */ alien = CreateAlien (); /* --- Add it to the units list --- */ unitList = g_list_append (unitList, alien); } }
/* This of course can be optimized :-) */ void RunGame(void) { int i, j; SDL_Event event; Uint8 *keys; /* Paint the background */ numupdates = 0; for ( i=0; i<screen->w; i += background->w ) { SDL_Rect dst; dst.x = i; dst.y = 0; dst.w = background->w; dst.h = background->h; SDL_BlitSurface(background, NULL, screen, &dst); } SDL_UpdateRect(screen, 0, 0, 0, 0); /* Initialize the objects */ player.alive = 1; player.x = (screen->w - player.image->w)/2; player.y = (screen->h - player.image->h) - 1; player.facing = 0; DrawObject(&player); for ( i=0; i<MAX_SHOTS; ++i ) { shots[i].alive = 0; } for ( i=0; i<MAX_ALIENS; ++i ) { aliens[i].alive = 0; } CreateAlien(); DrawObject(&aliens[0]); UpdateScreen(); while ( player.alive ) { /* Wait for the next frame */ WaitFrame(); /* Poll input queue, run keyboard loop */ while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) return; } keys = SDL_GetKeyState(NULL); /* Erase everything from the screen */ for ( i=0; i<MAX_SHOTS; ++i ) { if ( shots[i].alive ) { EraseObject(&shots[i]); } } for ( i=0; i<MAX_ALIENS; ++i ) { if ( aliens[i].alive ) { EraseObject(&aliens[i]); } } EraseObject(&player); for ( i=0; i<MAX_ALIENS+1; ++i ) { if ( explosions[i].alive ) { EraseObject(&explosions[i]); } } /* Decrement the lifetime of the explosions */ for ( i=0; i<MAX_ALIENS+1; ++i ) { if ( explosions[i].alive ) { --explosions[i].alive; } } /* Create new aliens */ if ( (rand()%ALIEN_ODDS) == 0 ) { CreateAlien(); } /* Create new shots */ if ( ! reloading ) { if ( keys[SDLK_SPACE] == SDL_PRESSED ) { for ( i=0; i<MAX_SHOTS; ++i ) { if ( ! shots[i].alive ) { break; } } if ( i != MAX_SHOTS ) { shots[i].x = player.x + (player.image->w-shots[i].image->w)/2; shots[i].y = player.y - shots[i].image->h; shots[i].alive = 1; Mix_PlayChannel(SHOT_WAV, sounds[SHOT_WAV], 0); } } } reloading = (keys[SDLK_SPACE] == SDL_PRESSED); /* Move the player */ player.facing = 0; if ( keys[SDLK_RIGHT] ) { ++player.facing; } if ( keys[SDLK_LEFT] ) { --player.facing; } player.x += player.facing*PLAYER_SPEED; if ( player.x < 0 ) { player.x = 0; } else if ( player.x >= (screen->w-player.image->w) ) { player.x = (screen->w-player.image->w)-1; } /* Move the aliens */ for ( i=0; i<MAX_ALIENS; ++i ) { if ( aliens[i].alive ) { aliens[i].x += aliens[i].facing*ALIEN_SPEED; if ( aliens[i].x < 0 ) { aliens[i].x = 0; aliens[i].y += aliens[i].image->h; aliens[i].facing = 1; } else if ( aliens[i].x >= (screen->w-aliens[i].image->w) ) { aliens[i].x = (screen->w-aliens[i].image->w)-1; aliens[i].y += aliens[i].image->h; aliens[i].facing = -1; } } } /* Move the shots */ for ( i=0; i<MAX_SHOTS; ++i ) { if ( shots[i].alive ) { shots[i].y -= SHOT_SPEED; if ( shots[i].y < 0 ) { shots[i].alive = 0; } } } /* Detect collisions */ for ( j=0; j<MAX_SHOTS; ++j ) { for ( i=0; i<MAX_ALIENS; ++i ) { if ( shots[j].alive && aliens[i].alive && Collide(&shots[j], &aliens[i]) ) { aliens[i].alive = 0; explosions[i].x = aliens[i].x; explosions[i].y = aliens[i].y; explosions[i].alive = EXPLODE_TIME; Mix_PlayChannel(EXPLODE_WAV, sounds[EXPLODE_WAV], 0); shots[j].alive = 0; break; } } } for ( i=0; i<MAX_ALIENS; ++i ) { if ( aliens[i].alive && Collide(&player, &aliens[i]) ) { aliens[i].alive = 0; explosions[i].x = aliens[i].x; explosions[i].y = aliens[i].y; explosions[i].alive = EXPLODE_TIME; player.alive = 0; explosions[MAX_ALIENS].x = player.x; explosions[MAX_ALIENS].y = player.y; explosions[MAX_ALIENS].alive = EXPLODE_TIME; Mix_PlayChannel(EXPLODE_WAV, sounds[EXPLODE_WAV], 0); } } /* Draw the aliens, shots, player, and explosions */ for ( i=0; i<MAX_ALIENS; ++i ) { if ( aliens[i].alive ) { DrawObject(&aliens[i]); } } for ( i=0; i<MAX_SHOTS; ++i ) { if ( shots[i].alive ) { DrawObject(&shots[i]); } } if ( player.alive ) { DrawObject(&player); } for ( i=0; i<MAX_ALIENS+1; ++i ) { if ( explosions[i].alive ) { DrawObject(&explosions[i]); } } UpdateScreen(); /* Loop the music */ #if defined(PLAY_MOD) || defined(PLAY_MID) if ( ! Mix_PlayingMusic() ) { Mix_PlayMusic(music, 0); } #else if ( ! Mix_Playing(MUSIC_WAV) ) { Mix_PlayChannel(MUSIC_WAV, sounds[MUSIC_WAV], 0); } #endif /* Check for keyboard abort */ if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) { player.alive = 0; } } /* Wait for the player to finish exploding */ while ( Mix_Playing(EXPLODE_WAV) ) { WaitFrame(); } Mix_HaltChannel(-1); return; }