Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5.  
  6. #define HAUT 22 //20+2 pour les bordures
  7. #define LARG 52 //50+2 pour les bordures
  8.  
  9.  
  10. typedef struct // définition de la structure des fantomes
  11. {
  12. int fantlig;
  13. int fantcol;
  14. int fantdeplig;
  15. int fantdepcol;
  16.  
  17. } t_fant;
  18.  
  19. typedef struct // définition de la structure position du pacman
  20. {
  21. int poslig;
  22. int poscol;
  23. } t_pos;
  24.  
  25. void hidecursor() //fonction cachant le curseur (TROUVE SUR INTERNET)
  26. {
  27. HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  28. CONSOLE_CURSOR_INFO info;
  29. info.dwSize = 100;
  30. info.bVisible = FALSE;
  31. SetConsoleCursorInfo(consoleHandle, &info);
  32. }
  33.  
  34. void color(int t,int f) //Pour mettre des couleurs dans la console (trouvée sur internet)
  35. {
  36. HANDLE H=GetStdHandle(STD_OUTPUT_HANDLE);
  37. SetConsoleTextAttribute(H,f*16+t);
  38. }
  39.  
  40. void gotoligcol( int lig, int col )
  41. {
  42. // ressources
  43. COORD mycoord;
  44.  
  45. mycoord.X = col;
  46. mycoord.Y = lig;
  47. SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), mycoord );
  48. }
  49.  
  50. void affiche (char mat[HAUT][LARG]) //Programme pour afficher une matrice
  51. {
  52. //Déclaration des variables
  53. int i, j ; //Compteurs dans boucles for
  54.  
  55. //Code
  56. for (i=0 ; i<HAUT ; i++) //On parcourt les lignes
  57. {
  58. for (j=0 ; j<LARG ; j++) //On parcourt les colonnes
  59. {
  60. if (mat[i][j] == 'D')
  61. {
  62. color(9,0) ;
  63. printf("D") ;
  64. color(15,0) ;
  65. }
  66. else
  67. printf("%c", mat[i][j]) ; //On affiche tous les éléments de la matrice
  68. }
  69. printf("\n"); //Après chaque ligne faite, on retourne à la ligne
  70. }
  71. }
  72.  
  73. void depfantome (t_fant *fantome,t_pos *position,char mat[HAUT][LARG]) //Déplacement des fantômes guidés
  74. {
  75.  
  76. int deplacement_fantome;
  77.  
  78. //Déplacement du fantome
  79. gotoligcol(fantome->fantlig,fantome->fantcol);//on place le curseur sur la position du fantome
  80. if (mat[fantome->fantlig][fantome->fantcol]=='D')
  81. {
  82. mat[fantome->fantlig][fantome->fantcol]='D' ;
  83. color(9,0) ;
  84. printf("D") ;
  85. color(15,0) ;
  86. }
  87. else
  88. {
  89. mat[fantome->fantlig][fantome->fantcol]=' ' ;
  90. printf(" ");
  91. }
  92.  
  93. fantome->fantcol+=fantome->fantdepcol, fantome->fantlig+=fantome->fantdeplig ; //En fonction du déplacement, on modifie la position du fantome
  94.  
  95.  
  96. if (fantome->fantcol >= LARG-1) //Si le fantome dépasse la dernière colonne, il revient à la première
  97. {
  98. fantome->fantcol=1 ;
  99. }
  100. if (fantome->fantlig >= HAUT-1) //Si le fantome dépasse la dernière ligne, il revient à la première
  101. {
  102. fantome->fantlig=1 ;
  103. }
  104. if (fantome->fantcol < 1) //Si le fantome passe derrière la première colonne, il revient à la dernière
  105. {
  106. fantome->fantcol=LARG-2 ;
  107. }
  108. if (fantome->fantlig < 1) //Si le fantome passe derrière la première ligne, il revient à la dernière
  109. {
  110. fantome->fantlig=HAUT-2 ;
  111. }
  112.  
  113. if (mat[fantome->fantlig][fantome->fantcol]=='D')
  114. {
  115. mat[fantome->fantlig][fantome->fantcol]='D' ;
  116. }
  117. else
  118. {
  119. mat[fantome->fantlig][fantome->fantcol]='F' ;
  120. }
  121. gotoligcol(fantome->fantlig,fantome->fantcol);//on place le curseur sur la nouvelle position du fantome
  122. color(12,0) ;
  123. printf("%c",mat[fantome->fantlig][fantome->fantcol]); //on affiche le fantome sur sa nouvelle position
  124. color(15,0) ;
  125.  
  126.  
  127. //déplacement fantome pour pousuivre le pacman
  128.  
  129. if (fantome->fantcol>position->poscol)
  130. deplacement_fantome=3;
  131. if (fantome->fantcol<position->poscol)
  132. deplacement_fantome=4;
  133. if (fantome->fantlig>position->poslig)
  134. deplacement_fantome=1;
  135. if (fantome->fantlig<position->poslig)
  136. deplacement_fantome=2;
  137.  
  138.  
  139.  
  140.  
  141. switch (deplacement_fantome)
  142. {
  143. //Il monte
  144. case 1 :
  145. fantome->fantdeplig = -1 ;
  146. fantome->fantdepcol = 0 ;
  147. break ;
  148. //Il descend
  149. case 2 :
  150. fantome->fantdeplig = 1 ;
  151. fantome->fantdepcol = 0 ;
  152. break ;
  153. //Il va à gauche
  154. case 3 :
  155. fantome->fantdeplig = 0 ;
  156. fantome->fantdepcol = -1 ;
  157. break ;
  158. //Il va à droite
  159. case 4 :
  160. fantome->fantdeplig = 0 ;
  161. fantome->fantdepcol = 1 ;
  162. break ;
  163. }
  164.  
  165. // pour gérer les murs au milieu de la map
  166. if ((mat[fantome->fantlig+fantome->fantdeplig][fantome->fantcol+fantome->fantdepcol]==(char)186) || (mat[fantome->fantlig+fantome->fantdeplig][fantome->fantcol+fantome->fantdepcol]==(char)205))
  167. {
  168. switch (deplacement_fantome)
  169. {
  170. //Il monte
  171. case 1 :
  172. fantome->fantdeplig = 1 ;
  173. fantome->fantdepcol = 0 ;
  174. break ;
  175. //Il descend
  176. case 2 :
  177. fantome->fantdeplig = -1 ;
  178. fantome->fantdepcol = 0 ;
  179. break ;
  180. //Il va à gauche
  181. case 3 :
  182. fantome->fantdeplig = 0 ;
  183. fantome->fantdepcol = 1 ;
  184. break ;
  185. //Il va à droite
  186. case 4 :
  187. fantome->fantdeplig = 0 ;
  188. fantome->fantdepcol = -1 ;
  189. break ;
  190. }
  191. }
  192.  
  193. }
  194.  
  195. void depfantomealea (t_fant *fantome,t_pos *position,char mat[HAUT][LARG], int* dep_lineaire, int bordure) //Déplacement des fantômes aléatoires
  196. {
  197.  
  198. //Déclaration des variables
  199. int deplacement_fantome_alea ;
  200. int rand1 ;
  201.  
  202.  
  203. //Déplacement du fantome
  204. gotoligcol(fantome->fantlig,fantome->fantcol);//on place le curseur sur la position du fantome
  205. if (mat[fantome->fantlig][fantome->fantcol]=='D')
  206. {
  207. mat[fantome->fantlig][fantome->fantcol]='D' ;
  208. color(9,0) ;
  209. printf("D") ;
  210. color(15,0) ;
  211. }
  212. else
  213. {
  214. mat[fantome->fantlig][fantome->fantcol]=' ' ;
  215. printf(" ");
  216. }
  217.  
  218.  
  219. fantome->fantcol+=fantome->fantdepcol, fantome->fantlig+=fantome->fantdeplig ; //En fonction du déplacement, on modifie la position du fantome
  220.  
  221. //Quand le PACMAN rencontre un mur sans bordure
  222. if (fantome->fantcol >= LARG-1) //Si le fantome dépasse la dernière colonne, il revient à la première
  223. {
  224. fantome->fantcol=1 ;
  225. }
  226. if (fantome->fantlig >= HAUT-1) //Si le fantome dépasse la dernière ligne, il revient à la première
  227. {
  228. fantome->fantlig=1 ;
  229. }
  230. if (fantome->fantcol < 1) //Si le fantome passe derrière la première colonne, il revient à la dernière
  231. {
  232. fantome->fantcol=LARG-3 ;
  233. }
  234. if (fantome->fantlig < 1) //Si le fantome passe derrière la première ligne, il revient à la dernière
  235. {
  236. fantome->fantlig=HAUT-2 ;
  237. }
  238.  
  239. // Cas ou les bordures sont activé
  240. if (bordure == 1)
  241. {
  242.  
  243. if ((fantome->fantcol+fantome->fantdepcol) >= LARG-1) //Si le PACMAN arrive a la dernière colonne, il rebondit
  244. {
  245. fantome->fantdepcol = -1 ;
  246. }
  247. if (fantome->fantlig+fantome->fantdeplig >= HAUT-1) //Si le PACMAN dépasse la dernière ligne, il rebondit
  248. {
  249. fantome->fantdeplig = -1 ;
  250. }
  251. if (fantome->fantcol+fantome->fantdepcol < 1) //Si le PACMAN passe derrière la première colonne, il rebondit
  252. {
  253. fantome->fantdepcol = 1;
  254. }
  255. if (fantome->fantlig+fantome->fantdeplig < 1) //Si le PACMAN passe derrière la première ligne, il rebondit
  256. {
  257. fantome->fantdeplig = 1;
  258. }
  259. }
  260.  
  261.  
  262.  
  263. if (mat[fantome->fantlig][fantome->fantcol]=='D')
  264. {
  265. mat[fantome->fantlig][fantome->fantcol]='D' ;
  266. }
  267. else
  268. {
  269. mat[fantome->fantlig][fantome->fantcol]='F' ;
  270. }
  271. gotoligcol(fantome->fantlig,fantome->fantcol);//on place le curseur sur la nouvelle position du fantome
  272. color(12,0) ;
  273. printf("%c",mat[fantome->fantlig][fantome->fantcol]); //on affiche le fantome sur sa nouvelle position
  274. color(15,0) ;
  275.  
  276.  
  277. //déplacement fantome aléatoire
  278.  
  279. switch (*dep_lineaire)
  280. {
  281. //Il monte
  282. case 1 :
  283. fantome->fantdeplig = -1 ;
  284. fantome->fantdepcol = 0 ;
  285. break ;
  286. //Il descend
  287. case 2 :
  288. fantome->fantdeplig = 1 ;
  289. fantome->fantdepcol = 0 ;
  290. break ;
  291. //Il va à gauche
  292. case 3 :
  293. fantome->fantdeplig = 0 ;
  294. fantome->fantdepcol = -1 ;
  295. break ;
  296. //Il va à droite
  297. case 4 :
  298. fantome->fantdeplig = 0 ;
  299. fantome->fantdepcol = 1 ;
  300. break ;
  301. }
  302.  
  303.  
  304. // pour gérer les murs au milieu de la map
  305. if ((mat[fantome->fantlig+fantome->fantdeplig][fantome->fantcol+fantome->fantdepcol]==(char)186) || (mat[fantome->fantlig+fantome->fantdeplig][fantome->fantcol+fantome->fantdepcol]==(char)205))
  306. {
  307. switch (*dep_lineaire)
  308. {
  309. //Il monte
  310. case 1 :
  311. fantome->fantdeplig = 1 ;
  312. fantome->fantdepcol = 0 ;
  313. break ;
  314. //Il descend
  315. case 2 :
  316. fantome->fantdeplig = -1 ;
  317. fantome->fantdepcol = 0 ;
  318. break ;
  319. //Il va à gauche
  320. case 3 :
  321. fantome->fantdeplig = 0 ;
  322. fantome->fantdepcol = 1 ;
  323. break ;
  324. //Il va à droite
  325. case 4 :
  326. fantome->fantdeplig = 0 ;
  327. fantome->fantdepcol = -1 ;
  328. break ;
  329. }
  330. }
  331.  
  332. }
  333.  
  334. void sauvegarde (int derniere_val_aj,int num_tableau) //procédure de sauvegarde du jeux
  335. {
  336. //Declaration des variables
  337. int val;
  338. char mot[21];
  339. FILE* fic=NULL;
  340.  
  341. //Création (ou ouverture) du fichier de sauvegarde
  342. fic=fopen("sauvegarde.txt","a");
  343. if (fic) //test si le fichier abien été ouvert
  344. {
  345. printf("Saisissez le nom de votre sauvegarde \(20 caratere maximum et sans espace\) et appuyez sur entree:\n");
  346. fflush(stdin);
  347. gets(mot);//entrer le nom de la sauvegarde
  348. fprintf(fic,"Partie %d:%s %d\n",derniere_val_aj+1,mot,num_tableau);//écriture de la ligne de sauvegarde dans le fichier
  349. fclose(fic);
  350. }
  351. else //cas ou il y a eu une erreur dans la création du fichier
  352. {
  353. system("cls");
  354. printf("La sauvegarde n'a pas put etre effectue");
  355. }
  356. }
  357.  
  358. void recup_sauvegarde () //Fonction de récupération de la sauvegarde du jeu
  359. {
  360. //déclaration des varaibles
  361. int val;
  362. char mot[21];
  363. int num_tableau;
  364. int valeur;
  365. int ligne;
  366. int i;
  367. int multiple;
  368. FILE* fic=NULL;
  369.  
  370. //ouverture du fichier de sauvegarde
  371. fic=fopen("sauvegarde.txt","r");
  372. ligne=1;
  373. if (fic) //testsi le fichier a bien été ouvert
  374. {
  375. while (!feof(fic))
  376. printf("%c",fgetc(fic)); // Affichage des sauvegardes
  377. printf("\n\n\n Veuillez entrer le numero de la partie que vous souhaitez recuperer:\n");
  378. scanf("%d",&valeur); //Récupération du numéro de la partie que le joueur veut récupérer
  379. printf("\n");
  380. printf("\n");
  381. //on place le curseur au début du document texte
  382. rewind(fic);
  383. //on fait avancer le curseur tant qu'on est pas a la bonne ligne
  384. while(ligne != valeur)
  385. {
  386. val=fgetc(fic);
  387. if (val == '\n')
  388. ligne=ligne +1;
  389. }
  390. //on fait avancer le curseur jusqu'au début du nom de la sauvegarde
  391. while((val=fgetc(fic))!= ':');
  392. //on enregistre le nom de la sauvegarde dans la chaine de caratère mot
  393. strcpy(mot,"\0");
  394. while((val=fgetc(fic))!= ' ')
  395. strcat(mot,(char[])
  396. {
  397. val, '\0'
  398. });
  399.  
  400. //on enregistre le numéro du tableau en cour dans l'entier num_tableau
  401. while((val=fgetc(fic))!= ' ')
  402. num_tableau=val-48;
  403.  
  404.  
  405. system("cls");
  406.  
  407. //sauvegarde nous renvoi
  408. printf("%s\n",mot);
  409. printf("%d\n",num_tableau);
  410.  
  411.  
  412. }
  413. }
  414.  
  415. void menu (int *tempo, int* bordure , int* tableau_lvl ) //Sous programme du menu
  416. {
  417.  
  418. int choix;
  419. int choix2;
  420. int choix3;
  421. int debut;
  422. int i;
  423.  
  424.  
  425.  
  426. debut=0;
  427. choix2='m';
  428.  
  429. hidecursor();
  430.  
  431. while (choix2=='m')
  432. {
  433. printf("\n ===== BIENVENUE SUR PACMAN ===== \n\n");
  434. printf(" MENU DU JEU :\n ");
  435. printf("1: Regles du jeux\n ");
  436. printf("2: Vitesse de depart\n ");
  437. printf("3: Bordure\n ");
  438. printf("4: Demarer une nouvelle partie\n ");
  439. printf("5: Reprendre la partie\n ");
  440. printf("6: Quitter la partie\n\n");
  441. printf("Saisissez votre choix et appuyer sur entree\n");
  442. fflush(stdin);
  443. scanf("%d",&choix);
  444. system("cls");
  445.  
  446. switch (choix)
  447. {
  448. case 1:
  449. printf("\n\n ===REGLE DU JEUX===\n\n");
  450. printf("Le but du jeux est de manger des diamants repartis sur \n4 niveaux de difficultes croissante. Cependant,le pacman \nsera confronte a des obstacles que vous devrez eviter\n\n");
  451. printf(" ");
  452. printf("Voici les touches utiles:\n");
  453. printf(" ");
  454. printf("4: Se deplacer vers la gauche\n");
  455. printf(" ");
  456. printf("8: Se deplacer vers le haut\n");
  457. printf(" ");
  458. printf("6: Se deplacer vers la droite\n");
  459. printf(" ");
  460. printf("2: Se deplacer vers le bas\n");
  461. printf(" ");
  462. printf("p: Mettre le jeu en pause\n");
  463. printf(" ");
  464. printf("q: Retourner au menu\n");
  465. printf("\n\nPour retourner au menu, appuyer sur \"m\" et entree:\n");
  466. fflush(stdin);
  467. scanf("%c",&choix2);
  468. system("cls");
  469. break;
  470.  
  471. case 2:
  472. printf("\n\n ===REGLER LA VITESSE DE JEU===\n\n ");
  473. printf("1: Pacman lent\n ");
  474. printf("2: Pacman a vitesse moyenne\n ");
  475. printf("3: Pacman rapide\n\n ");
  476. printf("Choisir la vitesse de votre Pacman et appuye sur entree:\n");
  477. scanf("%d",&choix3);
  478. system("cls");
  479. switch(choix3)
  480. {
  481. case 1:
  482. printf("\n\n ****Vous avez choisi un Pacman lent****\n\n");
  483. (*tempo) = (*tempo)*2;
  484. break;
  485. case 2:
  486. printf("\n\n ****Vous avez choisi un Pacman a vitesse moyenne****\n\n");
  487. break;
  488. case 3:
  489. printf("\n\n ****Vous avez choisi un Pacman rapide****\n\n");
  490. (*tempo) = (*tempo)/2;
  491. break;
  492. default:
  493. printf(" ===Votre saisi est errone===");
  494. }
  495. printf("\n\nPour retourner au menu, appuyer sur \"m\" et entree:\n");
  496. fflush(stdin);
  497. scanf("%c",&choix2);
  498. system("cls");
  499. break;
  500. case 3:
  501. printf("\n\n ===REGLER LA BORDURE DU JEU===\n\n ");
  502. printf("1: ON\n ");
  503. printf("2: OFF\n\n ");
  504. printf("Choisir si vous souhaitez des bordures ou non et appuyez sur entree:\n");
  505. scanf("%d",&choix3);
  506. system("cls");
  507. switch(choix3)
  508. {
  509. case 1:
  510. printf("\n\n ****Vous avez choisi d'avoir des bordures****\n\n");
  511. (*bordure) = 1;
  512. break;
  513. case 2:
  514. printf("\n\n ****Vous avez choisi de ne pas avoir de bordures****\n\n");
  515. (*bordure) = 0;
  516. break;
  517. default:
  518. printf(" ===Votre saisi est errone===");
  519. }
  520. printf("\n\nPour retourner au menu, appuyer sur \"m\" et entree:\n");
  521. fflush(stdin);
  522. scanf("%c",&choix2);
  523. system("cls");
  524. break;
  525. case 4:
  526. printf("\n\n ****Vous avez choisi de commencer une nouvelle partie****\n\n");
  527. (*tableau_lvl) = 1 ;
  528. debut=1;
  529. choix2='n';
  530. for (i=0; i<1000000000; i++)
  531. {
  532. }
  533. system("cls");
  534. break;
  535. case 5:
  536. printf("\n\n ****Vous avez choisi de reprendre votre partie sauvegarde****\n\n");
  537. system("cls");
  538. recup_sauvegarde();
  539. debut=1;
  540. choix2='n';
  541. break;
  542. case 6:
  543. printf("\n\n ****Vous avez choisi de quitter le jeu****\n\n");
  544. sauvegarde(1,1);
  545. system("cls");
  546. gotoligcol(5, 10) ;
  547. printf("%c%c%c%c%c%c%c%c%c%c%c%c%c", 201, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 187) ;
  548. gotoligcol(6, 10) ;
  549. printf("%c", 186) ;
  550. printf(" AU REVOIR ") ;
  551. printf("%c", 186) ;
  552. gotoligcol(7, 10) ;
  553. printf("%c%c%c%c%c%c%c%c%c%c%c%c%c", 200, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 188) ;
  554. choix2='n';
  555. debut=1;
  556. printf("\n\n\n\n\n");
  557. break;
  558. default:
  559. printf("\n===Saisi erone===\n");
  560. }
  561. }
  562. if (debut==0)
  563. printf("\n===Saisi errone, relancez le jeu===\n\n");
  564.  
  565. }
  566.  
  567. void tableau ( int nb_objets,int *vie, int NB_FANT, int *score, int nb_fant_alea, int nb_fant_guid, int *dep_lineaire, int tempo, int bordure, int *tableau_lvl ) //Sous-programme qui affcihe chaque tableau
  568. {
  569. hidecursor();
  570.  
  571. //Déclaration des variables
  572. char mat[HAUT][LARG] ;
  573.  
  574. int deplig, depcol ; //deplig & depcol correspondent aux déplacements lignes/colonnes du PACMAN
  575.  
  576. int i, j ; //Compteurs dans boucles for
  577. int rand1, rand2 ; //Deux nombres aléatoires
  578. char touche ; //Variable contenant une touche tapée par l'utilisateur
  579. int coord_objet[2][nb_objets] ; //Tableau contenant les coordonnées des objets
  580. int bordure_v, bordure_h, coin_sg, coin_sd, coin_ig, coin_id ; //Bordure verticale/horizontale Coin inférieur/supérieur droit/gauche
  581. int boucle ; //Variable de la boucle du jeu
  582. boucle = 1 ; //Vraie au début du jeu
  583. int d; //Diviseur de tempo
  584. d=1;
  585. int temps_acceleration; //Durée de l'accélèration quand on a mangé un objet
  586. temps_acceleration=0;
  587. int score_tab ; //Score du tableau
  588. score_tab = 0 ;
  589.  
  590. t_fant fantomas[NB_FANT];
  591. t_pos position;
  592.  
  593. int t;
  594. int compteur;
  595. compteur=0;
  596.  
  597. //initialisation des bordures
  598. if (bordure == 1)
  599. {
  600. bordure_h = 205, bordure_v = 186, coin_sg = 201, coin_sd = 187, coin_ig = 200, coin_id = 188 ; //J'assigne à chaque bordure sa valeur decimale ASCII
  601. }
  602. else
  603. {
  604. bordure_h = 46, bordure_v = 46, coin_sg = 46, coin_sd = 46, coin_ig = 46, coin_id = 46 ; //J'assigne à chaque bordure sa valeur decimale ASCII
  605. }
  606.  
  607.  
  608. //Initialisation du jeu
  609. rand1 = rand()%(HAUT-1 - 1) + 1 ; //Ici la borne inf efantomas[i].fantligst 1 et la borne sup est HAUT-1 (car on est sur les indices)
  610. rand2 = rand()%(LARG-1 - 1) + 1 ; //Ici la borne inf est 1 et la borne sup est LARG-1 (car on est sur les indices)
  611.  
  612. position.poslig=rand1, position.poscol=rand2 ; //Par défaut, le PACMAN commence dans le coin supérieur gauche
  613. deplig=0, depcol=0 ; //Par défaut, le PACMAN est immobile
  614.  
  615. //Spawn fantômes
  616. for (i=0; i<NB_FANT; i++)
  617. {
  618. rand1 = rand()%(HAUT-1 - 2) + 2 ; //Ici la borne inf est 2 et la borne sup est HAUT-1 (car on est sur les indices)
  619. rand2 = rand()%(LARG-1 - 2) + 2 ; //Ici la borne inf est 2 et la borne sup est LARG-1 (car on est sur les indices)
  620. fantomas[i].fantdeplig=0, fantomas[i].fantdepcol=0; //Par défaut, le fantome est immmobile
  621. while(mat[rand1][rand2] == 'X' || mat[rand1][rand2] == (char)205 || mat[rand1][rand2] == (char)186 ) //Vérification afin d'éviter d'avoir des fantomes sur le PACMAN & les murs
  622. {
  623. rand1 = rand()%(HAUT-1 - 1) + 1 ;
  624. rand2 = rand()%(LARG-1 - 1) + 1 ;
  625. coord_objet[0][i] = rand1 ;
  626. coord_objet[1][i] = rand2 ;
  627. }
  628. fantomas[i].fantlig=rand1, fantomas[i].fantcol=rand2;//Par défaut, le fantome commence à une position aléatoire
  629. }
  630.  
  631. //Remplissage de la bordure
  632. for (j=1 ; j<LARG-1 ; j++)
  633. mat[0][j] = (char)bordure_h ; //Bordure verticale supérieure
  634. for (j=1 ; j<LARG-1 ; j++)
  635. mat[HAUT-1][j] = (char)bordure_h ; //Bordure verticale inférieure
  636. for (i=1 ; i<HAUT-1 ; i++)
  637. mat[i][0] = (char)bordure_v ; //Bordure horizontale gauche
  638. for (i=1 ; i<HAUT-1 ; i++)
  639. mat[i][LARG-1] = (char)bordure_v ; //Bordure horizontale droite
  640. mat[0][0] = (char)coin_sg ; //Coin supérieur gauche
  641. mat[0][LARG-1] = (char)coin_sd ; //Coin supérieur droit
  642. mat[HAUT-1][0] = (char)coin_ig ; //Coin inférieur gauche
  643. mat[HAUT-1][LARG-1] = (char)coin_id ; //Coin inférieur droit
  644.  
  645.  
  646. //Remplissage de l'air de jeu
  647. for (i=1 ; i<HAUT-1 ; i++)
  648. {
  649. for (j=1 ; j<LARG-1 ; j++)
  650. {
  651.  
  652. if ((*tableau_lvl) == 3) //Dans le tableau 3
  653. {
  654. if (j == 10 && i>=6 && i<=14)
  655. mat[i][j]=186 ; //On place les murs
  656. else if (j == 41 && i>=6 && i<=14)
  657. mat[i][j]=186 ; //On place les murs
  658. else if (i == 10 && j>=16 && j<=35)
  659. mat[i][j]=205 ; //On place les murs
  660. else
  661. mat[i][j]=' ' ; //On remplit le reste de la matrice de cases vides
  662. }
  663.  
  664. else
  665. mat[i][j]=' ' ; //On remplit la matrice de cases vides
  666. }
  667. }
  668.  
  669. //Création des objets à manger
  670. for (i=0 ; i<nb_objets ; i++)
  671. {
  672. //On ne veut pas faire apparaitre d'objets sur la position de X
  673. rand1 = rand()%(HAUT-1 - 2) + 2 ; //Ici la borne inf est 2 et la borne sup est HAUT-1 (car on est sur les indices)
  674. rand2 = rand()%(LARG-1 - 2) + 2 ; //Ici la borne inf est 2 et la borne sup est LARG-1 (car on est sur les indices)
  675. coord_objet[0][i] = rand1 ; //On stocke dans le tableau les coordonnées de chaque objet
  676. coord_objet[1][i] = rand2 ; //On stocke dans le tableau les coordonnées de chaque objet
  677.  
  678. while(mat[rand1][rand2] == 'D' || mat[rand1][rand2] == 'X' || mat[rand1][rand2] == 'F' || mat[rand1][rand2] == (char)205 || mat[rand1][rand2] == (char)186 ) //Vérification afin d'éviter d'avoir 2 pommes au même endroit ou sur le PACMAN ou sur les fantomes
  679. {
  680. rand1 = rand()%(HAUT-1 - 2) + 2 ;
  681. rand2 = rand()%(LARG-1 - 2) + 2 ;
  682. coord_objet[0][i] = rand1 ;
  683. coord_objet[1][i] = rand2 ;
  684. }
  685. color(9,0) ;
  686. mat[rand1][rand2] = 'D' ; //On place aléatoirement nb_objets objets dans l'aire de jeu
  687. color(15,0) ;
  688.  
  689. }
  690.  
  691.  
  692.  
  693. //Aux coordonnées du PACMAN, on remplace par un X
  694. mat[position.poslig][position.poscol]='X' ;
  695.  
  696.  
  697.  
  698. //Affichage de l'écran de jeu
  699. affiche(mat) ;
  700.  
  701.  
  702.  
  703. //Boucle du jeu
  704. while(boucle) //On fait tourner une boucle permanente
  705. {
  706. for (i=0 ; i<(tempo/d) ; i++)
  707. {
  708. //On fait tourner une longue boucle pour que l'utilisateur puisse voir le jeu à une vitesse normale
  709. }
  710.  
  711. //Déplacement du PACMAN
  712. gotoligcol(position.poslig,position.poscol);//on place le curseur sur la position du pacman
  713. mat[position.poslig][position.poscol]=' ' ; //On efface le PACMAN de sa position
  714. printf(" "); //on efface le pacman
  715. position.poscol+=depcol, position.poslig+=deplig ; //En fonction du déplacement, on modifie la position du PACMAN
  716. if (position.poscol >= LARG-1) //Si le PACMAN dépasse la dernière colonne, il revient à la première
  717. {
  718. position.poscol=1 ;
  719. }
  720. if (position.poslig >= HAUT-1) //Si le PACMAN dépasse la dernière ligne, il revient à la première
  721. {
  722. position.poslig=1 ;
  723. }
  724. if (position.poscol < 1) //Si le PACMAN passe derrière la première colonne, il revient à la dernière
  725. {
  726. position.poscol=LARG-2 ;
  727. }
  728. if (position.poslig < 1) //Si le PACMAN passe derrière la première ligne, il revient à la dernière
  729. {
  730. position.poslig=HAUT-2 ;
  731. }
  732.  
  733.  
  734. //Quand on mange un objet
  735. if (mat[position.poslig][position.poscol] == 'D')
  736. {
  737. (*score)+=10 ; //Si les coordonnées du PACMAN correspondent à celles d'un des objets, le score augmente
  738. score_tab+=10 ; //Idem pour le score du tableau
  739. if ((*tableau_lvl) >= 3)
  740. temps_acceleration=60;
  741. }
  742.  
  743. //Comportement entre PACMAN et fantomes
  744. for (i=0 ; i<NB_FANT ; i++)
  745. {
  746. if (fantomas[i].fantcol == position.poscol && fantomas[i].fantlig == position.poslig)
  747. *vie-=1 ;
  748. }
  749.  
  750. mat[position.poslig][position.poscol]='X' ;
  751. gotoligcol(position.poslig,position.poscol);//on place le curseur sur la nouvelle position du pacman
  752. color(14,0) ;
  753. printf("X"); //on affiche le pacman sur sa nouvelle position
  754. color(15,0) ;
  755.  
  756.  
  757.  
  758.  
  759.  
  760. //Pour les fantomes aleatoires
  761. if (temps_acceleration>0)
  762. {
  763. if ((*tableau_lvl) >=3)
  764. {
  765. t=3+compteur;
  766. if ((t%6)==0)
  767. {
  768. for (i=0; i<nb_fant_alea; i++)
  769. {
  770. depfantomealea (&fantomas[i],&position,mat, &dep_lineaire[i],bordure );
  771. }
  772. }
  773. if ((*tableau_lvl) == 2)
  774. compteur+=1;
  775. }
  776. }
  777.  
  778. else if ((*tableau_lvl) >= 3)
  779. {
  780. t=3+compteur ;
  781. if ((t%3)==0)
  782. {
  783. for (i=0 ; i<nb_fant_alea; i++)
  784. {
  785. depfantomealea (&fantomas[i],&position,mat, &dep_lineaire[i],bordure );
  786. }
  787. }
  788. compteur +=1 ;
  789. }
  790.  
  791. else
  792. {
  793. for (i=0; i<nb_fant_alea; i++)
  794. {
  795. depfantomealea (&fantomas[i],&position,mat, &dep_lineaire[i],bordure);
  796. }
  797. }
  798.  
  799. //Pour les fantomes guidés
  800. if (temps_acceleration>0)
  801. {
  802. if ((*tableau_lvl) >=3)
  803. {
  804. t=3+compteur;
  805. if ((t%3)==0)
  806. {
  807. for (i=nb_fant_alea; i<(nb_fant_guid+nb_fant_alea); i++)
  808. {
  809. depfantome (&fantomas[i],&position,mat);
  810. }
  811. }
  812. compteur+=1;
  813. }
  814. }
  815.  
  816. else
  817. {
  818. for (i=nb_fant_alea; i<(nb_fant_guid+nb_fant_alea); i++)
  819. {
  820. depfantome (&fantomas[i],&position,mat);
  821. }
  822. }
  823.  
  824.  
  825. for (i=0; i<NB_FANT; i++)
  826. {
  827. if (fantomas[i].fantlig == position.poslig && fantomas[i].fantcol == position.poscol)
  828. (*vie) = (*vie) - 1 ;
  829. }
  830.  
  831. //Affichage du score
  832. gotoligcol(HAUT+2,0) ;
  833. printf("Score : %d", (*score)) ;
  834.  
  835. //Affichage des vies
  836. gotoligcol(HAUT, 0) ;
  837. printf("Vie(s) : %d", (*vie)) ;
  838.  
  839.  
  840.  
  841. //Action utilisateur
  842. if (kbhit())
  843. touche = getch() ; //Si l'utilisateur tape sur son clavier, alors le caractère tapé se met dans la variable touche
  844.  
  845.  
  846. //Analyse action utilisateur
  847. switch (touche) //En fonction de la touche tapée, on modifie le déplacement du PACMAN
  848.  
  849. {
  850. //Il monte
  851. case '8' :
  852. deplig = -1 ;
  853. depcol = 0 ;
  854. d=1;
  855. gotoligcol(HAUT+1, 0) ;
  856. printf(" ") ;
  857. break ;
  858. //Il descend
  859. case '2' :
  860. deplig = 1 ;
  861. depcol = 0 ;
  862. d=1;
  863. gotoligcol(HAUT+1, 0) ;
  864. printf(" ") ;
  865. break ;
  866. //Il va à gauche
  867. case '4' :
  868. deplig = 0 ;
  869. depcol = -1 ;
  870. d=2;
  871. gotoligcol(HAUT+1, 0) ;
  872. printf(" ") ;
  873. break ;
  874. //Il va à droite
  875. case '6' :
  876. deplig = 0 ;
  877. depcol = 1 ;
  878. d=2;
  879. gotoligcol(HAUT+1, 0) ;
  880. printf(" ") ;
  881. break ;
  882. //Pause
  883. case 'p' :
  884. for (i=0 ; i<NB_FANT ; i++)
  885. fantomas[i].fantdeplig=0, fantomas[i].fantdepcol=0 ;
  886. deplig = 0 ;
  887. depcol = 0 ;
  888. gotoligcol(HAUT+1, 0) ;
  889. printf("APPUYEZ SUR 2,4,6,8 POUR REPARTIR DANS UNE DIRECTION") ;
  890. break ;
  891. //
  892. case 'q' :
  893. //Faire la sauvegarde
  894. (*tableau_lvl) = (*tableau_lvl) - 1 ;
  895. (*vie) = 5 ;
  896. return ;
  897. break ;
  898.  
  899. }
  900.  
  901. // pour gérer les murs au milieu de la map
  902. if ((mat[position.poslig+ deplig][position.poscol+ depcol]==(char)186) || (mat[position.poslig+ deplig][position.poscol+ depcol]==(char)205))
  903. {
  904. switch (touche)
  905. {
  906. //Il monte
  907. case '8' :
  908. deplig = 1 ;
  909. depcol = 0 ;
  910. d=1;
  911. break ;
  912. //Il descend
  913. case '2' :
  914. deplig = -1 ;
  915. depcol = 0 ;
  916. d=1;
  917. break ;
  918. //Il va à gauche
  919. case '4' :
  920. deplig = 0 ;
  921. depcol = 1 ;
  922. d=2;
  923. break ;
  924. //Il va à droite
  925. case '6' :
  926. deplig = 0 ;
  927. depcol = -1 ;
  928. d=2;
  929. break ;
  930. }
  931. }
  932.  
  933. // Cas ou les bordures sont activé
  934. if (bordure == 1)
  935. {
  936.  
  937. if ((position.poscol+depcol) >= LARG-1) //Si le PACMAN arrive a la dernière colonne, il rebondit
  938. {
  939. depcol = -1 ;
  940. (*vie) = (*vie) -1;
  941. touche = '4';
  942. }
  943. if (position.poslig+deplig >= HAUT-1) //Si le PACMAN dépasse la dernière ligne, il rebondit
  944. {
  945. deplig= -1 ;
  946. (*vie) = (*vie) -1;
  947. touche = '8';
  948. }
  949. if (position.poscol+depcol < 1) //Si le PACMAN passe derrière la première colonne, il rebondit
  950. {
  951. depcol = 1;
  952. (*vie) = (*vie) -1;
  953. touche = '6';
  954. }
  955. if (position.poslig+deplig < 1) //Si le PACMAN passe derrière la première ligne, il rebondit
  956. {
  957. deplig = 1;
  958. (*vie) = (*vie) -1;
  959. touche = '2';
  960. }
  961. }
  962.  
  963.  
  964. if(temps_acceleration>0)
  965. {
  966. temps_acceleration = temps_acceleration-1;
  967. d=d*3;
  968. }
  969.  
  970.  
  971. if ((*vie)<=0) //Affichage GAME OVER
  972. {
  973. system("cls") ;
  974. gotoligcol(5, 10) ;
  975. printf("%c%c%c%c%c%c%c%c%c%c%c", 201, 205, 205, 205, 205, 205, 205, 205, 205, 205, 187) ;
  976. gotoligcol(6, 10) ;
  977. printf("%c", 186) ;
  978. printf("GAME OVER") ;
  979. printf("%c", 186) ;
  980. gotoligcol(7, 10) ;
  981. printf("%c%c%c%c%c%c%c%c%c%c%c", 200, 205, 205, 205, 205, 205, 205, 205, 205, 205, 188) ;
  982. gotoligcol(8, 10) ;
  983. printf("Score : %d", *score) ;
  984. gotoligcol(12, 10) ;
  985. break ;
  986. }
  987.  
  988. //Passage d'un tableau à un autre
  989. if (score_tab%50 == 0 && score_tab!=0)
  990. {
  991. boucle = 0 ;
  992. system("cls") ;
  993. score_tab = 0 ;
  994. }
  995. }
  996. }
  997.  
  998. int main()
  999. {
  1000. //Initialisation du générateur aléatoire
  1001. srand(time(NULL)) ;
  1002.  
  1003. //Déclaration des varibales
  1004. int score ;
  1005. int dep_lineaire [4] ;
  1006. int i, j;
  1007. int vie;
  1008. int tempo;
  1009. int bordure;
  1010. int tableau_lvl;
  1011.  
  1012. //Code
  1013.  
  1014. tempo = 80000000;
  1015.  
  1016. hidecursor();
  1017.  
  1018. score = 0;
  1019. vie = 5;
  1020. bordure = 0;
  1021. tableau_lvl = 1 ;
  1022.  
  1023. //Écran d'acceuil
  1024. color(14, 0) ;
  1025. printf("\n\n 8b,dPPYba, ,adPPYYba, ,adPPYba, 88,dPYba,,adPYba, ,adPPYYba, 8b,dPPYba,\n") ;
  1026. printf(" 88P' \"8a \"\" `Y8 a8\" \"\" 88P' \"88\" \"8a \"\" `Y8 88P' `\"8a\n") ;
  1027. printf(" 88 d8 ,adPPPPP88 8b 88 88 88 ,adPPPPP88 88 88 \n") ;
  1028. printf(" 88b, ,a8\" 88, ,88 \"8a, ,aa 88 88 88 88, ,88 88 88\n") ;
  1029. printf(" 88`YbbdP\"' `\"8bbdP\"Y8 `\"Ybbd8\"' 88 88 88 `\"8bbdP\"Y8 88 88\n") ;
  1030. printf(" 88\n") ;
  1031. printf(" 88\n") ;
  1032. color(15, 0) ;
  1033. printf("\n Made by : FABRE Maximilien\n") ;
  1034. printf(" DESCOTTES Martin\n\n") ;
  1035. printf(" Year : 2018\n") ;
  1036. printf(" Referent : RAVAUT Frederic") ;
  1037. for (i=0; i<2000000000; i++)
  1038. {
  1039.  
  1040. }
  1041. system("cls");
  1042.  
  1043. while(1)
  1044. {
  1045.  
  1046. menu(&tempo,&bordure ,&tableau_lvl);
  1047.  
  1048. system("cls");
  1049. gotoligcol(HAUT/2,LARG/2);
  1050. if (tableau_lvl == 1)
  1051. {
  1052. printf ("TABLEAU 1");
  1053. for (i=0; i<1000000000; i++)
  1054. {
  1055.  
  1056. }
  1057. system("cls");
  1058. score=0 ;
  1059.  
  1060. tableau(5,&vie,0,&score, 0, 0,0, tempo, bordure, &tableau_lvl) ;
  1061.  
  1062. tableau_lvl+=1 ;
  1063. }
  1064.  
  1065.  
  1066. for (i=0; i<4; i++)
  1067. {
  1068. dep_lineaire[i] = rand()%(5-1) + 1 ;
  1069. }
  1070.  
  1071. system("cls");
  1072. gotoligcol(HAUT/2,LARG/2);
  1073. if (tableau_lvl == 2)
  1074. {
  1075. printf ("TABLEAU 2");
  1076. for (i=0; i<1000000000; i++)
  1077. {
  1078.  
  1079. }
  1080. system("cls");
  1081. score = 50 ;
  1082.  
  1083. tableau(5,&vie, 4,&score, 4, 0, &dep_lineaire, tempo, bordure, &tableau_lvl) ;
  1084.  
  1085. tableau_lvl+=1 ;
  1086. }
  1087.  
  1088.  
  1089. if (vie>0)
  1090. {
  1091. for (i=0; i<4; i++)
  1092. {
  1093. dep_lineaire[i] = rand()%(5-1) + 1 ;
  1094. }
  1095.  
  1096. system("cls");
  1097. gotoligcol(HAUT/2,LARG/2);
  1098. if (tableau_lvl == 3)
  1099. {
  1100. printf ("TABLEAU 3");
  1101. for (i=0; i<1000000000; i++)
  1102. {
  1103.  
  1104. }
  1105. system("cls");
  1106. score = 100 ;
  1107.  
  1108. tableau(5,&vie, 4,&score, 2, 2, &dep_lineaire, tempo, bordure, &tableau_lvl) ;
  1109.  
  1110. tableau_lvl+=1 ;
  1111. }
  1112.  
  1113. }
  1114. system("cls");
  1115. }
  1116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement