Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. /*
  2. ** EPITECH PROJECT, 2019
  3. ** guitarhero
  4. ** File description:
  5. ** main
  6. */
  7.  
  8. #include <unistd.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include "SDLDisplay.hpp"
  13. #include "Input.hpp"
  14. #include "Colors.hpp" // ici les couleurs
  15. #include "RNG.hpp"
  16. bool RNG::_initialized = false;
  17.  
  18. #define LIGNE_TEST_CLIC 750
  19.  
  20. using namespace std;
  21.  
  22. enum Collone
  23. {
  24. COLLONE_1,
  25. COLLONE_2,
  26. COLLONE_3,
  27. COLLONE_4,
  28. COLLONE_5,
  29. COLLONE_6
  30. };
  31.  
  32. class FlashRoug
  33. {
  34. public:
  35.  
  36. FlashRoug(){}
  37.  
  38. static void play()
  39. {
  40. tempsLast = SDL_GetTicks();
  41. }
  42.  
  43. static void affiche(SDLDisplay & a)
  44. {
  45. if(SDL_GetTicks() - tempsLast < 50)
  46. {
  47. a.putRect(0, 0, 900, 900, Colors::Red);
  48. }
  49. }
  50.  
  51. private:
  52.  
  53. const int duree = 100;
  54. static int tempsLast;
  55. };
  56.  
  57. int FlashRoug::tempsLast = 0;
  58.  
  59. class Carre
  60. {
  61. public:
  62. Carre(Collone c_, int y_, int hauteur_)
  63. {
  64. collone = c_;
  65. y = y_;
  66. hauteur = hauteur_;
  67.  
  68. tempsLast = SDL_GetTicks();
  69.  
  70. presence = true;
  71. }
  72.  
  73. void change(Collone c_, int y_, int hauteur_)
  74. {
  75. collone = c_;
  76. y = y_;
  77. hauteur = hauteur_;
  78.  
  79. tempsLast = SDL_GetTicks();
  80.  
  81. presence = true;
  82. }
  83.  
  84. void afficher(SDLDisplay & ecran)
  85. {
  86. if(presence)
  87. {
  88. switch(collone)
  89. {
  90. case COLLONE_1:
  91. ecran.putRect(static_cast<int>(collone) * 150, y, largeur, hauteur, Colors::Green);
  92. break;
  93. case COLLONE_2:
  94. ecran.putRect(static_cast<int>(collone) * 150, y, largeur, hauteur, Colors::Blue);
  95. break;
  96. case COLLONE_3:
  97. ecran.putRect(static_cast<int>(collone) * 150, y, largeur, hauteur, Colors::Yellow);
  98. break;
  99. case COLLONE_4:
  100. ecran.putRect(static_cast<int>(collone) * 150, y, largeur, hauteur, Colors::Cyan);
  101. break;
  102. case COLLONE_5:
  103. ecran.putRect(static_cast<int>(collone) * 150, y, largeur, hauteur, Colors::Magenta);
  104. break;
  105. case COLLONE_6:
  106. ecran.putRect(static_cast<int>(collone) * 150, y, largeur, hauteur, Colors::Orange);
  107. break;
  108. }
  109. }
  110. }
  111.  
  112. void descendre(int nbr, SDLDisplay & a)
  113. {
  114. if(presence)
  115. {
  116. if(SDL_GetTicks() - tempsLast > 20)
  117. {
  118. y += nbr;
  119. tempsLast = SDL_GetTicks();
  120. }
  121. if(y >= 900)
  122. {
  123. presence = false;
  124. FlashRoug::play();
  125. }
  126.  
  127. }
  128. }
  129.  
  130. bool testPresence()
  131. {
  132. return presence;
  133. }
  134.  
  135. void testClic(Input & input, bool & pasFoire)
  136. {
  137.  
  138. if(presence && LIGNE_TEST_CLIC > y && LIGNE_TEST_CLIC < y + hauteur )
  139. {
  140. switch(collone)
  141. {
  142. case COLLONE_1:
  143. if(input.getKeyStateOnce(SDL_SCANCODE_Q) )
  144. {
  145. presence = false;
  146. pasFoire = true;
  147. }
  148. break;
  149. case COLLONE_2:
  150. if(input.getKeyStateOnce(SDL_SCANCODE_W) )
  151. {
  152. presence = false;
  153. pasFoire = true;
  154. }
  155. break;
  156. case COLLONE_3:
  157. if(input.getKeyStateOnce(SDL_SCANCODE_E) )
  158. {
  159. presence = false;
  160. pasFoire = true;
  161. }
  162. break;
  163. case COLLONE_4:
  164. if(input.getKeyStateOnce(SDL_SCANCODE_R) )
  165. {
  166. presence = false;
  167. pasFoire = true;
  168. }
  169. break;
  170. case COLLONE_5:
  171. if(input.getKeyStateOnce(SDL_SCANCODE_T) )
  172. {
  173. presence = false;
  174. pasFoire = true;
  175. }
  176. break;
  177. case COLLONE_6:
  178. if(input.getKeyStateOnce(SDL_SCANCODE_T) )
  179. {
  180. presence = false;
  181. pasFoire = true;
  182. }
  183. break;
  184. }
  185. }
  186. }
  187.  
  188. private:
  189. Collone collone;
  190. int y;
  191.  
  192. static const int largeur = 150;
  193.  
  194. int hauteur;
  195.  
  196. int tempsLast;
  197.  
  198. bool presence;
  199. };
  200.  
  201.  
  202.  
  203. class ListeCarre
  204. {
  205. public:
  206. void add(Collone c, int y , int hauteur)
  207. {
  208. bool fait = false;
  209.  
  210. for(int i = 0 ; i < listCarre.size() ; i++)
  211. {
  212. if(!listCarre[i].testPresence() )
  213. {
  214. listCarre[i].change(c, y, hauteur);
  215. fait = true;
  216. }
  217. }
  218. if(!fait)
  219. {
  220. listCarre.push_back(Carre {c, y, hauteur});
  221. }
  222. }
  223.  
  224. void afficher(SDLDisplay & a)
  225. {
  226. for(int i = 0 ; i < listCarre.size() ; i++)
  227. {
  228. listCarre[i].afficher(a);
  229. }
  230. }
  231.  
  232. void descendre(int a, SDLDisplay & d)
  233. {
  234. for(int i = 0 ; i < listCarre.size() ; i++)
  235. {
  236. listCarre[i].descendre(a, d);
  237. }
  238. }
  239.  
  240. void testClic(Input & in, SDLDisplay & d)
  241. {
  242. bool pasFoire;
  243.  
  244. for(int i = 0 ; i < listCarre.size() ; i++)
  245. {
  246. listCarre[i].testClic(in, pasFoire);
  247. }
  248.  
  249. if(!pasFoire)
  250. {
  251. FlashRoug::play();
  252. }
  253. }
  254.  
  255. private :
  256. vector<Carre> listCarre;
  257. };
  258.  
  259. int main()
  260. {
  261. /* Gestion de la fenêtre */
  262. SDLDisplay display("minijeu", 900, 900);
  263. /* Gestion des touches */
  264. Input input;
  265.  
  266. // Les déclarations de variables se font ici
  267.  
  268. ListeCarre listCarre;
  269.  
  270. listCarre.add(COLLONE_1, 0, 100);
  271. listCarre.add(COLLONE_2, 0, 100);
  272. listCarre.add(COLLONE_3, 0, 100);
  273. listCarre.add(COLLONE_4, 0, 100);
  274. listCarre.add(COLLONE_5, 0, 100);
  275. listCarre.add(COLLONE_6, 0, 100);
  276.  
  277.  
  278.  
  279. while (!(input.shouldExit()) && !(input.getKeyState(SDL_SCANCODE_ESCAPE)))
  280. {
  281. // code
  282.  
  283. listCarre.descendre(5, display);
  284.  
  285. listCarre.testClic(input, display);
  286.  
  287. // affichage
  288.  
  289. display.clearScreen(); // "Nettoie" la fenêtre
  290.  
  291.  
  292. FlashRoug::affiche(display);
  293.  
  294. listCarre.afficher(display);
  295.  
  296. display.putRect(0, LIGNE_TEST_CLIC, 900, 2, Colors::White);
  297.  
  298. display.refreshScreen(); // Rafraîchit la fenêtre
  299. input.flushEvents(); // Met à jour les touches pressées / clics de souris
  300. }
  301. return (0);
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement