Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.66 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_primitives.h>
  3. #include <allegro5/allegro_image.h>
  4. #include <allegro5/allegro_font.h>
  5. #include <allegro5/allegro_ttf.h>
  6.  
  7. #include <fstream>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <stdlib.h>
  11.  
  12.  
  13. ALLEGRO_FONT* arial;
  14. ALLEGRO_FONT* howtoarial;
  15. ALLEGRO_FONT* titlearial;
  16. ALLEGRO_FONT* bigtitlearial;
  17. ALLEGRO_FONT* scorearial;
  18. ALLEGRO_FONT* game_over_arial;
  19.  
  20. int screen_width;
  21. int screen_height;
  22. int mouse_x;
  23. int mouse_y;
  24. bool mouse_pressed = false;
  25. bool mouse_was_pressed = false;
  26. bool quit = false;
  27. bool quit_was_pressed = false;
  28.  
  29. bool key[ALLEGRO_KEY_MAX];
  30.  
  31.  
  32. class tile
  33. {
  34.  
  35. public:
  36. int number;
  37. int r,g,b;
  38. int animation;
  39. int animation_size;
  40. void init()
  41. {
  42. number = 0;
  43. }
  44. void calculate()
  45. {
  46. if (animation < 10)animation++;
  47. if (animation > 10)animation--;
  48. if (animation <= 17) {
  49. animation_size = animation;
  50. }
  51. else {
  52. animation_size = animation - 2 * (animation - 17);
  53. }
  54.  
  55. if (number > 0) {
  56. switch (number)
  57. {
  58. case 2:
  59. r = 207;
  60. g = 127;
  61. b = 117;
  62. break;
  63.  
  64. case 4:
  65. r = 77;
  66. g = 152;
  67. b = 205;
  68. break;
  69.  
  70. case 8:
  71. r = 205;
  72. g = 69;
  73. b = 221;
  74. break;
  75.  
  76. case 16:
  77. r = 17;
  78. g = 226;
  79. b = 137;
  80. break;
  81.  
  82. case 32:
  83. r = 245;
  84. g = 124;
  85. b = 175;
  86. break;
  87.  
  88. case 64:
  89. r = 158;
  90. g = 73;
  91. b = 99;
  92. break;
  93.  
  94. case 128:
  95. r = 207;
  96. g = 126;
  97. b = 103;
  98. break;
  99.  
  100. case 256:
  101. r = 99;
  102. g = 69;
  103. b = 157;
  104. break;
  105.  
  106. case 512:
  107. r = 236;
  108. g = 200;
  109. b = 80;
  110. break;
  111.  
  112. case 1024:
  113. r = 237;
  114. g = 197;
  115. b = 63;
  116. break;
  117.  
  118. case 2048:
  119. r = 238;
  120. g = 194;
  121. b = 46;
  122. break;
  123.  
  124. default:
  125. r = 62;
  126. g = 57;
  127. b = 51;
  128. break;
  129.  
  130. }
  131. }
  132. }
  133.  
  134. };
  135.  
  136. class button
  137. {
  138. public:
  139. float x;
  140. float y;
  141. float cx;
  142. float cy;
  143. float rc;
  144. std::string text;
  145. bool pressed;
  146. bool active;
  147.  
  148. void if_pressed() {
  149. if ((mouse_pressed == true) && (mouse_x > x) && (mouse_y > y) && (mouse_x < cx) && (mouse_y < cy) && (active == true)) {
  150. pressed = true;
  151. }
  152. else {
  153. pressed = false;
  154. }
  155. }
  156. void draw() {
  157. al_draw_filled_rounded_rectangle(x, y, cx, cy, rc, rc, al_map_rgb(112, 108, 99));
  158. if ((mouse_pressed == true) && (mouse_x > x) && (mouse_y > y) && (mouse_x < cx) && (mouse_y < cy) && (active == true)) {
  159. al_draw_filled_rounded_rectangle(x, y, cx, cy, rc, rc, al_map_rgb(242, 175, 123));
  160. }
  161. if (active) {
  162. al_draw_text(scorearial, al_map_rgb(255, 247, 255), x + (cx - x) / 2, y + (cy - y) / 2 - 15, ALLEGRO_ALIGN_CENTRE, text.c_str());
  163. }
  164. else {
  165. al_draw_text(scorearial, al_map_rgb(188, 172, 159), x + (cx - x) / 2, y + (cy - y) / 2 - 15, ALLEGRO_ALIGN_CENTRE, text.c_str());
  166. }
  167.  
  168. }
  169. };
  170.  
  171. bool n = false;
  172. bool s = false;
  173. bool w = false;
  174. bool e = false;
  175. bool move_done = false;
  176. bool no_move = false;
  177. bool game = false;
  178. bool menu = true;
  179. bool no_undo = true;
  180.  
  181. float square_size;
  182. float square_x;
  183. float square_y;
  184. float tile_size;
  185. float break_size;
  186. int score = 0;
  187. int highscore = 0;
  188. int undo_reload = 0;
  189. int undo_pause = 20;
  190.  
  191. std::stringstream ss;
  192.  
  193. tile tiles[4][4];
  194. int backup_tiles[4][4];
  195. int undo_backup[10][4][4];
  196.  
  197. std::string text;
  198. std::string howto;
  199. std::string title;
  200. std::string scoretxt = "Punkty";
  201. std::string scorenum;
  202. std::string highscoretxt = "Rekord";
  203. std::string highscorenum;
  204. std::string game_over = "Koniec gry";
  205.  
  206. button new_game;
  207. button undo;
  208. button settings;
  209. button play;
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. void rysuj_plansze()
  217. {
  218. al_clear_to_color(al_map_rgb(155, 247, 255));
  219. al_draw_filled_rounded_rectangle(square_x, square_y, square_x + square_size, square_y + square_size, square_size / 50, square_size / 50, al_map_rgb(188, 111, 219));
  220.  
  221.  
  222. for (int a = 0; a < 4; a++) {
  223. for (int b = 0; b < 4; b++) {
  224. al_draw_filled_rounded_rectangle(square_x + a * break_size + break_size + a * tile_size, square_y + b * break_size + break_size + b * tile_size, square_x + a * break_size + break_size + a * tile_size + tile_size, square_y + b * break_size + break_size + b * tile_size + tile_size, square_size / 50, square_size / 50, al_map_rgb(0, 144, 0));
  225. }
  226. }
  227.  
  228. al_draw_text(howtoarial, al_map_rgb(112, 108, 99), square_x / 2, square_y + tile_size - break_size, ALLEGRO_ALIGN_CENTRE, howto.c_str());
  229. al_draw_text(titlearial, al_map_rgb(112, 108, 99), square_x / 2, square_y - break_size * 2, ALLEGRO_ALIGN_CENTRE, title.c_str());
  230.  
  231. new_game.draw();
  232.  
  233. undo.draw();
  234.  
  235.  
  236.  
  237.  
  238.  
  239. al_draw_filled_rounded_rectangle(square_x + square_size + break_size * 4, square_y + break_size, screen_width - break_size * 4, square_y + tile_size + break_size, square_size / 50, square_size / 50, al_map_rgb(112, 108, 99));
  240. al_draw_text(scorearial, al_map_rgb(255, 247, 255), square_x + square_size + square_x / 2, square_y + break_size * 2, ALLEGRO_ALIGN_CENTRE, scoretxt.c_str());
  241. ss.clear();
  242. ss << score;
  243. ss >> scorenum;
  244. al_draw_text(scorearial, al_map_rgb(255, 247, 255), square_x + square_size + square_x / 2, square_y + break_size + tile_size / 2, ALLEGRO_ALIGN_CENTRE, scorenum.c_str());
  245.  
  246.  
  247.  
  248.  
  249. al_draw_filled_rounded_rectangle(square_x + square_size + break_size * 4, square_y + break_size * 2 + tile_size, screen_width - break_size * 4, square_y + tile_size * 2 + break_size * 2, square_size / 50, square_size / 50, al_map_rgb(112, 108, 99));
  250. al_draw_text(scorearial, al_map_rgb(255, 247, 255), square_x + square_size + square_x / 2, square_y + break_size * 3 + tile_size, ALLEGRO_ALIGN_CENTRE, highscoretxt.c_str());
  251. ss.clear();
  252. ss << highscore;
  253. ss >> highscorenum;
  254. al_draw_text(scorearial, al_map_rgb(255, 247, 255), square_x + square_size + square_x / 2, square_y + break_size * 2 + tile_size / 2 + tile_size, ALLEGRO_ALIGN_CENTRE, highscorenum.c_str());
  255.  
  256.  
  257.  
  258.  
  259.  
  260. for (int a = 0; a < 4; a++) {
  261. for (int b = 0; b < 4; b++) {
  262. if (tiles[a][b].number > 0) {
  263. al_draw_filled_rounded_rectangle(square_x + a * break_size + break_size + a * tile_size + 10 * tile_size / 20 - tiles[a][b].animation_size * tile_size / 20, square_y + b * break_size + break_size + b * tile_size + 10 * tile_size / 20 - tiles[a][b].animation_size * tile_size / 20, square_x + a * break_size + break_size + a * tile_size + tile_size - 10 * tile_size / 20 + tiles[a][b].animation_size * tile_size / 20, square_y + b * break_size + break_size + b * tile_size + tile_size - 10 * tile_size / 20 + tiles[a][b].animation_size * tile_size / 20, square_size / 50, square_size / 50, al_map_rgb(tiles[a][b].r, tiles[a][b].g, tiles[a][b].b));
  264. ss.clear();
  265. ss << tiles[a][b].number;
  266. ss >> text;
  267. if (tiles[a][b].number > 4) {
  268. al_draw_text(arial, al_map_rgb(255, 247, 255), square_x + tile_size * a + break_size * a + break_size + tile_size / 2, square_y + tile_size * b + break_size * b + break_size + tile_size / 2 - 25, ALLEGRO_ALIGN_CENTRE, text.c_str());
  269. }
  270. else {
  271. al_draw_text(arial, al_map_rgb(125, 114, 110), square_x + tile_size * a + break_size * a + break_size + tile_size / 2, square_y + tile_size * b + break_size * b + break_size + tile_size / 2 - 25, ALLEGRO_ALIGN_CENTRE, text.c_str());
  272. }
  273. }
  274. }
  275. }
  276. if (no_move) {
  277. al_draw_filled_rounded_rectangle(square_x + 4 * break_size, screen_height / 2 - tile_size + break_size, square_x + square_size - 4 * break_size, screen_height / 2 + tile_size - break_size, square_size / 50, square_size / 50, al_map_rgba(112, 108, 99, 200));
  278. al_draw_text(game_over_arial, al_map_rgb(255, 247, 255), screen_width / 2, screen_height / 2 - 30, ALLEGRO_ALIGN_CENTRE, game_over.c_str());
  279. }
  280.  
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288. //
  289. // Mechanika gry
  290. //
  291. void make_backup() {
  292. for (int a = 0; a < 4; a++) {
  293. for (int b = 0; b < 4; b++) {
  294. backup_tiles[a][b] = tiles[a][b].number;
  295. }
  296. }
  297. }
  298.  
  299. void gen_new() {
  300. int a;
  301. int b;
  302. a = rand() % 4;
  303. b = rand() % 4;
  304. if (tiles[a][b].number == 0) {
  305. if (rand() % 3 == 0) {
  306. tiles[a][b].number = 4;
  307. tiles[a][b].animation = 0;
  308. }
  309. else {
  310. tiles[a][b].number = 2;
  311. tiles[a][b].animation = 0;
  312. }
  313. }
  314. else {
  315. gen_new();
  316. }
  317. }
  318.  
  319. void quit_fix() {
  320. if (quit) {
  321. if (quit_was_pressed) {
  322. quit = false;
  323. quit_was_pressed = false;
  324. }
  325. else {
  326. quit_was_pressed = true;
  327. }
  328. }
  329. }
  330.  
  331. void mouse_fix() {
  332. if (mouse_pressed) {
  333. if (mouse_was_pressed) {
  334. mouse_pressed = false;
  335. mouse_was_pressed = false;
  336. }
  337. else {
  338. mouse_was_pressed = true;
  339. }
  340. }
  341. }
  342.  
  343. void n_move(int a, int b)
  344. {
  345. if (!((b - 1) < 0)) {
  346. if (tiles[a][b - 1].number == 0) {
  347. tiles[a][b - 1].number = tiles[a][b].number;
  348. tiles[a][b].number = 0;
  349. tiles[a][b - 1].animation = tiles[a][b].animation;
  350. tiles[a][b].animation = 0;
  351. n_move(a, b - 1);
  352. }
  353. }
  354. }
  355.  
  356. void s_move(int a, int b)
  357. {
  358. if (!((b + 1) > 3)) {
  359. if (tiles[a][b + 1].number == 0) {
  360. tiles[a][b + 1].number = tiles[a][b].number;
  361. tiles[a][b].number = 0;
  362. tiles[a][b + 1].animation = tiles[a][b].animation;
  363. tiles[a][b].animation = 0;
  364. s_move(a, b + 1);
  365. }
  366. }
  367. }
  368.  
  369. void w_move(int a, int b)
  370. {
  371. if (!((a - 1) < 0)) {
  372. if (tiles[a - 1][b].number == 0) {
  373. tiles[a - 1][b].number = tiles[a][b].number;
  374. tiles[a][b].number = 0;
  375. tiles[a - 1][b].animation = tiles[a][b].animation;
  376. tiles[a][b].animation = 0;
  377. w_move(a - 1, b);
  378. }
  379. }
  380. }
  381.  
  382. void e_move(int a, int b)
  383. {
  384. if (!((a + 1) > 3)) {
  385. if (tiles[a + 1][b].number == 0) {
  386. tiles[a + 1][b].number = tiles[a][b].number;
  387. tiles[a][b].number = 0;
  388. tiles[a + 1][b].animation = tiles[a][b].animation;
  389. tiles[a][b].animation = 0;
  390. e_move(a + 1, b);
  391. }
  392. }
  393. }
  394.  
  395. void vertical(int a, int w)
  396. {
  397. if (w == 0) {
  398. for (int b = 0; b < 4; b++) {
  399. n_move(a, b);
  400. }
  401. }
  402. else {
  403. for (int b = 3; b >= 0; b--) {
  404. s_move(a, b);
  405. }
  406. }
  407.  
  408. if (w == 0) {
  409. for (int b = 0; b < 4; b++) {
  410. if (!((b - 1) < 0)) {
  411. if (tiles[a][b - 1].number == tiles[a][b].number) {
  412. score += (tiles[a][b - 1].number + tiles[a][b].number);
  413. tiles[a][b - 1].number += tiles[a][b].number;
  414. tiles[a][b - 1].animation = 24;
  415. tiles[a][b].number = 0;
  416. }
  417. }
  418. }
  419. }
  420. else {
  421. for (int b = 3; b >= 0; b--) {
  422. if (!((b + 1) > 3)) {
  423. if (tiles[a][b + 1].number == tiles[a][b].number) {
  424. score += (tiles[a][b + 1].number + tiles[a][b].number);
  425. tiles[a][b + 1].number += tiles[a][b].number;
  426. tiles[a][b + 1].animation = 24;
  427. tiles[a][b].number = 0;
  428. }
  429. }
  430. }
  431. }
  432.  
  433. if (w == 0) {
  434. for (int b = 0; b < 4; b++) {
  435. n_move(a, b);
  436. }
  437. }
  438. else {
  439. for (int b = 3; b >= 0; b--) {
  440. s_move(a, b);
  441. }
  442. }
  443. }
  444.  
  445. void horizontal(int b, int w)
  446. {
  447. if (w == 0) {
  448. for (int a = 0; a < 4; a++) {
  449. w_move(a, b);
  450. }
  451. }
  452. else {
  453. for (int a = 3; a >= 0; a--) {
  454. e_move(a, b);
  455. }
  456. }
  457.  
  458. if (w == 0) {
  459. for (int a = 0; a < 4; a++) {
  460. if (!((a - 1) < 0)) {
  461. if (tiles[a - 1][b].number == tiles[a][b].number) {
  462. score += (tiles[a - 1][b].number + tiles[a][b].number);
  463. tiles[a - 1][b].number += tiles[a][b].number;
  464. tiles[a - 1][b].animation = 24;
  465. tiles[a][b].number = 0;
  466. }
  467. }
  468. }
  469. }
  470. else {
  471. for (int a = 3; a >= 0; a--) {
  472. if (!((a + 1) > 3)) {
  473. if (tiles[a + 1][b].number == tiles[a][b].number) {
  474. score += (tiles[a + 1][b].number + tiles[a][b].number);
  475. tiles[a + 1][b].number += tiles[a][b].number;
  476. tiles[a + 1][b].animation = 24;
  477. tiles[a][b].number = 0;
  478. }
  479. }
  480. }
  481. }
  482.  
  483. if (w == 0) {
  484. for (int a = 0; a < 4; a++) {
  485. w_move(a, b);
  486. }
  487. }
  488. else {
  489. for (int a = 3; a >= 0; a--) {
  490. e_move(a, b);
  491. }
  492. }
  493. }
  494.  
  495.  
  496. void if_no_move() {
  497. no_move = true;
  498. for (int a = 0; a < 4; a++) {
  499. for (int b = 0; b < 4; b++) {
  500. if (!((a - 1) < 0)) {
  501. if ((tiles[a - 1][b].number == 0) || (tiles[a - 1][b].number == tiles[a][b].number)) {
  502. no_move = false;
  503. }
  504. }
  505. if (!((a + 1) > 3)) {
  506. if ((tiles[a + 1][b].number == 0) || (tiles[a + 1][b].number == tiles[a][b].number)) {
  507. no_move = false;
  508. }
  509. }
  510. if (!((b - 1) < 0)) {
  511. if ((tiles[a][b - 1].number == 0) || (tiles[a][b - 1].number == tiles[a][b].number)) {
  512. no_move = false;
  513. }
  514. }
  515. if (!((b + 1) > 3)) {
  516. if ((tiles[a][b + 1].number == 0) || (tiles[a][b + 1].number == tiles[a][b].number)) {
  517. no_move = false;
  518. }
  519. }
  520. }
  521. }
  522. }
  523.  
  524. void reset()
  525. {
  526. for (int a = 0; a < 4; a++) {
  527. for (int b = 0; b < 4; b++) {
  528. tiles[a][b].init();
  529. }
  530. }
  531. gen_new();
  532. gen_new();
  533. score = 0;
  534. make_backup();
  535. }
  536.  
  537. void aktualizuj_plansze()
  538. {
  539. make_backup();
  540.  
  541. new_game.if_pressed();
  542. undo.if_pressed();
  543.  
  544. if (n) {
  545. n = false;
  546. make_backup();
  547. for (int a = 0; a < 4; a++) {
  548. vertical(a, 0);
  549. }
  550. }
  551. if (s) {
  552. s = false;
  553. make_backup();
  554. for (int a = 0; a < 4; a++) {
  555. vertical(a, 1);
  556. }
  557. }
  558. if (w) {
  559. w = false;
  560. make_backup();
  561. for (int b = 0; b < 4; b++) {
  562. horizontal(b, 0);
  563. }
  564. }
  565. if (e) {
  566. e = false;
  567. make_backup();
  568. for (int b = 0; b < 4; b++) {
  569. horizontal(b, 1);
  570. }
  571. }
  572.  
  573. for (int a = 0; a < 4; a++) {
  574. for (int b = 0; b < 4; b++) {
  575. if (!(backup_tiles[a][b] == tiles[a][b].number)) {
  576. move_done = true;
  577. }
  578. }
  579. }
  580.  
  581. if (move_done) {
  582. gen_new();
  583. for (int i = 8; i >= 0; i--) {
  584. for (int a = 0; a < 4; a++) {
  585. for (int b = 0; b < 4; b++) {
  586. undo_backup[i + 1][a][b] = undo_backup[i][a][b];
  587. }
  588. }
  589. }
  590. for (int a = 0; a < 4; a++) {
  591. for (int b = 0; b < 4; b++) {
  592. undo_backup[0][a][b] = tiles[a][b].number;
  593. }
  594. }
  595. if (no_undo == true)no_undo = false;
  596.  
  597. if (undo_reload < undo_pause)undo_reload++;
  598. if (undo_reload == undo_pause)undo.active = true;
  599. else undo.active = false;
  600. }
  601. move_done = false;
  602.  
  603. std::ifstream highscore_in(".highscore.txt");
  604. highscore_in >> highscore;
  605. highscore_in.close();
  606. if (score > highscore) {
  607. highscore = score;
  608. std::ofstream highscore_out(".highscore.txt");
  609. highscore_out << score;
  610. highscore_out.close();
  611. }
  612.  
  613.  
  614. if (undo.pressed && no_undo == false) {
  615. for (int a = 0; a < 4; a++) {
  616. for (int b = 0; b < 4; b++) {
  617. tiles[a][b].number = undo_backup[1][a][b];
  618. }
  619. }
  620. for (int i = 1; i < 10; i++) {
  621. for (int a = 0; a < 4; a++) {
  622. for (int b = 0; b < 4; b++) {
  623. undo_backup[i - 1][a][b] = undo_backup[i][a][b];
  624. }
  625. }
  626. }
  627. for (int a = 0; a < 4; a++) {
  628. for (int b = 0; b < 4; b++) {
  629. undo_backup[9][a][b] = 0;
  630. }
  631. }
  632. undo_reload = 0;
  633. undo.active = false;
  634. }
  635.  
  636. if (new_game.pressed == true) {
  637. reset();
  638. }
  639.  
  640. if_no_move();
  641.  
  642. for (int a = 0; a < 4; a++) {
  643. for (int b = 0; b < 4; b++) {
  644. tiles[a][b].calculate();
  645. }
  646. }
  647.  
  648. mouse_fix();
  649.  
  650. }
  651.  
  652. //
  653. // Reakcja na akcje graczy
  654. //
  655.  
  656. void co_robia_gracze()
  657. {
  658. }
  659.  
  660. void rysuj_menu() {
  661. al_clear_to_color(al_map_rgb(255, 247, 255));
  662. al_draw_text(bigtitlearial, al_map_rgb(112, 108, 99), screen_width / 2, break_size * 4, ALLEGRO_ALIGN_CENTRE, title.c_str());
  663. play.draw();
  664. }
  665.  
  666. void aktualizuj_menu() {
  667. mouse_fix();
  668. play.if_pressed();
  669. if (play.pressed) {
  670. menu = false;
  671. game = true;
  672. }
  673. }
  674.  
  675. void rysuj_ekran() {
  676. if (game && !menu)rysuj_plansze();
  677. if (menu && !game)rysuj_menu();
  678. }
  679.  
  680.  
  681. void init_game_buttons() {
  682.  
  683.  
  684.  
  685.  
  686. new_game.x = break_size * 4;
  687. new_game.y = square_y + break_size * 5 + tile_size * 2;
  688. new_game.cx = square_x - break_size * 4;
  689. new_game.cy = square_y + break_size * 1 + tile_size * 3;
  690. new_game.rc = square_size / 50;
  691. new_game.text = "Nowa Gra";
  692. new_game.pressed = false;
  693. new_game.active = true;
  694.  
  695.  
  696.  
  697.  
  698. undo.x = break_size * 4;
  699. undo.y = square_y + break_size * 6 + tile_size * 3;
  700. undo.cx = square_x - break_size * 4;
  701. undo.cy = square_y + break_size * 2 + tile_size * 4;
  702. undo.rc = square_size / 50;
  703. undo.text = "Wstecz";
  704. undo.pressed = false;
  705. undo.active = false;
  706. }
  707.  
  708. void init_menu_buttons() {
  709.  
  710.  
  711.  
  712.  
  713. play.x = screen_width / 2 - (square_x + 8 * break_size) / 2;
  714. play.y = screen_height / 2 - tile_size / 2 + break_size * 2;
  715. play.cx = screen_width / 2 + (square_x + 8 * break_size) / 2;
  716. play.cy = screen_height / 2 + tile_size / 2 - break_size * 2;
  717. play.rc = square_size / 250;
  718. play.text = "Graj";
  719. play.pressed = false;
  720. play.active = true;
  721.  
  722.  
  723. }
  724.  
  725.  
  726. const float FPS = 50;
  727.  
  728. ALLEGRO_DISPLAY_MODE disp_data;
  729. ALLEGRO_DISPLAY* display;
  730. ALLEGRO_EVENT_QUEUE* event_queue = NULL;
  731. ALLEGRO_TIMER* timer = NULL;
  732.  
  733. int init()
  734. {
  735. if (!al_init()) {
  736. std::cerr << "Błąd podczas inicjalizacji allegro." << std::endl;
  737. return -1;
  738. }
  739.  
  740. if (!al_init_primitives_addon()) {
  741. std::cerr << "Błąd podczas inicjalizacji dodatku 'primitives'." << std::endl;
  742. return -1;
  743. }
  744.  
  745. if (!al_install_keyboard()) {
  746. std::cerr << "Błąd podczas inicjalizacji klawiatury." << std::endl;
  747. return -1;
  748. }
  749.  
  750. if (!al_install_mouse()) {
  751. std::cerr << "Błąd podczas inicjalizacji myszy." << std::endl;
  752. return -1;
  753. }
  754.  
  755. if (!al_init_image_addon()) {
  756. std::cerr << "Błąd podczas inicjalizacji dodatku image." << std::endl;
  757. return -1;
  758. }
  759.  
  760. al_init_font_addon();
  761.  
  762. if (!al_init_ttf_addon()) {
  763. std::cerr << "Błąd podczas inicjalizacji dodatku ttf." << std::endl;
  764. return -1;
  765. }
  766.  
  767.  
  768. timer = al_create_timer(1.0 / FPS);
  769. if (!timer) {
  770. std::cerr << "Błąd podczas inicjalizacji zegara." << std::endl;
  771. return -1;
  772. }
  773.  
  774. al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
  775. al_get_display_mode(0, &disp_data);
  776. screen_width = disp_data.width;
  777. screen_height = disp_data.height;
  778.  
  779.  
  780. screen_width = 1024;
  781. screen_height = 800;
  782.  
  783. square_size = screen_height * 3 / 4;
  784. square_x = (screen_width - square_size) / 2;
  785. square_y = screen_height / 8;
  786. break_size = square_size / 50;
  787. tile_size = (square_size - 5 * break_size) / 4;
  788. howtoarial = al_load_ttf_font("Arial.ttf", 20, 0);
  789. arial = al_load_ttf_font("ArialBlack.ttf", 38, 0);
  790. scorearial = al_load_ttf_font("ArialBlack.ttf", 20, 0);
  791. game_over_arial = al_load_ttf_font("ArialBlack.ttf", 38, 0);
  792. titlearial = al_load_ttf_font("ArialBlack.ttf", tile_size * 7 / 8, 0);
  793. bigtitlearial = al_load_ttf_font("ArialBlack.ttf", tile_size * 1.5, 0);
  794. for (int i = 9; i >= 0; i--) {
  795. for (int a = 0; a < 4; a++) {
  796. for (int b = 0; b < 4; b++) {
  797. undo_backup[i][a][b] = 0;
  798. }
  799. }
  800. }
  801. init_game_buttons();
  802. init_menu_buttons();
  803.  
  804.  
  805. display = al_create_display(screen_width, screen_height);
  806. if (!display) {
  807. std::cerr << "Błąd podczas inicjalizacji ekranu." << std::endl;
  808. al_destroy_timer(timer);
  809. return -1;
  810. }
  811.  
  812. event_queue = al_create_event_queue();
  813. if (!event_queue) {
  814. std::cerr << "Błąd podczas inicjalizacji kolejki zdarzeń." << std::endl;
  815. al_destroy_display(display);
  816. al_destroy_timer(timer);
  817. return -1;
  818. }
  819.  
  820. al_register_event_source(event_queue, al_get_display_event_source(display));
  821. al_register_event_source(event_queue, al_get_timer_event_source(timer));
  822. al_register_event_source(event_queue, al_get_keyboard_event_source());
  823. al_register_event_source(event_queue, al_get_mouse_event_source());
  824. al_clear_to_color(al_map_rgb(0, 0, 0));
  825.  
  826. al_flip_display();
  827. al_start_timer(timer);
  828.  
  829. for (int a = 0; a < 4; a++) {
  830. for (int b = 0; b < 4; b++) {
  831. tiles[a][b].init();
  832. }
  833. }
  834.  
  835. return 0;
  836. }
  837.  
  838. int main(int argc, char** argv)
  839. {
  840. if (init() != 0) {
  841. std::cerr << "Inicjalizacja nie powiodła się." << std::endl;
  842. return -1;
  843. }
  844.  
  845. bool przerysuj = true;
  846.  
  847. gen_new();
  848. gen_new();
  849. make_backup();
  850.  
  851.  
  852.  
  853. for (;;)
  854. {
  855. ALLEGRO_EVENT ev;
  856. al_wait_for_event(event_queue, &ev);
  857.  
  858. if (ev.type == ALLEGRO_EVENT_TIMER) {
  859.  
  860. przerysuj = true;
  861.  
  862. if (game && !menu) {
  863. co_robia_gracze();
  864.  
  865. aktualizuj_plansze();
  866. }
  867.  
  868. if (menu && !game) {
  869. aktualizuj_menu();
  870. rysuj_menu();
  871. }
  872.  
  873.  
  874. }
  875. else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
  876. key[ev.keyboard.keycode] = true;
  877. }
  878. else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
  879. key[ev.keyboard.keycode] = false;
  880.  
  881. if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
  882. quit = true;
  883. }
  884. if (ev.keyboard.keycode == ALLEGRO_KEY_UP) {
  885. n = true;
  886. }
  887. if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN) {
  888. s = true;
  889. }
  890. if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT) {
  891. w = true;
  892. }
  893. if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
  894. e = true;
  895. }
  896. }
  897. else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  898. quit = true;
  899. }
  900. else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES || ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) {
  901. mouse_x = ev.mouse.x;
  902. mouse_y = ev.mouse.y;
  903. }
  904. else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
  905. mouse_pressed = false;
  906. }
  907. else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
  908. mouse_pressed = true;
  909. }
  910.  
  911. if (przerysuj && al_is_event_queue_empty(event_queue)) {
  912. przerysuj = false;
  913.  
  914. rysuj_ekran();
  915.  
  916. al_flip_display();
  917.  
  918. }
  919.  
  920. quit_fix();
  921. if (quit && menu && !game)break;
  922. if (quit && !menu && game) {
  923. menu = true;
  924. game = false;
  925. }
  926. }
  927.  
  928. return 0;
  929. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement