int main (int argc, char * argv[]) { int i,j, taille, nb_coup; grille g, gc; carac_navires tab_def_jeu[4]; FILE * fichierIn; if (argc != 3) { printf("main.c : Il n'y a pas le bon nombre d'arguments\n"); exit(1); } else { taille = atoi(argv[1]); fichierIn = open_file(argv[2]); } liste_navires * l; nb_coup = 0; g = creer_grille(taille); init_grille(g, taille); init_carac_navires (tab_def_jeu); gc = creer_grille(taille); init_grille(gc, taille); remplit_grille(g, taille, fichierIn, tab_def_jeu); l = creer_liste_navires(g, taille); while(!jeu_fini(l)) { printf("Quel point ?\n"); if (scanf("%d", &i)!= 1) { printf("Entrer un couple d'entier svp !\n"); getchar(); } printf("la première coordonées est entrée, entrez la deuxième : \n"); if (scanf("%d", &j)!= 1) { printf("Entrer un couple d'entier svp !\n"); getchar(); } printf("Les deux coordonées on bien été récupérées !\n"); joue(g, gc, taille, l, i, j); nb_coup++; affichage(gc, taille); } printf("Félicitation le jeu est terminé !!\nIl vous a fallut %d coup pour coulé tout les bateaux\n", nb_coup); return 0; }
int main() { char filename[106] = "data/grille.txt"; char buffer[500]; char buffer2[300]; SDL_Surface *screen, *temp, *sprite, *backbg, *monster, *wall, *trap, *life, *sword, *armor, *door, *text, *defatt; SDL_Rect rcSprite, rcGrass, rcMonster, rcWall, rcTrap, rcLife, rcSword, rcArmor, rcDoor, rcText, rcDefatt; SDL_Event event; TTF_Font *font; SDL_Color white = {255, 255, 255}; Uint8 *keystate; int colorkey, gameover = 0; SDL_Init(SDL_INIT_VIDEO); TTF_Init(); SDL_WM_SetCaption("Lab", "Lab"); screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0); colorkey = SDL_MapRGB(screen->format, 255, 0, 255); temp = IMG_Load("data/perso.bmp"); sprite = SDL_DisplayFormat(temp); SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); temp = IMG_Load("data/monstre.bmp"); monster = SDL_DisplayFormat(temp); SDL_SetColorKey(monster, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); font = TTF_OpenFont("data/a.ttf", 14); temp = SDL_LoadBMP("data/sol.bmp"); backbg = SDL_DisplayFormat(temp); temp = SDL_LoadBMP("data/mur.bmp"); wall = SDL_DisplayFormat(temp); temp = SDL_LoadBMP("data/piege.bmp"); trap = SDL_DisplayFormat(temp); temp = SDL_LoadBMP("data/potion.bmp"); life = SDL_DisplayFormat(temp); SDL_SetColorKey(life, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); temp = SDL_LoadBMP("data/epee.bmp"); sword = SDL_DisplayFormat(temp); SDL_SetColorKey(sword, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); temp = SDL_LoadBMP("data/armure.bmp"); armor = SDL_DisplayFormat(temp); SDL_SetColorKey(armor, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey); temp = SDL_LoadBMP("data/porte.bmp"); door = SDL_DisplayFormat(temp); joueur * j = alloue_joueur(); grille * g = creer_grille(filename, j); monstre * m = creer_monstre(g); rcSprite.x = j->x*SPRITE_SIZE; rcSprite.y = j->y*SPRITE_SIZE; rcMonster.x = m->x*SPRITE_SIZE; rcMonster.y = m->y*SPRITE_SIZE; rcText.x = 32; rcText.y = SCREEN_HEIGHT - 32; rcDefatt.x = 32; rcDefatt.y = SCREEN_HEIGHT - 16; while ((gameover != 1)) { if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: gameover = 1; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: gameover = 1; case SDLK_q: gameover = 1; break; default: break; } break; } keystate = SDL_GetKeyState(NULL); if ((keystate[SDLK_LEFT]) && (gameover != 2) ) { deplacement(g,j,113); sprintf(buffer, "Remaining lives: %d", j->vie); text = TTF_RenderText_Solid(font, buffer, white); if (g->content[j->y][j->x] == 8) { sprintf(buffer, "You won! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } if (m -> vie > 0) deplacement_monstre(g, j, m); if (j->vie <=0 ) { sprintf(buffer, "You lost! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } rcSprite.x = (j->x)*STEP_SIZE; rcMonster.x = (m->x)*STEP_SIZE; sprintf(buffer2, "Points of attack: %d Points of defense: %d", j->attaque, j->defense); defatt = TTF_RenderText_Solid(font, buffer2, white); } if (keystate[SDLK_RIGHT] && (gameover != 2) ) { deplacement(g,j,100); sprintf(buffer, "Remaining lives: %d", j->vie); text = TTF_RenderText_Solid(font, buffer, white); if (g->content[j->y][j->x] == 8) { sprintf(buffer, "You won! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } if (m -> vie > 0) deplacement_monstre(g, j, m); if (j->vie <=0 ) { sprintf(buffer, "You lost! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } rcSprite.x = (j->x)*STEP_SIZE; rcMonster.x = (m->x)*STEP_SIZE; sprintf(buffer2, "Points of attack: %d Points of defense: %d", j->attaque, j->defense); defatt = TTF_RenderText_Solid(font, buffer2, white); } if (keystate[SDLK_UP] && (gameover != 2) ) { deplacement(g,j,122); sprintf(buffer, "Remaining lives: %d", j->vie); text = TTF_RenderText_Solid(font, buffer, white); if (g->content[j->y][j->x] == 8) { sprintf(buffer, "You won! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } if (m -> vie > 0) deplacement_monstre(g, j, m); if (j->vie <=0 ) { sprintf(buffer, "You lost! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } rcSprite.y = (j->y)*STEP_SIZE; rcMonster.y = (m->y)*STEP_SIZE; sprintf(buffer2, "Points of attack: %d Points of defense: %d", j->attaque, j->defense); defatt = TTF_RenderText_Solid(font, buffer2, white); } if (keystate[SDLK_DOWN] && (gameover != 2) ) { deplacement(g,j,115); sprintf(buffer, "Remaining lives: %d", j->vie); text = TTF_RenderText_Solid(font, buffer, white); if (g->content[j->y][j->x] == 8) { sprintf(buffer, "You won! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } if (m -> vie > 0) deplacement_monstre(g, j, m); if (j->vie <=0 ) { sprintf(buffer, "You lost! Press Q or ESC to quit"); text = TTF_RenderText_Solid(font, buffer, white); gameover = 2; } rcSprite.y = (j->y)*STEP_SIZE; rcMonster.y = (m->y)*STEP_SIZE; sprintf(buffer2, "Points of attack: %d Points of defense: %d", j->attaque, j->defense); defatt = TTF_RenderText_Solid(font, buffer2, white); } if ( rcSprite.x < 0 ) { rcSprite.x = 0; } else if ( rcSprite.x > SCREEN_WIDTH-SPRITE_SIZE ) { rcSprite.x = SCREEN_WIDTH-SPRITE_SIZE; } if ( rcSprite.y < 0 ) { rcSprite.y = 0; } else if ( rcSprite.y > SCREEN_HEIGHT-SPRITE_SIZE ) { rcSprite.y = SCREEN_HEIGHT-SPRITE_SIZE; } for (int i = 0; i < (g -> n); i++) { for (int j = 0; j < (g -> m); j++) { switch (g->content[i][j]) { case 0 : rcGrass.x = j * SPRITE_SIZE; rcGrass.y = i * SPRITE_SIZE; SDL_BlitSurface(backbg, NULL, screen, &rcGrass); break; case 1 : rcWall.x = j * SPRITE_SIZE; rcWall.y = i * SPRITE_SIZE; SDL_BlitSurface(wall, NULL, screen, &rcWall); break; case 2 : rcTrap.x = j * SPRITE_SIZE; rcTrap.y = i * SPRITE_SIZE; SDL_BlitSurface(trap, NULL, screen, &rcTrap); break; case 3 : rcGrass.x = j * SPRITE_SIZE; rcGrass.y = i * SPRITE_SIZE; SDL_BlitSurface(backbg, NULL, screen, &rcGrass); rcLife.x = j * SPRITE_SIZE; rcLife.y = i * SPRITE_SIZE; SDL_BlitSurface(life, NULL, screen, &rcLife); break; case 6 : rcGrass.x = j * SPRITE_SIZE; rcGrass.y = i * SPRITE_SIZE; SDL_BlitSurface(backbg, NULL, screen, &rcGrass); rcArmor.x = j * SPRITE_SIZE; rcArmor.y = i * SPRITE_SIZE; SDL_BlitSurface(armor, NULL, screen, &rcArmor); break; case 7 : rcGrass.x = j * SPRITE_SIZE; rcGrass.y = i * SPRITE_SIZE; SDL_BlitSurface(backbg, NULL, screen, &rcGrass); rcSword.x = j * SPRITE_SIZE; rcSword.y = i * SPRITE_SIZE; SDL_BlitSurface(sword, NULL, screen, &rcSword); break; case 8 : rcDoor.x = j * SPRITE_SIZE; rcDoor.y = i * SPRITE_SIZE; SDL_BlitSurface(door, NULL, screen, &rcDoor); break; default : rcGrass.x = j * SPRITE_SIZE; rcGrass.y = i * SPRITE_SIZE; SDL_BlitSurface(backbg, NULL, screen, &rcGrass); break; } } } drawRectCairo(screen); SDL_BlitSurface(sprite, NULL, screen, &rcSprite); if (m -> vie > 0) SDL_BlitSurface(monster, NULL, screen, &rcMonster); SDL_BlitSurface(text, NULL, screen, &rcText); SDL_BlitSurface(defatt, NULL, screen, &rcDefatt); SDL_UpdateRect(screen,0,0,0,0); } } TTF_CloseFont(font); TTF_Quit(); SDL_FreeSurface(sprite); SDL_FreeSurface(backbg); SDL_FreeSurface(monster); SDL_FreeSurface(wall); SDL_FreeSurface(trap); SDL_FreeSurface(life); SDL_FreeSurface(sword); SDL_FreeSurface(armor); SDL_FreeSurface(door); SDL_FreeSurface(text); SDL_FreeSurface(defatt); SDL_FreeSurface(temp); SDL_FreeSurface(screen); suppr_grille(g); suppr_joueur(j); suppr_monstre(m); SDL_Quit(); return 0; }