Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 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 <fstream>
  13. #include <string>
  14. #include "SDLDisplay.hpp"
  15. #include "Input.hpp"
  16. #include "Colors.hpp" // ici les couleurs
  17. #include "RNG.hpp"
  18. bool RNG::_initialized = false;
  19.  
  20. #define LIGNE_TEST_CLIC 750
  21.  
  22. using namespace std;
  23.  
  24.  
  25.  
  26. enum Collone
  27. {
  28. COLLONE_1,
  29. COLLONE_2,
  30. COLLONE_3,
  31. COLLONE_4,
  32. COLLONE_5
  33. };
  34.  
  35. class FlashRoug
  36. {
  37. public:
  38.  
  39. FlashRoug(){}
  40.  
  41. static void play() // FlasRoug::play()
  42. {
  43. tempsLast = SDL_GetTicks();
  44. }
  45.  
  46. static void affiche(SDLDisplay & a)
  47. {
  48. if(SDL_GetTicks() - tempsLast < 50)
  49. {
  50. a.putRect(150, 0, 750, 900, Colors::Red);
  51. }
  52. }
  53.  
  54. private:
  55.  
  56. const int duree = 100;
  57. static int tempsLast;
  58. };
  59.  
  60. int FlashRoug::tempsLast = 0;
  61.  
  62. class Score
  63. {
  64. public:
  65. static void reset()
  66. {
  67. score = 0;
  68. }
  69. static void perfect()
  70. {
  71. suite++;
  72. score += 5000 * multiply();
  73. cout << "PARFAIT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  74. }
  75. static void normal()
  76. {
  77. cout << "touche !!!!!!!!!!!!!!"<<endl;
  78. suite++;
  79. score += 1000 * multiply();
  80. }
  81. static void rate()
  82. {
  83. cout << "rate !" << endl;
  84. score -= 500;
  85. suite = 0;
  86. FlashRoug::play();
  87. }
  88. static void malTouche()
  89. {
  90. FlashRoug::play();
  91. cout << "hum hum..." << endl;
  92. score -= 100;
  93. suite = 0;
  94. }
  95. static int multiply()
  96. {
  97. return (suite / 5 )+ 1;
  98. }
  99. static long int getScore()
  100. {
  101. return score;
  102. }
  103.  
  104. private:
  105. static long int score;
  106. static int suite;
  107. };
  108.  
  109. long int Score::score = 0;
  110. int Score::suite = 0;
  111.  
  112. void affichScore(SDLDisplay & a)
  113. {
  114. string nbr { to_string( Score::getScore() ) };
  115.  
  116. for(unsigned int i = 0 ; i < nbr.size() ; i++)
  117. {
  118. string b;
  119. b += "0";
  120. b += nbr[i];
  121. b += ".png";
  122.  
  123. a.putImage(b , 40 * i, 0);
  124. }
  125. }
  126.  
  127. class Carre
  128. {
  129. public:
  130. Carre(Collone c_, int y_, int hauteur_)
  131. {
  132. collone = c_;
  133. y = y_;
  134. hauteur = hauteur_;
  135.  
  136. tempsLast = SDL_GetTicks();
  137.  
  138. presence = true;
  139. }
  140.  
  141. void change(Collone c_, int y_, int hauteur_)
  142. {
  143. collone = c_;
  144. y = y_;
  145. hauteur = hauteur_;
  146.  
  147. tempsLast = SDL_GetTicks();
  148.  
  149. presence = true;
  150. }
  151.  
  152. void afficher(SDLDisplay & ecran)
  153. {
  154. if(presence)
  155. {
  156. switch(collone)
  157. {
  158. case COLLONE_1:
  159. ecran.putRect(static_cast<int>(collone) * 150 + 150 + 10, y, largeur, hauteur, Colors::Green);
  160. break;
  161. case COLLONE_2:
  162. ecran.putRect(static_cast<int>(collone) * 150 + 150 + 10, y, largeur, hauteur, Colors::Blue);
  163. break;
  164. case COLLONE_3:
  165. ecran.putRect(static_cast<int>(collone) * 150 + 150 + 10, y, largeur, hauteur, Colors::Yellow);
  166. break;
  167. case COLLONE_4:
  168. ecran.putRect(static_cast<int>(collone) * 150 + 150 + 10, y, largeur, hauteur, Colors::Cyan);
  169. break;
  170. case COLLONE_5:
  171. ecran.putRect(static_cast<int>(collone) * 150 + 150 + 10, y, largeur, hauteur, Colors::Magenta);
  172. break;
  173. }
  174.  
  175. ecran.putRect(static_cast<int>(collone) * 150 + 150 +10 + 20, y + (45.0 / 100.0 * hauteur), largeur -40, (10.0/100.0 * hauteur), Colors::Fushia);
  176. }
  177. }
  178.  
  179. void descendre(int nbr)
  180. {
  181. if(presence)
  182. {
  183. if(SDL_GetTicks() - tempsLast > 20)
  184. {
  185. y += nbr;
  186. tempsLast = SDL_GetTicks();
  187. }
  188. if(y >= 900)
  189. {
  190. presence = false;
  191. cout << "a pu" << endl;
  192. Score::rate();
  193. }
  194.  
  195. }
  196. }
  197.  
  198. bool testPresence()
  199. {
  200. return presence;
  201. }
  202.  
  203. bool bienPlace()
  204. {
  205. if(LIGNE_TEST_CLIC + 10 > y && LIGNE_TEST_CLIC < y + hauteur + 10 )
  206. {
  207. return true;
  208. }
  209. else
  210. {
  211. return false;
  212. }
  213. }
  214.  
  215. Collone getCollone()
  216. {
  217. return collone;
  218. }
  219.  
  220. void clique()
  221. {
  222. cout << "tu te caches maintenant" << endl;
  223. presence = false;
  224. }
  225.  
  226. bool testPerfect()
  227. {
  228. if(y + (45.0 / 100 * hauteur) <= LIGNE_TEST_CLIC && y + (55.0 / 100 * hauteur >= LIGNE_TEST_CLIC ) )//testPerfect
  229. {
  230. return true;
  231. }
  232. else
  233. {
  234. return false;
  235. }
  236. }
  237.  
  238. private:
  239. Collone collone;
  240. int y;
  241.  
  242. static const int largeur = 130;
  243.  
  244. int hauteur;
  245.  
  246. int tempsLast;
  247.  
  248. bool presence;
  249. };
  250.  
  251.  
  252.  
  253. class ListeCarre
  254. {
  255. public:
  256. void add(Collone c, int y , int hauteur)
  257. {
  258. bool fait = false;
  259.  
  260. for(int i = 0 ; i < listCarre.size() ; i++)
  261. {
  262. if(!listCarre[i].testPresence() && !fait)
  263. {
  264. listCarre[i].change(c, y, hauteur);
  265. fait = true;
  266. }
  267. }
  268.  
  269. if(!fait)
  270. {
  271. listCarre.push_back(Carre {c, y, hauteur});
  272. }
  273. }
  274.  
  275. void afficher(SDLDisplay & a)
  276. {
  277. for(unsigned int i = 0 ; i < listCarre.size() ; i++)
  278. {
  279. listCarre[i].afficher(a);
  280. }
  281. }
  282.  
  283. void descendre(int a)
  284. {
  285. for(unsigned int i = 0 ; i < listCarre.size() ; i++)
  286. {
  287. listCarre[i].descendre(a);
  288. }
  289. }
  290.  
  291. void testClic(Input & input)
  292. {
  293. bool pasFoire = true;
  294.  
  295. if(input.getKeyStateOnce(SDL_SCANCODE_Q) )
  296. {
  297. if( !testPosEach(COLLONE_1) )
  298. {
  299. pasFoire = false;
  300. }
  301. }
  302.  
  303. if(input.getKeyStateOnce(SDL_SCANCODE_W) )
  304. {
  305. if( !testPosEach(COLLONE_2) )
  306. {
  307. pasFoire = false;
  308. }
  309. }
  310.  
  311. if(input.getKeyStateOnce(SDL_SCANCODE_E) )
  312. {
  313. if( !testPosEach(COLLONE_3) )
  314. {
  315. pasFoire = false;
  316. }
  317. }
  318.  
  319. if(input.getKeyStateOnce(SDL_SCANCODE_R) )
  320. {
  321. if( !testPosEach(COLLONE_4) )
  322. {
  323. pasFoire = false;
  324. }
  325. }
  326.  
  327. if(input.getKeyStateOnce(SDL_SCANCODE_T) )
  328. {
  329. if( !testPosEach(COLLONE_5) )
  330. {
  331. pasFoire = false;
  332. }
  333. }
  334.  
  335. if(!pasFoire)
  336. {
  337. Score::malTouche();
  338.  
  339. }
  340. }
  341.  
  342. bool testPosEach(Collone const & co)
  343. {
  344. for(unsigned int i = 0; i < listCarre.size() ; i++)
  345. {
  346. if(listCarre[i].bienPlace() && co == listCarre[i].getCollone() && listCarre[i].testPresence() )
  347. {
  348. listCarre[i].clique();
  349.  
  350. if(listCarre[i].testPerfect() )
  351. {
  352. Score::perfect();
  353. }
  354. else
  355. {
  356. Score::normal();
  357. }
  358.  
  359. return true;
  360. }
  361. }
  362.  
  363. cout << "pas chaché mé appuyé" << endl;
  364. return false;
  365. }
  366.  
  367. private :
  368. vector<Carre> listCarre;
  369. };
  370.  
  371. class LirMus
  372. {
  373. public:
  374. static void lirNote(ListeCarre & a)
  375. {
  376. if(fichier)
  377. {
  378. if(SDL_GetTicks() - tempsLast >= 1000)
  379. {
  380. string ligne;
  381. int colloneInt;
  382.  
  383. fichier >> colloneInt;
  384.  
  385. getline(fichier, ligne);
  386.  
  387. Collone c = static_cast<Collone>(colloneInt);
  388.  
  389. a.add(c, 0, 100);
  390.  
  391. tempsLast = SDL_GetTicks();
  392. }
  393. }
  394. }
  395. private:
  396. static int tempsLast;
  397. static ifstream fichier;
  398. };
  399.  
  400. int LirMus::tempsLast = 0;
  401. ifstream LirMus::fichier {R"(musique1.mus)"};
  402.  
  403. int main()
  404. {
  405. /* Gestion de la fenêtre */
  406. SDLDisplay display("minijeu", 900, 900);
  407. /* Gestion des touches */
  408. Input input;
  409.  
  410. // Les déclarations de variables se font ici
  411.  
  412. ListeCarre listCarre;
  413.  
  414. listCarre.add(COLLONE_1, 0, 100);
  415. listCarre.add(COLLONE_2, 0, 100);
  416. listCarre.add(COLLONE_3, 0, 100);
  417. listCarre.add(COLLONE_4, 0, 100);
  418. listCarre.add(COLLONE_5, 0, 100);
  419.  
  420.  
  421.  
  422. while (!(input.shouldExit()) && !(input.getKeyState(SDL_SCANCODE_ESCAPE)))
  423. {
  424. // code
  425.  
  426. listCarre.descendre(5);
  427.  
  428. listCarre.testClic(input);
  429.  
  430. LirMus::lirNote(listCarre);
  431. // affichage
  432.  
  433. display.clearScreen(); // "Nettoie" la fenêtre
  434.  
  435.  
  436. FlashRoug::affiche(display);
  437.  
  438.  
  439.  
  440. display.putRect(150, LIGNE_TEST_CLIC, 900 - 150, 2, Colors::White);
  441. display.putRect(145, 0, 5, 900, Colors::White);
  442.  
  443. display.putRect(160, LIGNE_TEST_CLIC + 10, 130, 130, Colors::Green);
  444. display.putRect(310, LIGNE_TEST_CLIC + 10, 130, 130, Colors::Blue);
  445. display.putRect(460, LIGNE_TEST_CLIC + 10, 130, 130, Colors::Yellow);
  446. display.putRect(610, LIGNE_TEST_CLIC + 10, 130, 130, Colors::Cyan);
  447. display.putRect(760, LIGNE_TEST_CLIC + 10, 130, 130, Colors::Magenta);
  448.  
  449. display.putRect(170, LIGNE_TEST_CLIC + 30, 110, 110, Colors::Black);
  450. display.putRect(320, LIGNE_TEST_CLIC + 30, 110, 110, Colors::Black);
  451. display.putRect(470, LIGNE_TEST_CLIC + 30, 110, 110, Colors::Black);
  452. display.putRect(620, LIGNE_TEST_CLIC + 30, 110, 110, Colors::Black);
  453. display.putRect(770, LIGNE_TEST_CLIC + 30, 110, 110, Colors::Black);
  454.  
  455. display.putRect(300, 0, 1, LIGNE_TEST_CLIC, Colors::White);
  456. display.putRect(450, 0, 1, LIGNE_TEST_CLIC, Colors::White);
  457. display.putRect(600, 0, 1, LIGNE_TEST_CLIC, Colors::White);
  458. display.putRect(750, 0, 1, LIGNE_TEST_CLIC, Colors::White);
  459.  
  460. listCarre.afficher(display);
  461.  
  462. affichScore(display);
  463.  
  464. display.refreshScreen(); // Rafraîchit la fenêtre
  465. input.flushEvents(); // Met à jour les touches pressées / clics de souris
  466.  
  467. SDL_Delay(1);
  468. }
  469.  
  470. cout << Score::getScore() << endl;
  471. return (0);
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement