File moon-lander-overwork1.patch of Package moon-lander
--- moon_lander.c_org 2011-06-19 12:22:07.000000000 +0200
+++ moon_lander.c 2011-06-19 19:41:40.000000000 +0200
@@ -19,6 +19,8 @@
*
* 05/06/2005 - Dr. Robert Meier improved the autopilot to act as a
* flight director.
+*
+* 18/06/2011 - bugs fixed about background-pics / game-parameters and code cleaned by Uli
*
*/
@@ -192,10 +194,7 @@ void get_new_background(Game *game) {
int count = 0;
/* read images/backgrounds dir and choose a random image from there.
- * put it's filename in image_file
- */
-
-
+ put it's filename in image_file */
sprintf(filename, "%simages/backgrounds", DATAPATH);
@@ -207,17 +206,16 @@ void get_new_background(Game *game) {
while (!done){
if ( (files[count] = readdir(dir)) ){
-
- //printf("I see - %d %s\n", count, files[count]->d_name);
- if ((strchr(files[count]->d_name, '.') - files[count]->d_name) > 0) {
- count++;
+ //printf("I see - %d %s\n", count, files[count]->d_name);
+ if ((strchr(files[count]->d_name, '.') - files[count]->d_name) > 0) { // filtering the "." ".." and ".Directory"
+ count++; // if first char of filename is NOT a "." use it as a picture
}
}
else{
done = 1;
}
- if (count > 99) {
+ if (count > 99) { // not more then 99 background pics
done = 1;
}
}
@@ -230,14 +228,17 @@ void get_new_background(Game *game) {
exit(0);
}
+ // randomize backgroundpics
+#ifndef WIN32_BUILD
+ game->back_no = 0 + ( random() % (count)); // a + ( rand() % ( b - a + 1)) a=0, b=count-1
+#endif
-
-
- //game->back_no++;
- srand( (unsigned) time(NULL) ) ;
- game->back_no = 0 + ( rand() % (count)); //a + ( rand() % ( b - a + 1)) a=0, b=count-1
-
-// if (game->back_no < 0){
+#ifdef WIN32_BUILD
+ game->back_no = 0 + ( rand() % (count)); // a + ( rand() % ( b - a + 1)) a=0, b=count-1
+#endif
+
+//game->back_no++;
+// if (game->back_no < 0){ // game->back_no is not used because of random
// game->back_no = 0;
// }
@@ -245,7 +246,6 @@ void get_new_background(Game *game) {
// game->back_no = 0;
// }
-
if (game->background.image != NULL){
//printf("about to free background\n");
SDL_FreeSurface(game->background.image);
@@ -255,15 +255,12 @@ void get_new_background(Game *game) {
}
//printf("about to get new background: %d\n", game->back_no );
-
sprintf(filename, "%simages/backgrounds/%s", DATAPATH, files[game->back_no]->d_name);
// printf("got %s\n", filename);
-
new_sprite(&(game->background), filename, 0, 0, 0, 0);
- // printf("got new background\n");
-
+ // printf("got new background\n");
}
/************************************************/
@@ -273,7 +270,7 @@ void save_game(Game *game){
FILE *file;
#ifndef WIN32_BUILD
- struct passwd *pwp;
+ struct passwd *pwp; // linux
/* get user home dir */
@@ -287,7 +284,7 @@ void save_game(Game *game){
#endif
#ifdef WIN32_BUILD
- sprintf(filename, "moon_lander.conf");
+ sprintf(filename, "moon_lander.conf"); //windows
#endif
printf("saving game options in %s\n", filename);
@@ -313,15 +310,8 @@ void load_game(Game *game){
char filename[200];
FILE *file;
- game->opt_num_lives = 3;
- game->opt_lp_bonus = 1;
- game->opt_lp_warn = 1;
- game->opt_prog_grav = 1;
- game->opt_fancy_terrain = 1;
- game->opt_frame_period = 2;
-
#ifndef WIN32_BUILD
- struct passwd *pwp;
+ struct passwd *pwp; //linux
/* get user home dir */
if ( !(pwp = getpwuid(getuid()))){
@@ -334,7 +324,7 @@ void load_game(Game *game){
#endif
#ifdef WIN32_BUILD
- sprintf(filename, "moon_lander.conf");
+ sprintf(filename, "moon_lander.conf"); //windows
#endif
printf("reading game options in %s\n", filename);
@@ -343,13 +333,12 @@ void load_game(Game *game){
fscanf(file,"%d %d %d %d %d %d", &game->opt_num_lives,
&game->opt_lp_bonus, &game->opt_lp_warn, &game->opt_prog_grav,
- &game->opt_fancy_terrain, &game->opt_frame_period);
-
+ &game->opt_fancy_terrain, &game->opt_frame_period);
}
else{
printf("cannot open file for reading: %s - loading defaults\n", filename);
- /* defaults */
+ /* using defaults */
game->opt_num_lives = 3;
game->opt_lp_bonus = 1;
game->opt_lp_warn = 1;
@@ -378,9 +367,6 @@ void options (Game *game) {
Uint8 *key_table;
SDL_Event event;
- get_new_background(game);
- SDL_Flip(game->screen);
-
sprintf(options[0],"%s", "Fancy Terrain");
sprintf(options[1],"%s", "Progressive Gravity");
sprintf(options[2],"%s", "Landing Pad Speed Warning");
@@ -492,10 +478,17 @@ void options (Game *game) {
/* draw the options */
- draw_sprite(game->screen, game->background);
+
+ if (game->background.image == NULL) { // ESC during the startscreen
+ draw_sprite(game->screen, game->gameover_screen); // use / show the startscreen
+ }
+ else { // ESC during the game
+ draw_sprite(game->screen, game->background); // use / show the game background
+ }
+
DT_DrawText("OPTIONS", game->screen, game->big_font, 260, 50);
DT_DrawText("Arrow Keys Select", game->screen, game->small_font, 260, 75);
- DT_DrawText("ENTER changes value", game->screen, game->small_font, 260, 90);
+ DT_DrawText("ENTER / Arrow Keys changes value", game->screen, game->small_font, 260, 90);
DT_DrawText("ESC back to game", game->screen, game->small_font, 260, 105);
for (count = 0; count < 6; count ++) {
@@ -514,14 +507,14 @@ void options (Game *game) {
delay(3);
SDL_Flip(game->screen);
-
- } /* end while */
+ } /* end while */
+
save_game(game);
-
+
/* clear event buffer */
- delay(5);
- while (SDL_PollEvent(&event)){};
+ delay(15); // if somebody press ESC to long
+ while (SDL_PollEvent(&event)){}; // should kill al the wrong ESC key-pressed
}
@@ -553,7 +546,6 @@ void DrawPixel(SDL_Surface *screen, Uint
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
-
}
/************************************************/
@@ -639,10 +631,8 @@ void random_level(Game *game) {
SDL_LockSurface(game->current_level.sprite.image);
#ifndef WIN32_BUILD
- srandom(time(NULL));
-
while ( (y > 230) || (y < 10) ){
- y = random()%TERRAIN_YSIZE;
+ y = random()%TERRAIN_YSIZE; // linux-code
}
miny = ( (random()%(TERRAIN_YSIZE/2)) + 10);
@@ -650,9 +640,7 @@ void random_level(Game *game) {
#endif
#ifdef WIN32_BUILD
- srand(time(NULL));
-
- while ( (y > 230) || (y < 10) ){
+ while ( (y > 230) || (y < 10) ){ // windows-code
y = rand()%TERRAIN_YSIZE;
}
@@ -824,7 +812,7 @@ void random_level(Game *game) {
SDL_UnlockSurface(game->current_level.sprite.image);
new_sprite_surface(&(game->current_level.sprite), game->current_level.sprite.image, 0, TERRAIN_YSIZE, 1);
-
+
game->current_level.fuel = (700 - (game->difficulty * 25));
if (game->current_level.fuel < 300){
@@ -846,17 +834,17 @@ void draw_score(Game *game, int landing_
if (game->small_font > -1){
DT_DrawText(display_string, game->screen, game->small_font, 1, 1 );
}
-
+
sprintf(display_string, "X Velocity %.2f", game->ship.x_vel);
if (game->small_font > -1){
- DT_DrawText(display_string, game->screen, game->small_font, 100, 1);
+ DT_DrawText(display_string, game->screen, game->small_font, 90, 1);
}
sprintf(display_string, "Y Velocity %.2f", game->ship.y_vel);
if (game->small_font > -1){
- DT_DrawText(display_string, game->screen, game->small_font, 250, 1);
+ DT_DrawText(display_string, game->screen, game->small_font, 240, 1);
}
sprintf(display_string, "Score: %d", game->score);
@@ -902,6 +890,7 @@ void game_over(Game *game, int first_tim
game->difficulty = 0;
game->ships_remaining = game->opt_num_lives;
game->gravity = 0.05;
+ game->background.image = NULL; // needed for showing the right background for options-screen
}
if (first_time == 0){
@@ -913,7 +902,8 @@ void game_over(Game *game, int first_tim
DT_DrawText(display_string, game->screen, game->big_font, 240, 190 );
draw_score(game, 0);
draw_sprite(game->screen, game->magigames);
-
+ game->background.image = NULL; // needed for showing the right background for options-screen
+
SDL_Flip(game->screen);
delay(125);
}
@@ -932,7 +922,7 @@ void game_over(Game *game, int first_tim
/* show the game name, etc */
- /* if they just lost, show them thier score and stuff */
+ /* if they just lost, show them their score and stuff */
// if (first_time == 0){
// DT_DrawText("Game Over", game->screen, game->big_font, 252, 248 );
@@ -942,7 +932,7 @@ void game_over(Game *game, int first_tim
DT_DrawText("Arrow keys control the ship", game->screen, game->big_font, 100, 100 );
DT_DrawText("Q quit P pause ESC options", game->screen, game->big_font, 75, 125 );
DT_DrawText("A autopilot F flight director", game->screen, game->big_font, 75, 150 );
- DT_DrawText("Press ENTER to play", game->screen, game->big_font, 175, 175 );
+ DT_DrawText("Press ENTER to play", game->screen, game->big_font, 175, 200 );
DT_DrawText("Score for each round = landing pad score + remaining fuel.", game->screen, game->small_font, 150, 280 );
DT_DrawText("Safe Landing requires X velocity < 0.5 and Y velocity < indicated by landing pad color.", game->screen, game->small_font, 55, 300 );
@@ -1057,19 +1047,15 @@ void game_over(Game *game, int first_tim
}
frame_rate_limiter(game);
-
+
} /* end while */
-
/* reset all the stuff for a new game */
game->score = 0;
game->difficulty = 0;
game->ships_remaining = (game->opt_num_lives - 1);
game->gravity = 0.05;
-
-
-
}
@@ -1084,11 +1070,10 @@ void new_round(Game *game, int died_won)
//printf("new round\n");
/* clear event buffer */
-
while (SDL_PollEvent(&event)){};
/* reset ship */
- game->ship.x = XSIZE / 2;
+ game->ship.x = (XSIZE / 2) - (game->ship.w / 2); // middle of the screen
game->ship.y = 1;
game->ship.y_vel=0;
game->ship.x_vel=0;
@@ -1104,8 +1089,6 @@ void new_round(Game *game, int died_won)
game->difficulty++;
/* get new level */
-
-
random_level(game);
game->fuel = game->current_level.fuel;
@@ -1114,8 +1097,6 @@ void new_round(Game *game, int died_won)
if (game->opt_prog_grav){
game->gravity = 0.05 + (game->difficulty * 0.001 );
}
-
-
}
else if (died_won == 3) { /* starting new game */
@@ -1157,11 +1138,11 @@ void new_round(Game *game, int died_won)
sprintf(display_string, "Level: %d", (game->difficulty) + 1);
DT_DrawText(display_string, game->screen, game->big_font, 250, 150 );
-
- /* flip screen so they show up */
#ifndef NOSOUND
play_audio(game->ready, 1);
#endif
+
+ /* flip screen so they show up */
SDL_Flip(game->screen);
if( game->demo_mode == 1 ) {
@@ -1181,10 +1162,11 @@ void new_round(Game *game, int died_won)
DT_DrawText("Ready...", game->screen, game->big_font, 250, 150 );
- /* flip screen so they show up */
#ifndef NOSOUND
play_audio(game->ready, 1);
#endif
+
+ /* flip screen so they show up */
SDL_Flip(game->screen);
delay(70);
@@ -1201,10 +1183,11 @@ void new_round(Game *game, int died_won)
DT_DrawText("GO!", game->screen, game->big_font, 290, 150 );
- /* flip screen so they show up */
#ifndef NOSOUND
play_audio(game->go, 0);
#endif
+
+ /* flip screen so they show up */
SDL_Flip(game->screen);
delay(25);
@@ -1212,7 +1195,6 @@ void new_round(Game *game, int died_won)
/* clear event buffer */
while (SDL_PollEvent(&event)){}
-
}
@@ -1242,19 +1224,14 @@ void explode (Sprite exploder,
}
/* display fuel */
-
draw_score(game, 1);
- /* Update the screen */
-
+ /* Update the screen */
SDL_Flip(game->screen);
/* timer - so that things run at an even speed regardless of cpu speed */
-
frame_rate_limiter(game);
-
-
}
delay(30);
@@ -1306,7 +1283,6 @@ void win (Game *game, int bonus) {
}
/* Update the screen */
-
SDL_Flip(game->screen);
delay(80);
}
@@ -1886,9 +1862,13 @@ void gameloop(Game *game){
game->ship.x = game->ship.x + game->ship.x_vel;
game->ship.y = game->ship.y + game->ship.y_vel;
+
+ if (game->ship.y <= -game->ship.h+20) { // can“t fly higher !! OK, a little bit just for fun
+ game->ship.y = -game->ship.h+20;
+ }
/* update rocket exhaust position */
-
+
game->thrust.x = (game->ship.x + ( (game->ship.w - game->thrust.w) / 2));
game->thrust.y = (game->ship.y + (game->ship.h -5));
@@ -1988,14 +1968,12 @@ void gameloop(Game *game){
draw_score(game, 1);
/* flip screen so they show up */
-
SDL_Flip(game->screen);
/* check if off screen and way low */
- if (game->ship.y > YSIZE) {
- explode(game->ship, game->current_level.sprite, game );
-
+ if ((game->ship.y > YSIZE) || (game->ship.x < -game->ship.w) || (game->ship.x > XSIZE)) {
+ explode(game->ship, game->current_level.sprite, game );
if( !game->ships_remaining ) {
game->landing_pad = count;
game->state = GAMEOVER;
@@ -2114,8 +2092,24 @@ int main(int argc, char **argv) {
printf( "Sound disabled\n" );
#endif
+#ifndef WIN32_BUILD
+ srandom(time(NULL)); //linux random generater start
+#endif
+
+#ifdef WIN32_BUILD
+ srand(time(NULL)); //windows random generater start
+#endif
+
+
game.demo_mode = 0;
game.autopilot = 0;
+
+ game.opt_num_lives = 3; // initialising the game-parameter,
+ game.opt_lp_bonus = 1; //if config-file is defect or false (older one)
+ game.opt_lp_warn = 1;
+ game.opt_prog_grav = 1;
+ game.opt_fancy_terrain = 1;
+ game.opt_frame_period = 2;
load_game(&game);
@@ -2128,7 +2122,7 @@ int main(int argc, char **argv) {
game.current_level.sprite.x = -1;
game.background.image = NULL;
game.background.x = -1;
- game.back_no = 0;
+//game.back_no = 0; //not longer used, because random backgrounds
@@ -2249,7 +2243,7 @@ int main(int argc, char **argv) {
while(1){
-
+
#ifndef NOSOUND
Mix_HaltChannel(1);
#endif
@@ -2301,7 +2295,7 @@ int main(int argc, char **argv) {
gameloop(&game);
break;
}
-
+
} /* end switch */
}