int main(int argc, char *argv[]) { int i, j; for(i = 0; i < LARGE; i++) list[i] = random() % LARGE; if( level_1() % 2 == 0) printf("level 1 ... Success!\n"); level_2(); level_3(); level_4(); return 1; }
int main(int argc, char* argv[]) { srand(time(NULL)); previoustime = 0; previoustime_deaths = currenttime; previoustime_creation_enemies = -10000; previoustime_bonus = -15000; time_bouclier = -5000; previoustime = currenttime; Uint8 *key = SDL_GetKeyState(NULL); /* initialize video system */ SDL_Init(SDL_INIT_VIDEO); /* set the title bar */ SDL_WM_SetCaption("SDL Move", "SDL Move"); /* create window */ screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0); /*begin*/ printf("Appuyez sur Entrée pour commencer la partie\n"); temp = SDL_LoadBMP("debut.bmp"); wall = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); while (!begin) { if (SDL_PollEvent(&event)) { HandleEvent(event); } SDL_BlitSurface(wall, NULL, screen, &imWall); SDL_UpdateRect(screen,0,0,0,0); } /* load sprite */ temp = SDL_LoadBMP("salameche.bmp"); spritetemp = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); perso = sprite_cons(0, 362, 110, 0, spritetemp, colorkey, 5, NULL, 's', 5, 22); temp = SDL_LoadBMP("heart.bmp"); heart = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); SDL_SetColorKey(heart, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); bonus_vie = sprite_cons(rand()%(LEVEL_WIDTH-16), rand()%(LEVEL_HEIGHT/2-16)+LEVEL_HEIGHT/2, 0, 0, heart, colorkey, 0, NULL,'b', 0, 16); temp = SDL_LoadBMP("abri.bmp"); abri = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); SDL_SetColorKey(heart, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); bonus_abri = sprite_cons(rand()%(LEVEL_WIDTH-40), rand()%(LEVEL_HEIGHT/2-40)+LEVEL_HEIGHT/2, 0, 0, abri, colorkey, 0, NULL,'b', 0, 40); /*temp = SDL_LoadBMP("lanceflamme.bmp"); lance_flamme = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); SDL_SetColorKey(heart, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); bonus_lf = sprite_cons(rand()%(LEVEL_WIDTH-47), rand()%(LEVEL_HEIGHT/2-8)+LEVEL_HEIGHT/2, 0, 0, lance_flamme, colorkey, 7, NULL,'b', 0, 30);*/ temp = SDL_LoadBMP("bloc.bmp"); stone1 = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); temp = SDL_LoadBMP("bloc2.bmp"); stone2 = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); temp = SDL_LoadBMP("bloc3.bmp"); stone3 = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); /* load wall */ temp = SDL_LoadBMP("fond.bmp"); wall = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); /* set wall position */ imWall.x = 0; imWall.y = 0; gameover = 0; /* message pump */ currenttime2 = SDL_GetTicks(); int nb_enemies, randnumber; while (!gameover) { currenttime = SDL_GetTicks(); if (SDL_PollEvent(&event)) { HandleEvent(event); } if (currenttime - currenttime2 > 50){ currenttime2 = currenttime; MultiEvent(key); //apparition ennemis if (currenttime - previoustime_creation_enemies > delta_creation_enemies && niveau !=3) { for (nb_enemies = 0; nb_enemies < 5; nb_enemies = nb_enemies +1) { randnumber = rand()%3; if (randnumber == 0) { temp = SDL_LoadBMP("taupi.bmp"); spritetemp = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); list_enemy = cons(sprite_cons(rand()%(LEVEL_WIDTH-22),rand()%(LEVEL_HEIGHT-22), 0, 0, spritetemp, colorkey, 0, NULL, 't', 2, 22),list_enemy); } else if (randnumber == 1) { temp = SDL_LoadBMP("cara.bmp"); spritetemp = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); list_enemy = cons(sprite_cons(rand()%(LEVEL_WIDTH-25),rand()%(LEVEL_HEIGHT-25), 0, 0, spritetemp, colorkey, -3, NULL, 'c',2, 25),list_enemy); } else { temp = SDL_LoadBMP("meta.bmp"); spritetemp = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); colorkey = SDL_MapRGB(screen->format, 0, 255, 255); list_enemy = cons(sprite_cons(rand()%(LEVEL_WIDTH-20),rand()%(LEVEL_HEIGHT-20), 0, 0, spritetemp, colorkey, -5, NULL, 'm',2, 20),list_enemy); } test_apparition(first_of(list_enemy)); } previoustime_creation_enemies = currenttime; } // test arret gauche if (pasgauche && !pasdroit) { arret_gauche(perso); pasgauche = false; } // test arret droit if (pasdroit && !pasgauche) { arret_droit(perso); pasdroit = false; } // saut if (jump) { jump_perso(perso); } else { //gravitation i = (perso -> imSprite.x + perso -> posSprite.h/2)/32; j = (perso -> imSprite.y + perso -> posSprite.h +vy)/32; surbloc = tab_blocs[j][i] != 0; sursol = perso ->imSprite.y == 383 - perso -> posSprite.h ; if (!surbloc && perso->imSprite.y < 361) { vy = 7; perso->imSprite.y += vy; } j = (perso -> imSprite.y + perso -> posSprite.h + vy)/32; surbloc = tab_blocs[j][i] != 0; sursol = perso->imSprite.y >= 361; if (surbloc && !sursol) { perso->imSprite.y = j*32 - perso->posSprite.h; } if (sursol) { perso->imSprite.y = 361; } } //bonus if (currenttime - previoustime_bonus > 15000 && currenttime-time_bouclier>=5000) { placement_bonus(); } if (collision(perso, bonus_vie)) { deaths = deaths - 1; bonus_vie->imSprite.x = LEVEL_WIDTH + 1; previoustime_bonus = currenttime; } if (currenttime-time_bouclier >= 5000 && immunite){ bonus_abri->imSprite.x = LEVEL_WIDTH + 1; immunite = false; } if (collision(perso, bonus_abri)) { bonus_abri->imSprite.x = perso->imSprite.x - (45 - perso->posSprite.h)/3; bonus_abri->imSprite.y = perso->imSprite.y - (45 - perso->posSprite.h)/3; if (!immunite){ time_bouclier = currenttime; } immunite = true; } else { immunite = false; } move_enemy(list_enemy, list_fire_enemy); positionfire(list_fire); list_enemy = list_enemy_destruction (list_fire, list_enemy, &list_fire_enemy, camera); list_fire = list_fire_destruction (list_fire, camera); list_fire_enemy = list_fire_destruction (list_fire_enemy, camera); level_2(); level_3(); } if (currenttime - previoustime_deaths > 1000){ death_temp = deaths; deaths = collision_enemy(list_enemy, perso, deaths, camera); if (death_temp != deaths){ previoustime_deaths = currenttime; } } Setcamera(); SDL_BlitSurface(wall, NULL, screen, &imWall); print_tab (tab_blocs, camera); printlist (list_enemy); imCamera = Calculposition(bonus_vie ->imSprite.x, bonus_vie ->imSprite.y, camera); SDL_BlitSurface(bonus_vie -> spr, &bonus_vie -> posSprite, screen, &imCamera); imCamera = Calculposition(bonus_abri ->imSprite.x, bonus_abri ->imSprite.y, camera); SDL_BlitSurface(bonus_abri -> spr, &bonus_abri -> posSprite, screen, &imCamera); /* draw the sprite */ imCamera = Calculposition(perso ->imSprite.x, perso ->imSprite.y, camera); if (imCamera.x < 0) { imCamera.x = 0; perso -> imSprite.x = 0; } if (imCamera.x > SCREEN_WIDTH-22) { imCamera.x = SCREEN_WIDTH-22; perso -> imSprite.x = LEVEL_WIDTH-22; } SDL_BlitSurface(perso -> spr, &perso ->posSprite, screen, &imCamera); printlist(list_fire); printlist(list_fire_enemy); heart_pos.x = 0; for (i = 0; i < 5 - deaths; i = i+1) { SDL_BlitSurface(heart, NULL, screen, &heart_pos); heart_pos.x = heart_pos.x + 17; } if (niveau==3){ if (!is_empty(list_enemy)){ heart_pos.x = 624; for (i = 0; i < first_of(list_enemy)->life; i = i+1) { SDL_BlitSurface(heart, NULL, screen, &heart_pos); heart_pos.x = heart_pos.x - 17; } }else{ printf("BRAVO, VOUS AVEZ GAGNÉ !\n"); gameover = 1; } } if (deaths >= 5){ printf("DÉSOLÉ, VOUS AVEZ PERDU !\n"); gameover = 1; } /* update the screen */ SDL_UpdateRect(screen,0,0,0,0); SDL_Delay(5); } /* clean up */ SDL_FreeSurface(stone1); SDL_FreeSurface(stone2); SDL_FreeSurface(stone3); SDL_FreeSurface(heart); SDL_FreeSurface(screen); SDL_FreeSurface(wall); free (bonus_vie); SDL_FreeSurface(bonus_abri->spr); free (bonus_abri); SDL_FreeSurface(perso -> spr); free (perso); free_list(&list_fire); free_list(&list_enemy); SDL_Quit(); return 0; }