Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include "LevelMAPmatrice.hpp"
  5.  
  6. #define LARGEUR_FENETRE 1360
  7. #define HAUTEUR_FENETRE 800
  8. #define NB_CASE_LAGEUR 32
  9. #define NB_CASE_HAUTEUR 18
  10. #define TAILLE_CASE 40
  11. #define NOMBRE_TEXTURE 15
  12. #define PAS 5
  13.  
  14. using namespace sf ;
  15.  
  16. typedef struct
  17. {
  18. int x, y ;
  19. } Point ;
  20. typedef struct
  21. {
  22. int l, h ;
  23. } Size ;
  24. typedef struct
  25. {
  26. int r, g, b ;
  27. } CodeRGB ;
  28. typedef struct
  29. {
  30. char name[20] ;
  31. Size t /* t = taille */;
  32. Point p /* p = position */;
  33. CodeRGB couleur ;
  34. } Button ;
  35.  
  36. void importTexture(Texture tab[NOMBRE_TEXTURE]) ;
  37. void level ( int numeroLvl, Texture t[NOMBRE_TEXTURE]) ;
  38. void afficheCaseLvl ( RenderWindow * fenetre, int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR], Texture t[NOMBRE_TEXTURE] ) ;
  39. void CopyMatrice(int m1[NB_CASE_HAUTEUR][NB_CASE_LAGEUR], int m2[NB_CASE_HAUTEUR][NB_CASE_LAGEUR] ) ;
  40. void afficheObjet (RenderWindow * fenetre, Point p, Texture t[NOMBRE_TEXTURE] ) ;
  41.  
  42.  
  43.  
  44. void Viseur(Point * PosVis, Event evenement);
  45. void Deplacement(Point * PosPla, Event evenement);
  46. void affichageViseur(RenderWindow * fenetre, Point PosVis, Texture t[]);
  47. void affichagePlayer(RenderWindow * fenetre, Point PosPla, Texture t[]);
  48. void tombe(int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR],Point * PosPla);
  49. void spawn(int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR],Point * PosPla );
  50. void mur(int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR],Point * PosPla);
  51.  
  52.  
  53.  
  54.  
  55. int main()
  56. {
  57. int niv ;
  58. Texture texture[NOMBRE_TEXTURE] ;
  59. importTexture(texture);
  60. printf("Choisissez un level : ");
  61. scanf("%i", &niv);
  62. level(niv, texture) ;
  63. return 0;
  64. }
  65.  
  66. void level(int numeroLvl, Texture t[NOMBRE_TEXTURE])
  67. {
  68. Point posObj= {500,160}, posVis = {0,0}, posPla ;
  69. Sprite fond ;
  70. fond.setTexture(t[8]);
  71. char nomLvl[10] ;
  72. int matrice[NB_CASE_HAUTEUR][NB_CASE_LAGEUR];
  73.  
  74. {
  75. /* Selection du niveau selon le chiffre */
  76. sprintf(nomLvl, "Niveau %i", numeroLvl) ;
  77. switch (numeroLvl)
  78. {
  79. case 1 :
  80. CopyMatrice(matrice,LEVELTUTO1) ;
  81. break ;
  82. case 2 :
  83. CopyMatrice(matrice,LEVELTUTO2) ;
  84. break ;
  85. case 3 :
  86. CopyMatrice(matrice,LEVEL3) ;
  87. break ;
  88. case 4 :
  89. CopyMatrice(matrice,LEVEL4) ;
  90. break ;
  91. case 5 :
  92. CopyMatrice(matrice,LEVEL5) ;
  93. break ;
  94. case 6 :
  95. CopyMatrice(matrice,LEVEL6) ;
  96. break ;
  97. case 7 :
  98. CopyMatrice(matrice,LEVEL7) ;
  99. break ;
  100. }
  101. /*fin de selection*/
  102. }
  103. spawn(matrice,&posPla );
  104. {
  105. /* Fenetre du niveau */
  106. RenderWindow app(VideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE), nomLvl);
  107. app.setMouseCursorVisible(false);
  108.  
  109. while (app.isOpen())
  110. {
  111. Event event;
  112. while (app.pollEvent(event))
  113. {
  114. if (event.type == Event::Closed)
  115. app.close();
  116. Deplacement(&posPla, event);
  117. Viseur(&posVis, event);
  118. }
  119. mur(matrice,&posPla);
  120. tombe(matrice,&posPla);
  121. tombe(matrice,&posObj);
  122.  
  123. app.clear();
  124. app.draw(fond);
  125. afficheCaseLvl( &app, matrice, t );
  126.  
  127. /* C'EST ICI QU'IL FAUT RAJOUTER VOS FONCTIONS QUI AFFICHENT DE NOUVEAU SPRITE SUR LA FENETRE /!\ NE RIEN MODIFIER AILLEURS !!! */
  128.  
  129. afficheObjet(&app,posObj,t);
  130. affichagePlayer(&app,posPla,t);
  131. affichageViseur(&app,posVis,t);
  132.  
  133. /* fin */
  134.  
  135. app.display();
  136. }
  137. /*fin fenetre*/
  138. }
  139. }
  140.  
  141.  
  142.  
  143. /* PAS TOUCHE A MES FONCTIONS S'IL VOUS PLAIT */
  144.  
  145. // Fonction qui permet d'afficher chaque case en fonction de son nombre dans la matrice
  146. void afficheCaseLvl ( RenderWindow * fenetre, int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR], Texture t[NOMBRE_TEXTURE] )
  147. {
  148. int i, j, nb ;
  149. for (i=0 ; i<NB_CASE_HAUTEUR; i++)
  150. {
  151. for (j=0 ; j<NB_CASE_LAGEUR ; j++)
  152. {
  153. Sprite carre ;
  154. carre.setPosition((j+1)*TAILLE_CASE,(i+1)*TAILLE_CASE) ;
  155. switch ( matCases[i][j] )
  156. {
  157. case 0 :
  158. carre.setTexture(t[0]);
  159. break ;
  160. case 1 :
  161. carre.setTexture(t[1]);
  162. break ;
  163. case 2 :
  164. carre.setTexture(t[2]);
  165. break ;
  166. case 3 :
  167. carre.setTexture(t[3]);
  168. break ;
  169. case 4 :
  170. carre.setTexture(t[4]);
  171. break ;
  172. case 5 :
  173. carre.setTexture(t[5]);
  174. break ;
  175. case 6 :
  176. carre.setTexture(t[6]);
  177. break ;
  178. case 7 :
  179. carre.setTexture(t[7]);
  180. break ;
  181. case 9 :
  182. carre.setTexture(t[9]);
  183. break ;
  184. }
  185.  
  186. fenetre->draw(carre);
  187. }
  188. }
  189. }
  190.  
  191. /* Liste des def de blocs
  192. 0=bloc null
  193. 1=bloc sans portail
  194. 2=bloc portail
  195. 3=kill
  196. 4=bloc reset portail
  197. 5=bouton
  198. 6=arrivee
  199. 7=spawn
  200. 9=cubeLave
  201. */
  202.  
  203. // Fonction qui permet de copier une matrice dans une autre
  204. void CopyMatrice(int m1[NB_CASE_HAUTEUR][NB_CASE_LAGEUR], int m2[NB_CASE_HAUTEUR][NB_CASE_LAGEUR] )
  205. {
  206. int i, j ;
  207. for (i=0 ; i<NB_CASE_HAUTEUR ; i++)
  208. {
  209. for (j=0 ; j<NB_CASE_LAGEUR ; j++)
  210. m1[i][j]=m2[i][j];
  211. }
  212. }
  213.  
  214. void afficheObjet (RenderWindow * fenetre, Point p, Texture t[NOMBRE_TEXTURE] )
  215. {
  216. Sprite cube ;
  217. cube.setTexture(t[14]) ;
  218. cube.setOrigin(0,TAILLE_CASE-10);
  219. cube.setPosition(p.x,p.y);
  220. fenetre->draw(cube);
  221. }
  222.  
  223. void importTexture(Texture tab[NOMBRE_TEXTURE])
  224. {
  225. tab[0].loadFromFile("vide.png");
  226. tab[1].loadFromFile("blockNon.png");
  227. tab[2].loadFromFile("blockOui.png");
  228. tab[3].loadFromFile("deathBlock.png");
  229. tab[4].loadFromFile("barrier.png");
  230. tab[5].loadFromFile("bouton.png");
  231. tab[6].loadFromFile("fin.png");
  232. tab[7].loadFromFile("spawn.png");
  233. tab[8].loadFromFile("bordure.png");
  234. tab[9].loadFromFile("lave.png");
  235. tab[10].loadFromFile("curseur.png");
  236. tab[11].loadFromFile("player.png");
  237. tab[12].loadFromFile("TirV.png");
  238. tab[13].loadFromFile("TirR.png");
  239. tab[14].loadFromFile("cube.png");
  240. }
  241.  
  242. void Viseur(Point * PosVis, Event evenement)
  243. {
  244. if (evenement.type == Event::MouseMoved)
  245. {
  246. PosVis->x=evenement.mouseMove.x;
  247. PosVis->y=evenement.mouseMove.y;
  248. }
  249. }
  250.  
  251. void Deplacement(Point * PosPla, Event evenement)
  252. {
  253. if (evenement.key.code == Keyboard::Left)
  254. PosPla->x-=PAS;
  255. if (evenement.key.code == Keyboard::Right)
  256. PosPla->x+=PAS;
  257. }
  258.  
  259. void affichageViseur(RenderWindow * fenetre, Point PosVis, Texture t[])
  260. {
  261. Sprite viseur;
  262. viseur.setTexture(t[10]);
  263. viseur.setOrigin(Vector2f(TAILLE_CASE/2,TAILLE_CASE/2));
  264. viseur.setPosition(PosVis.x,PosVis.y);
  265. fenetre->draw(viseur);
  266. }
  267. void affichagePlayer(RenderWindow * fenetre, Point PosPla, Texture t[])
  268. {
  269. Sprite player;
  270. player.setTexture(t[11]);
  271. player.setOrigin(0,TAILLE_CASE*2);
  272. player.setPosition(PosPla.x,PosPla.y);
  273. fenetre->draw(player);
  274. }
  275.  
  276. void tombe(int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR],Point * PosPla)
  277. {
  278. Point positionMap ;
  279. positionMap.x = PosPla->x/40;
  280. positionMap.y = PosPla->y/40;
  281. if ((matCases[positionMap.y-1][positionMap.x] == 0 && matCases[positionMap.y-1][positionMap.x-1] == 0) || (matCases[positionMap.y-1][positionMap.x-1]==7))
  282. PosPla->y++;
  283. }
  284.  
  285. void spawn(int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR],Point * PosPla )
  286. {
  287. int i, j ;
  288. for (i=0 ; i<NB_CASE_HAUTEUR; i++)
  289. {
  290. for (j=0 ; j<NB_CASE_LAGEUR ; j++)
  291. {
  292. if ( matCases[i][j]==7)
  293. {
  294. PosPla->x=((j+1)*40)+20;
  295. PosPla->y=((i+1)*40);
  296. }
  297. }
  298. }
  299.  
  300. }
  301.  
  302. //COLLISIONS MUR
  303. void mur(int matCases[NB_CASE_HAUTEUR][NB_CASE_LAGEUR],Point * PosPla)
  304. {
  305. Point positionMap ;
  306. positionMap.x = PosPla->x/40;
  307. positionMap.y = PosPla->y/40;
  308.  
  309. if ((matCases[positionMap.y-2][positionMap.x-1] != 0) && (matCases[positionMap.y-2][positionMap.x-1] != 7) && (matCases[positionMap.y-2][positionMap.x-1] != 5))
  310. PosPla->x++;
  311. if ((matCases[positionMap.y-2][positionMap.x] != 0) && (matCases[positionMap.y-2][positionMap.x] != 7) && (matCases[positionMap.y-2][positionMap.x] != 5))
  312. PosPla->x--;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement