Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.79 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_acodec.h>
  3. #include <allegro5/allegro_audio.h>
  4. #include <allegro5/allegro_color.h>
  5. #include <allegro5/allegro_font.h>
  6. #include <allegro5/allegro_image.h>
  7. #include <allegro5/allegro_primitives.h>
  8. #include <allegro5/allegro_native_dialog.h>
  9. #include <allegro5/allegro_ttf.h>
  10. #include <iostream>
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <cmath>
  14. #include <time.h>
  15.  
  16. using namespace std;
  17.  
  18. int screen_width = 800;
  19. int screen_height = 600;
  20.  
  21. int main()
  22. {
  23. enum Direction { DOWN, LEFT, RIGHT, UP };
  24. const float frameFPS = 10.0;
  25.  
  26. if (!al_init())
  27. {
  28. fprintf(stderr, "Can't load allegro\n");
  29. exit(1);
  30. }
  31.  
  32. ALLEGRO_DISPLAY* display = al_create_display(screen_width, screen_height);
  33. if (!display) {
  34. fprintf(stderr, "Can't load display\n");
  35. exit(1);
  36. }
  37.  
  38. al_set_window_position(display, 200, 200);
  39. al_set_window_title(display, "Have Time ? Let's PLAY !");
  40.  
  41.  
  42. al_init_primitives_addon();
  43. al_install_keyboard();
  44. al_install_mouse();
  45. al_init_image_addon();
  46. al_init_font_addon();
  47. al_init_ttf_addon();
  48. al_install_audio();
  49. al_init_acodec_addon();
  50.  
  51. ALLEGRO_TIMER* timer = al_create_timer(1.0 / 10);
  52. //snake
  53. ALLEGRO_TIMER* timerS = al_create_timer(1.0 / 10);
  54. ALLEGRO_TIMER* timerK = al_create_timer(1.0 / 50);
  55. ALLEGRO_TIMER* timerF = al_create_timer(1.0 / 60);
  56. ALLEGRO_TIMER* frameTimer = al_create_timer(1.0 / frameFPS);
  57. ALLEGRO_TIMER* game_time = al_create_timer(1);
  58. ALLEGRO_KEYBOARD_STATE keyState;
  59.  
  60. ALLEGRO_FONT* introF1 = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro1.ttf", 100, NULL);
  61. if (!introF1) {
  62. fprintf(stderr, "Can't load font\n");
  63. exit(1);
  64. }
  65. ALLEGRO_FONT* introF2 = al_load_ttf_font("C:/Users/Windica/source/repos/snake3/snake.ttf", 140, NULL);
  66.  
  67. ALLEGRO_FONT* menu_selectF = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro2.ttf", 30, NULL);
  68. ALLEGRO_FONT* menuF = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro2.ttf", 50, NULL);
  69. ALLEGRO_FONT* click = al_load_font("C:/Users/Windica/source/repos/Menu/Fonts/intro2.ttf", 30, NULL);
  70. //Snake
  71. ALLEGRO_FONT* font = al_load_ttf_font("C:/Users/Windica/source/repos/snake3/snake.ttf", 50, NULL);
  72. ALLEGRO_FONT* font1 = al_load_ttf_font("C:/Users/Windica/source/repos/snake3/snake.ttf", 180, NULL);
  73.  
  74. ALLEGRO_SAMPLE* menuSample = al_load_sample("C:/Users/Windica/source/repos/Menu/Sounds/Menu.wav");
  75. if (!menuSample) {
  76. fprintf(stderr, "Can't load intro sound\n");
  77. exit(1);
  78. }
  79. ALLEGRO_SAMPLE_ID menuSampleID;
  80. //Snake
  81. ALLEGRO_SAMPLE* eat = al_load_sample("C:/Users/Windica/source/repos/snake3/eat.wav");
  82. ALLEGRO_SAMPLE* mushroom_eat = al_load_sample("C:/Users/Windica/source/repos/snake3/mushroom_eat.wav");
  83. ALLEGRO_SAMPLE* snakeSample = al_load_sample("C:/Users/Windica/source/repos/snake3/background_music.wav");
  84. ALLEGRO_SAMPLE* fail = al_load_sample("C:/Users/Windica/source/repos/snake3/fail.wav");
  85. ALLEGRO_SAMPLE* birdUP = al_load_sample("C:/Users/Windica/source/repos/menu/Sounds/birdupS.wav");
  86. ALLEGRO_SAMPLE* birdSCORE = al_load_sample("C:/Users/Windica/source/repos/menu/Sounds/birdscoreS.wav");
  87. ALLEGRO_SAMPLE_ID snakeSampleID;
  88. ALLEGRO_SAMPLE_ID birdUPID;
  89.  
  90. al_reserve_samples(7);
  91.  
  92.  
  93. ALLEGRO_BITMAP* do_not_disturb = al_load_bitmap("C:/Users/Windica/source/repos/Menu/Images/do_not_disturb.png");
  94. al_convert_mask_to_alpha(do_not_disturb, al_map_rgb(255, 255, 255));
  95. ALLEGRO_BITMAP* background = al_load_bitmap("C:/Users/Windica/source/repos/Menu/Images/background.png");
  96. ALLEGRO_BITMAP* bird = al_load_bitmap("C:/Users/Windica/source/repos/Menu/Images/bird.png");
  97. al_convert_mask_to_alpha(bird, al_map_rgb(255, 255, 255));
  98. //Snake
  99. ALLEGRO_BITMAP* headDOWN = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headDOWN.png");
  100. al_convert_mask_to_alpha(headDOWN, al_map_rgb(255, 255, 255));
  101. ALLEGRO_BITMAP* headLEFT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headLEFT.png");
  102. al_convert_mask_to_alpha(headLEFT, al_map_rgb(255, 255, 255));
  103. ALLEGRO_BITMAP* headRIGHT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headRIGHT.png");
  104. al_convert_mask_to_alpha(headRIGHT, al_map_rgb(255, 255, 255));
  105. ALLEGRO_BITMAP* headUP = al_load_bitmap("C:/Users/Windica/source/repos/snake3/headUP.png");
  106. al_convert_mask_to_alpha(headUP, al_map_rgb(255, 255, 255));
  107.  
  108. ALLEGRO_BITMAP* bodyDOWN = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyDOWN.png");
  109. al_convert_mask_to_alpha(bodyDOWN, al_map_rgb(255, 255, 255));
  110. ALLEGRO_BITMAP* bodyLEFT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyLEFT.png");
  111. al_convert_mask_to_alpha(bodyLEFT, al_map_rgb(255, 255, 255));
  112. ALLEGRO_BITMAP* bodyRIGHT = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyRIGHT.png");
  113. al_convert_mask_to_alpha(bodyRIGHT, al_map_rgb(255, 255, 255));
  114. ALLEGRO_BITMAP* bodyUP = al_load_bitmap("C:/Users/Windica/source/repos/snake3/bodyUP.png");
  115. al_convert_mask_to_alpha(bodyUP, al_map_rgb(255, 255, 255));
  116.  
  117. ALLEGRO_BITMAP* logo = al_load_bitmap("C:/Users/Windica/source/repos/snake3/snake_logo.png");
  118. al_convert_mask_to_alpha(logo, al_map_rgb(255, 255, 255));
  119.  
  120. ALLEGRO_BITMAP* rabbit = al_load_bitmap("C:/Users/Windica/source/repos/snake3/rabbit.png");
  121. al_convert_mask_to_alpha(rabbit, al_map_rgb(255, 255, 255));
  122. ALLEGRO_BITMAP* gold_rabbit = al_load_bitmap("C:/Users/Windica/source/repos/snake3/goldrabbit.png");
  123. al_convert_mask_to_alpha(gold_rabbit, al_map_rgb(255, 255, 255));
  124. ALLEGRO_BITMAP* mushroom = al_load_bitmap("C:/Users/Windica/source/repos/snake3/mushroom.png");
  125. al_convert_mask_to_alpha(mushroom, al_map_rgb(255, 255, 255));
  126. ALLEGRO_BITMAP* frog = al_load_bitmap("C:/Users/Windica/source/repos/snake3/frog.png");
  127. al_convert_mask_to_alpha(frog, al_map_rgb(255, 255, 255));
  128.  
  129. ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
  130. al_register_event_source(event_queue, al_get_mouse_event_source());
  131. al_register_event_source(event_queue, al_get_keyboard_event_source());
  132. al_register_event_source(event_queue, al_get_timer_event_source(timer));
  133. al_register_event_source(event_queue, al_get_timer_event_source(timerS));
  134. al_register_event_source(event_queue, al_get_timer_event_source(timerK));
  135. al_register_event_source(event_queue, al_get_timer_event_source(timerF));
  136. al_register_event_source(event_queue, al_get_timer_event_source(frameTimer));
  137. al_register_event_source(event_queue, al_get_timer_event_source(game_time));
  138. al_register_event_source(event_queue, al_get_display_event_source(display));
  139.  
  140. al_start_timer(timer);
  141. al_start_timer(timerS);
  142. al_start_timer(frameTimer);
  143. al_start_timer(game_time);
  144. srand(time(NULL));
  145.  
  146. bool doneM = false;
  147. bool intro = true;
  148. bool menu = false;
  149. bool drawM = true;
  150. bool snake = false;
  151. bool kwadrat = false;
  152. bool flip = false;
  153.  
  154. int Mx = 0;
  155. int My = 0;
  156. int timeI = 0;
  157.  
  158.  
  159. while (!doneM) {
  160.  
  161. ALLEGRO_EVENT events;
  162. al_wait_for_event(event_queue, &events);
  163.  
  164.  
  165. if (events.type == ALLEGRO_EVENT_KEY_UP)
  166. {
  167. switch (events.keyboard.keycode) {
  168. case ALLEGRO_KEY_ESCAPE:
  169. doneM = true;
  170. break;
  171. case ALLEGRO_KEY_ENTER:
  172. break;
  173. }
  174. }
  175. else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  176. doneM = true;
  177. else if (events.type == ALLEGRO_EVENT_MOUSE_AXES)
  178. {
  179. Mx = events.mouse.x;
  180. My = events.mouse.y;
  181. }
  182. else if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
  183. if (events.mouse.button & 1) {
  184. if (menu == true && Mx >= 360 && Mx < 500 && My >= 160 && My < 210)
  185. {
  186. al_stop_sample(&menuSampleID);
  187. snake = true;
  188. menu = false;
  189. }
  190.  
  191. if (menu == true && Mx >= 360 && Mx < 500 && My >= 260 && My < 310)
  192. {
  193. al_stop_sample(&menuSampleID);
  194. kwadrat = true;
  195. menu = false;
  196. }
  197. if (menu == true && Mx >= 360 && Mx < 500 && My >= 360 && My < 410)
  198. {
  199. al_stop_sample(&menuSampleID);
  200. flip = true;
  201. menu = false;
  202. }
  203.  
  204. }
  205. }
  206. else if (events.type == ALLEGRO_EVENT_TIMER) {
  207. if (events.timer.source == timer) {
  208. drawM = true;
  209. if (drawM == true)
  210. {
  211. if (intro == true)
  212. {
  213. al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
  214. for (int i = 0; i < 8500; i++) {
  215. al_draw_text(introF2, al_map_rgb(255, 0, 255), 130, 200, NULL, "HAVE TIME ?");
  216. al_flip_display();
  217. }
  218. al_draw_text(introF1, al_map_rgb(255, 0, 255), 130, 200, NULL, "LET'S PLAY !");
  219. al_flip_display();
  220. al_clear_to_color(al_map_rgb(0, 0, 0));
  221. do {
  222. al_get_keyboard_state(&keyState);
  223. al_draw_text(introF1, al_map_rgb(255, 0, 255), 130, 200, NULL, "LET'S PLAY !");
  224. al_draw_text(click, al_map_rgb(255, 0, 255), 170, 400, NULL, "Press the button to START !");
  225. al_wait_for_event(event_queue, &events);
  226. al_flip_display();
  227. al_clear_to_color(al_map_rgb(0, 0, 0));
  228. } while (events.type != ALLEGRO_EVENT_KEY_DOWN);
  229. intro = false;
  230. menu = true;
  231.  
  232. }
  233. if (menu == true)
  234. {
  235. al_draw_bitmap(do_not_disturb, 740, 510, NULL);
  236. al_draw_text(menuF, al_map_rgb(255, 0, 255), 190, 50, NULL, "GAME SELECT MENU");
  237. al_draw_text(menu_selectF, al_map_rgb(255, 0, 255), 380, 170, NULL, "SNAKE");
  238. al_draw_text(menu_selectF, al_map_rgb(255, 0, 255), 380, 270, NULL, "CHILL");
  239. al_draw_text(menu_selectF, al_map_rgb(255, 0, 255), 380, 370, NULL, "Flappy Bird");
  240. }
  241. if (snake == true)
  242. {
  243. al_set_window_position(display, 200, 200);
  244. al_set_window_title(display, "HTLP - Snake");
  245. bool doneS = false, activeS = false;
  246. bool drawS = true;
  247. int x = 0, y = 0, moveSpeed = 5;
  248. al_play_sample(snakeSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &snakeSampleID);
  249. int eventT = 0;
  250. int timeS = 0;
  251. int timeF = 0;
  252. int dir = DOWN, prevdir = DOWN;
  253. int score = 1;
  254. int lastX;
  255. int lastY;
  256. char finalscore[100];
  257.  
  258. int rabbitX = 40 * (rand() % 20);
  259. int rabbitY = 40 * (rand() % 15);
  260.  
  261. int gold_rabbitX = 40 * (rand() % 20);
  262. int gold_rabbitY = 40 * (rand() % 15);
  263.  
  264. int mushroomX = 40 * (rand() % 20);
  265. int mushroomY = 40 * (rand() % 15);
  266.  
  267. int frogX = 40 * (rand() % 20);
  268. int frogY = 40 * (rand() % 15);
  269.  
  270. int snakeT[150];
  271. for (int i = 0; i <= 149; i++) {
  272. snakeT[i] = 0;
  273. }
  274. int snakeX[150], snakeY[150];
  275.  
  276. bool menuS = true;
  277. bool dead = false;
  278. bool drawgolden = false;
  279. while (!doneS) {
  280.  
  281.  
  282.  
  283. lastX = x;
  284. lastY = y;
  285.  
  286.  
  287. ALLEGRO_EVENT events;
  288. al_wait_for_event(event_queue, &events);
  289.  
  290.  
  291. if (events.type == ALLEGRO_EVENT_KEY_UP)
  292. {
  293. al_get_keyboard_state(&keyState);
  294. switch (events.keyboard.keycode) {
  295. case ALLEGRO_KEY_ESCAPE:
  296. al_stop_sample(&snakeSampleID);
  297. doneS = true;
  298. snake = false;
  299. menu = true;
  300. break;
  301. case ALLEGRO_KEY_ENTER:
  302. if (menuS) menuS = false, score = 1, timeS = 0, x = 0, y = 0;
  303. break;
  304. }
  305. }
  306. else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  307. doneS = true;
  308. snake = false;
  309. doneM = true;
  310. break;
  311. }
  312.  
  313. if (events.type == ALLEGRO_EVENT_TIMER) {
  314. if (events.timer.source == game_time) {
  315. timeS++;
  316. eventT++;
  317. }
  318. if (events.timer.source == timer) {
  319.  
  320. al_get_keyboard_state(&keyState);
  321. if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT) && dir != LEFT)
  322. dir = RIGHT;
  323. else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT) && dir != RIGHT)
  324. dir = LEFT;
  325. else if (al_key_down(&keyState, ALLEGRO_KEY_UP) && dir != DOWN)
  326. dir = UP;
  327. else if (al_key_down(&keyState, ALLEGRO_KEY_DOWN) && dir != UP)
  328. dir = DOWN;
  329. else if (al_key_down(&keyState, ALLEGRO_KEY_A))
  330. score++;
  331. else if (al_key_down(&keyState, ALLEGRO_KEY_ENTER) && menu == true)
  332. menu = false, score = 1, timeS = 0, x = 0, y = 0;
  333. if (menu == false) {
  334. if (score != 0) {
  335. for (int i = score; i > 0; i--) {
  336. snakeX[i] = snakeX[i - 1];
  337. snakeY[i] = snakeY[i - 1];
  338. }
  339. snakeX[0] = lastX;
  340. snakeY[0] = lastY;
  341. }
  342. }
  343.  
  344. switch (dir) {
  345. case RIGHT: x = x + 40;
  346. break;
  347. case LEFT: x = x - 40;
  348. break;
  349. case UP: y = y - 40;
  350. break;
  351. case DOWN: y = y + 40;
  352. break;
  353. }
  354.  
  355.  
  356.  
  357. if (x == rabbitX && y == rabbitY) {
  358. al_play_sample(eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  359. score++;
  360. rabbitX = 40 * (rand() % 20);
  361. rabbitY = 40 * (rand() % 15);
  362. if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
  363. rabbitX = 40 * (rand() % 20);
  364. rabbitY = 40 * (rand() % 15);
  365. }
  366. snakeT[score] = 1;
  367. }
  368.  
  369. if (x == frogX && y == frogY) {
  370. al_play_sample(eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  371. score--;
  372. frogX = 40 * (rand() % 20);
  373. frogY = 40 * (rand() % 15);
  374. if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
  375. frogX = 40 * (rand() % 20);
  376. frogY = 40 * (rand() % 15);
  377. }
  378. snakeT[score] = 0;
  379. }
  380.  
  381. if (x == mushroomX && y == mushroomY) {
  382. al_play_sample(mushroom_eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  383. score = score - 5;
  384. mushroomX = 40 * (rand() % 20);
  385. mushroomY = 40 * (rand() % 15);
  386. if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || mushroomX == frogX && mushroomY == frogY) {
  387. mushroomX = 40 * (rand() % 20);
  388. mushroomY = 40 * (rand() % 15);
  389. }
  390. snakeT[score] = 0;
  391. }
  392.  
  393. if (x == gold_rabbitX && y == gold_rabbitY) {
  394. al_play_sample(eat, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  395. score = score + 3;
  396. drawgolden = false;
  397. gold_rabbitX = 40 * (rand() % 20);
  398. gold_rabbitY = 40 * (rand() % 15);
  399. if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
  400. gold_rabbitX = 40 * (rand() % 20);
  401. gold_rabbitY = 40 * (rand() % 15);
  402. }
  403. snakeT[score] = 1;
  404. }
  405.  
  406. if (eventT % 6 == 0) {
  407. eventT++;
  408. mushroomX = 40 * (rand() % 20);
  409. mushroomY = 40 * (rand() % 15);
  410. if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
  411. mushroomX = 40 * (rand() % 20);
  412. mushroomY = 40 * (rand() % 15);
  413. }
  414. }
  415.  
  416. if (eventT % 15 == 0) {
  417. eventT++;
  418. drawgolden = true;
  419. gold_rabbitX = 40 * (rand() % 20);
  420. gold_rabbitY = 40 * (rand() % 15);
  421. if (mushroomX == rabbitX && mushroomY == rabbitY || mushroomX == gold_rabbitX && mushroomY == gold_rabbitY || rabbitX == frogX && rabbitY == frogY) {
  422. gold_rabbitX = 40 * (rand() % 20);
  423. gold_rabbitY = 40 * (rand() % 15);
  424. }
  425. }
  426.  
  427.  
  428. if (eventT % 5 == 0) {
  429. drawgolden = false;
  430. }
  431.  
  432. if (menuS == false) {
  433. for (int i = 1; i < score; i++) {
  434. if (x == snakeX[i] && y == snakeY[i] && menuS == false) {
  435. al_play_sample(fail, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  436. sprintf_s(finalscore, "%d", score - 1);
  437. al_show_native_message_box(display, "GAME OVER", "GAME OVER - your score", finalscore, NULL, ALLEGRO_MESSAGEBOX_ERROR);
  438. dead = true;
  439. al_rest(2.0);
  440. }
  441. }
  442. if (x < 0 || x >= screen_width || y < 0 || y >= screen_height && menuS == false) {
  443. al_play_sample(fail, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  444. sprintf_s(finalscore, "%d", score - 1);
  445. al_show_native_message_box(display, "GAME OVER", "GAME OVER - your score", finalscore, NULL, ALLEGRO_MESSAGEBOX_ERROR);
  446. dead = true;
  447. al_rest(2.0);
  448. }
  449. }
  450.  
  451. drawS = true;
  452.  
  453. }
  454. }
  455. prevdir = dir;
  456.  
  457. if (dead && menuS == false) {
  458. menuS = true;
  459. timeF = timeS;
  460. x = 0, y = 0;
  461. for (int i = 0; i <= 149; i++) {
  462. snakeT[i] = 0;
  463. }
  464. dead = false;
  465. dir = DOWN;
  466. }
  467.  
  468. if (drawS == true) {
  469. drawS = false;
  470. if (menuS) {
  471. x = 0, y = 0;
  472. for (int i = 0; i <= 149; i++) {
  473. snakeT[i] = 0;
  474. }
  475. al_draw_bitmap(logo, 0, 200, NULL);
  476. al_draw_bitmap(logo, 550, 200, NULL);
  477. al_draw_text(font1, al_map_rgb(255, 0, 0), 230, 10, 0, "SNAKE");
  478. al_draw_text(font, al_map_rgb(100, 50, 250), 225, 220, 0, "Press Enter to Start");
  479. al_draw_text(font, al_map_rgb(100, 50, 250), 245, 320, 0, "Press Esc to Exit");;
  480. al_draw_text(font, al_map_rgb(250, 0, 250), 0, 430, 0, "rabbit = +1");
  481. al_draw_text(font, al_map_rgb(250, 0, 250), 0, 470, 0, "Gold rabbit = +3");
  482. al_draw_text(font, al_map_rgb(250, 0, 250), 0, 510, 0, "frog = -1");
  483. al_draw_text(font, al_map_rgb(250, 0, 250), 0, 550, 0, "mushroom = -5");
  484. al_draw_textf(font, al_map_rgb(250, 0, 250), 650, 550, 0, "Score: %i", score - 1);
  485. }
  486. else {
  487.  
  488. al_draw_bitmap(mushroom, mushroomX, mushroomY, NULL);
  489. al_draw_bitmap(rabbit, rabbitX, rabbitY, NULL);
  490. al_draw_bitmap(frog, frogX, frogY, NULL);
  491. if (drawgolden == true) {
  492. al_draw_bitmap(gold_rabbit, gold_rabbitX, gold_rabbitY, NULL);
  493. }
  494. switch (prevdir) {
  495. case RIGHT: for (int i = 0; i < score; i++) {
  496. al_draw_bitmap(bodyRIGHT, snakeX[i], snakeY[i], NULL);
  497. }
  498. break;
  499. case LEFT: for (int i = 0; i < score; i++) {
  500. al_draw_bitmap(bodyRIGHT, snakeX[i], snakeY[i], NULL);
  501. }
  502. break;
  503. case UP: for (int i = 0; i < score; i++) {
  504. al_draw_bitmap(bodyUP, snakeX[i], snakeY[i], NULL);
  505. }
  506. break;
  507. case DOWN: for (int i = 0; i < score; i++) {
  508. al_draw_bitmap(bodyUP, snakeX[i], snakeY[i], NULL);
  509. }
  510. break;
  511. }
  512.  
  513. switch (prevdir) {
  514. case RIGHT: al_draw_bitmap(headRIGHT, x, y, NULL);
  515. break;
  516. case LEFT: al_draw_bitmap(headLEFT, x, y, NULL);
  517. break;
  518. case UP: al_draw_bitmap(headUP, x, y, NULL);
  519. break;
  520. case DOWN: al_draw_bitmap(headDOWN, x, y, NULL);
  521. break;
  522. }
  523. al_draw_textf(font, al_map_rgb(250, 0, 250), 5, 5, 0,
  524. "Score: %i", score - 1);
  525. al_draw_textf(font, al_map_rgb(250, 0, 250), 655, 5, 0,
  526. "Time: %i", timeS);
  527. }
  528.  
  529. al_flip_display();
  530. al_clear_to_color(al_map_rgb(0, 204, 102));
  531. }
  532. }
  533. al_stop_sample(&snakeSampleID);
  534. al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
  535. }
  536. if (kwadrat == true)
  537. {
  538. ALLEGRO_BITMAP* kwadratG;
  539. const int KWADRAT_SIZE = 32;
  540. float kwadrat_x = screen_height / 2.0 - KWADRAT_SIZE / 2.0;
  541. float kwadrat_y = screen_width / 2.0 - KWADRAT_SIZE / 2.0;
  542. float kwadrat_dx = -4.0, kwadrat_dy = 4.0;
  543. bool redraw = true;
  544. kwadratG = al_create_bitmap(KWADRAT_SIZE, KWADRAT_SIZE);
  545. int x = rand() % 255 + 1;
  546. int y = rand() % 255 + 1;
  547. int z = rand() % 255 + 1;
  548.  
  549. al_set_target_bitmap(kwadratG);
  550. al_clear_to_color(al_map_rgb(x, y, z));
  551. al_set_target_bitmap(al_get_backbuffer(display));
  552. al_clear_to_color(al_map_rgb(0, 0, 0));
  553.  
  554. bool doneK = false;
  555.  
  556. al_start_timer(timerK);
  557. while (!doneK)
  558. {
  559. al_wait_for_event(event_queue, &events);
  560.  
  561. if (events.type == ALLEGRO_EVENT_TIMER) {
  562. if (kwadrat_x < 0 || kwadrat_x > screen_width - KWADRAT_SIZE) {
  563. kwadrat_dx = -kwadrat_dx;
  564. al_set_target_bitmap(kwadratG);
  565.  
  566. int x = rand() % 255 + 1;
  567. int y = rand() % 255 + 1;
  568. int z = rand() % 255 + 1;
  569.  
  570. al_clear_to_color(al_map_rgb(x, y, z));
  571.  
  572. al_set_target_bitmap(al_get_backbuffer(display));
  573. }
  574.  
  575. if (kwadrat_y < 0 || kwadrat_y > screen_height - KWADRAT_SIZE) {
  576. kwadrat_dy = -kwadrat_dy;
  577. al_set_target_bitmap(kwadratG);
  578.  
  579. int x = rand() % 255 + 1;
  580. int y = rand() % 255 + 1;
  581. int z = rand() % 255 + 1;
  582.  
  583. al_clear_to_color(al_map_rgb(x, y, z));
  584.  
  585. al_set_target_bitmap(al_get_backbuffer(display));
  586. }
  587.  
  588. kwadrat_x += kwadrat_dx;
  589. kwadrat_y += kwadrat_dy;
  590.  
  591. redraw = true;
  592. }
  593. else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  594. doneK = true;
  595. kwadrat = false;
  596. doneM = true;
  597. }
  598. if (events.type == ALLEGRO_EVENT_KEY_UP)
  599. {
  600. al_get_keyboard_state(&keyState);
  601. switch (events.keyboard.keycode) {
  602. case ALLEGRO_KEY_ESCAPE:
  603. doneK = true;
  604. kwadrat = false;
  605. menu = true;
  606. break;
  607. case ALLEGRO_KEY_ENTER:
  608. break;
  609. }
  610. }
  611.  
  612. if (redraw && al_is_event_queue_empty(event_queue)) {
  613. redraw = false;
  614.  
  615. al_clear_to_color(al_map_rgb(0, 0, 0));
  616.  
  617. al_draw_bitmap(kwadratG, kwadrat_x, kwadrat_y, 0);
  618.  
  619. al_flip_display();
  620. }
  621. }
  622. al_destroy_bitmap(kwadratG);
  623. al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
  624. }
  625. if (flip == true) {
  626. bool key[ALLEGRO_KEY_MAX];
  627. struct przeszkoda {
  628. int x, y;
  629.  
  630. const int gap = 100;
  631. };
  632.  
  633. struct ptaszek {
  634. float x, y;
  635. };
  636.  
  637. const int ile_przeszkod = 3;
  638.  
  639. struct przeszkoda p[ile_przeszkod];
  640. struct ptaszek pt;
  641. float v;
  642. al_flip_display();
  643. al_start_timer(timerF);
  644. bool przerysuj = true;
  645. bool wyjdz = false;
  646.  
  647. for (int i = 0; i < ile_przeszkod; i++) {
  648. p[i].x = i * screen_width / ile_przeszkod;
  649. p[i].y = rand() % (screen_height - 60);
  650. }
  651.  
  652. pt.x = screen_width / 4;
  653. pt.y = screen_height / 2;
  654. int scoreF = 0;
  655.  
  656. v = 0;
  657. while (!wyjdz)
  658. {
  659. al_wait_for_event(event_queue, &events);
  660.  
  661. if (events.type == ALLEGRO_EVENT_TIMER) {
  662.  
  663. przerysuj = true;
  664.  
  665. if (key[ALLEGRO_KEY_SPACE]) {
  666. v = -8;
  667. }
  668.  
  669.  
  670. for (int i = 0; i < ile_przeszkod; i++) {
  671. p[i].x = p[i].x - 2;
  672. if (p[i].x < 0) {
  673. p[i].x = screen_width;
  674. }
  675. }
  676.  
  677. v += 1;
  678.  
  679. pt.y += v;
  680.  
  681. if (pt.y > screen_height) {
  682. v = -12;
  683. }
  684.  
  685. }
  686. else if (events.type == ALLEGRO_EVENT_KEY_DOWN) {
  687. key[events.keyboard.keycode] = true;
  688. }
  689. else if (events.type == ALLEGRO_EVENT_KEY_UP) {
  690. key[events.keyboard.keycode] = false;
  691.  
  692. if (events.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
  693. wyjdz = true;
  694. menu = true;
  695. flip = false;
  696.  
  697. }
  698. }
  699.  
  700. if (przerysuj && al_is_event_queue_empty(event_queue)) {
  701. przerysuj = false;
  702.  
  703. al_clear_to_color(al_map_rgb(255, 255, 255));
  704.  
  705. for (int i = 0; i < ile_przeszkod; i++) {
  706. al_draw_filled_rectangle(p[i].x, 0, p[i].x + 60, screen_height, al_map_rgb(0, 0, 255));
  707. al_draw_filled_rectangle(p[i].x, p[i].y, p[i].x + 60, p[i].y + 200, al_map_rgb(255, 255, 255));
  708. if (pt.x >= p[i].x && pt.x < p[i].x + 1) {
  709. scoreF++;
  710. al_play_sample(birdSCORE, 0.6, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
  711. }
  712. }
  713.  
  714. al_draw_textf(font, al_map_rgb(255, 0, 0), 5, 5, 0, "Score: %i", scoreF);
  715. al_draw_bitmap(bird, pt.x, pt.y, NULL);
  716. al_flip_display();
  717. }
  718. }
  719. al_play_sample(menuSample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, &menuSampleID);
  720. }
  721.  
  722.  
  723. al_flip_display();
  724. al_draw_bitmap(background, 0, 0, 0);
  725. }
  726. }
  727.  
  728. }
  729.  
  730. }
  731.  
  732. al_destroy_display(display);
  733. al_destroy_timer(timer);
  734. al_destroy_event_queue(event_queue);
  735. al_destroy_font(introF1);
  736. al_destroy_font(menuF);
  737. al_destroy_sample(menuSample);
  738. al_destroy_font(menu_selectF);
  739. //snake
  740. al_destroy_timer(timerS);
  741. al_destroy_timer(game_time);
  742. al_destroy_timer(frameTimer);
  743. al_destroy_font(font);
  744. al_destroy_font(font1);
  745. al_destroy_bitmap(headUP);
  746. al_destroy_bitmap(headRIGHT);
  747. al_destroy_bitmap(headLEFT);
  748. al_destroy_bitmap(headDOWN);
  749. al_destroy_bitmap(bodyUP);
  750. al_destroy_bitmap(bodyDOWN);
  751. al_destroy_bitmap(bodyLEFT);
  752. al_destroy_bitmap(bodyRIGHT);
  753. al_destroy_bitmap(rabbit);
  754. al_destroy_bitmap(frog);
  755. al_destroy_sample(eat);
  756. al_destroy_sample(mushroom_eat);
  757. al_destroy_sample(snakeSample);
  758. //kwadrat
  759. al_destroy_timer(timerK);
  760. //flip
  761. al_destroy_timer(timerF);
  762. al_destroy_bitmap(bird);
  763. return 0;
  764. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement