Advertisement
podsolnyxxx

+угловые не работают. нет цвета 11

Apr 7th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include "glut.h"
  7. const int SIZE = 6;
  8. const int TILE_SIZE = 100;
  9. int board[SIZE][SIZE] = { 0 };
  10. int originalBoard[SIZE][SIZE] = { 0 }; // Переменная для хранения копии доски
  11. int score = 0; // Переменная для хранения очков
  12.  
  13. void init() {
  14. glClearColor(0.9, 0.9, 0.9, 1.0);
  15. glMatrixMode(GL_PROJECTION);
  16. glLoadIdentity();
  17. gluOrtho2D(0, SIZE * TILE_SIZE, 0, SIZE * TILE_SIZE);
  18.  
  19. glLineWidth(2.0);
  20. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  21. }
  22.  
  23. void drawTile(int x, int y, int value) {
  24. glPushMatrix();
  25. glTranslatef(x, y, 0);
  26.  
  27. switch (value) {
  28. case 2: glColor3f(0.9f, 0.9f, 0.9f); break;
  29. case 4: glColor3f(1.0f, 1.0f, 0.5f); break;
  30. case 8: glColor3f(1.0f, 0.5f, 0.0f); break;
  31. case 16: glColor3f(1.0f, 0.0f, 0.0f); break;
  32. case 32: glColor3f(0.8f, 0.6f, 1.0f); break;
  33. case 64: glColor3f(0.0f, 0.0f, 1.0f); break;
  34. case 128: glColor3f(0.0f, 1.0f, 1.0f); break;
  35. case 256: glColor3f(0.6f, 1.0f, 0.6f); break;
  36. case 512: glColor3f(0.0f, 1.0f, 0.0f); break;
  37. case 1024: glColor3f(0.0f, 0.5f, 0.0f); break;
  38. case 2048: glColor3f(1.0f, 0.0f, 1.0f); break;
  39. default: glColor3f(1.0f, 1.0f, 1.0f);
  40. }
  41.  
  42. glBegin(GL_QUADS);
  43. glVertex2i(0, 0);
  44. glVertex2i(TILE_SIZE, 0);
  45. glVertex2i(TILE_SIZE, TILE_SIZE);
  46. glVertex2i(0, TILE_SIZE);
  47. glEnd();
  48.  
  49. if (value != 0) {
  50. glColor3f(0.0f, 0.0f, 0.0f);
  51. std::string text = std::to_string(value);
  52. glRasterPos2i(TILE_SIZE / 2 - 8 * text.length(), TILE_SIZE / 2 + 8);
  53. for (char c : text) {
  54. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  55. }
  56. }
  57.  
  58. glColor3f(0.0f, 0.0f, 0.0f);
  59. glBegin(GL_LINE_LOOP);
  60. glVertex2i(0, 0);
  61. glVertex2i(TILE_SIZE, 0);
  62. glVertex2i(TILE_SIZE, TILE_SIZE);
  63. glVertex2i(0, TILE_SIZE);
  64. glEnd();
  65.  
  66. glPopMatrix();
  67. }
  68.  
  69. void generateTile() {
  70. std::vector<std::pair<int, int>> emptyCells;
  71. for (int i = 1; i < SIZE - 1; ++i) {
  72. for (int j = 1; j < SIZE - 1; ++j) {
  73. if (board[i][j] == 0) {
  74. emptyCells.push_back(std::make_pair(i, j));
  75. }
  76. }
  77. }
  78. if (emptyCells.empty()) {
  79. return;
  80. }
  81.  
  82. int value = (rand() % 10 == 0) ? 4 : 2;
  83.  
  84. int position = rand() % emptyCells.size();
  85. int x = emptyCells[position].first;
  86. int y = emptyCells[position].second;
  87. board[x][y] = value;
  88. }
  89.  
  90. bool checkWin() {
  91. for (int i = 0; i < SIZE; ++i) {
  92. for (int j = 0; j < SIZE; ++j) {
  93. if (board[i][j] == 2048) {
  94. return true;
  95. }
  96. }
  97. }
  98. return false;
  99. }
  100.  
  101. bool checkLose() {
  102. for (int i = 0; i < SIZE; ++i) {
  103. for (int j = 0; j < SIZE; ++j) {
  104. if (board[i][j] == 0) {
  105. return false;
  106. }
  107. if (j > 0 && (board[i][j] == board[i][j - 1] || board[i][j - 1] == 0)) {
  108. return false;
  109. }
  110. if (j < SIZE - 1 && (board[i][j] == board[i][j + 1] || board[i][j + 1] == 0)) {
  111. return false;
  112. }
  113. if (i > 0 && (board[i][j] == board[i - 1][j] || board[i - 1][j] == 0)) {
  114. return false;
  115. }
  116. if (i < SIZE - 1 && (board[i][j] == board[i + 1][j] || board[i + 1][j] == 0)) {
  117. return false;
  118. }
  119. }
  120. }
  121. return true;
  122. }
  123.  
  124. void moveLeft() {
  125. for (int i = 1; i < SIZE - 1; ++i) {
  126. for (int j = 1; j < SIZE - 1; ++j) {
  127. if (board[i][j] == 0) {
  128. for (int k = j + 1; k < SIZE - 1; ++k) {
  129. if (board[i][k] != 0) {
  130. board[i][j] = board[i][k];
  131. board[i][k] = 0;
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139.  
  140. void mergeLeft() {
  141. for (int i = 1; i < SIZE - 1; ++i) {
  142. for (int j = 1; j < SIZE - 1; ++j) {
  143. if (board[i][j] == board[i][j + 1] && board[i][j] != 0) {
  144. board[i][j] *= 2;
  145. board[i][j + 1] = 0;
  146. score += board[i][j];
  147. }
  148. }
  149. }
  150. }
  151.  
  152. void moveRight() {
  153. for (int i = 1; i < SIZE - 1; ++i) {
  154. for (int j = SIZE - 2; j > 0; --j) {
  155. if (board[i][j] == 0) {
  156. for (int k = j - 1; k > 0; --k) {
  157. if (board[i][k] != 0) {
  158. board[i][j] = board[i][k];
  159. board[i][k] = 0;
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167.  
  168. void mergeRight() {
  169. for (int i = 1; i < SIZE - 1; ++i) {
  170. for (int j = SIZE - 2; j > 0; --j) {
  171. if (board[i][j] == board[i][j - 1] && board[i][j] != 0) {
  172. board[i][j] *= 2;
  173. board[i][j - 1] = 0;
  174. score += board[i][j];
  175. }
  176. }
  177. }
  178. }
  179.  
  180. void moveUp() {
  181. for (int j = 1; j < SIZE - 1; ++j) {
  182. for (int i = 1; i < SIZE - 1; ++i) {
  183. if (board[i][j] == 0) {
  184. for (int k = i + 1; k < SIZE - 1; ++k) {
  185. if (board[k][j] != 0) {
  186. board[i][j] = board[k][j];
  187. board[k][j] = 0;
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195.  
  196. void mergeUp() {
  197. for (int j = 1; j < SIZE - 1; ++j) {
  198. for (int i = 1; i < SIZE - 1; ++i) {
  199. if (board[i][j] == board[i + 1][j] && board[i][j] != 0) {
  200. board[i][j] *= 2;
  201. board[i + 1][j] = 0;
  202. score += board[i][j];
  203. }
  204. }
  205. }
  206. }
  207.  
  208. void moveDown() {
  209. for (int j = 1; j < SIZE - 1; ++j) {
  210. for (int i = SIZE - 2; i > 0; --i) {
  211. if (board[i][j] == 0) {
  212. for (int k = i - 1; k > 0; --k) {
  213. if (board[k][j] != 0) {
  214. board[i][j] = board[k][j];
  215. board[k][j] = 0;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223.  
  224. void mergeDown() {
  225. for (int j = 1; j < SIZE - 1; ++j) {
  226. for (int i = SIZE - 2; i > 0; --i) {
  227. if (board[i][j] == board[i - 1][j] && board[i][j] != 0) {
  228. board[i][j] *= 2;
  229. board[i - 1][j] = 0;
  230. score += board[i][j];
  231. }
  232. }
  233. }
  234. }
  235.  
  236. void handleKeypress(unsigned char key, int x, int y) {
  237. if (key == 27) {
  238. exit(0);
  239. }
  240. }
  241.  
  242. void updateOriginalBoard() {
  243. std::memcpy(originalBoard, board, SIZE * SIZE * sizeof(int));
  244. }
  245.  
  246. void handleSpecialKeypress(int key, int x, int y) {
  247. int originalBoard[SIZE][SIZE];
  248. std::memcpy(originalBoard, board, SIZE * SIZE * sizeof(int));
  249.  
  250. switch (key) {
  251. case GLUT_KEY_LEFT:
  252. moveLeft();
  253. mergeLeft();
  254. moveLeft();
  255. break;
  256. case GLUT_KEY_RIGHT:
  257. moveRight();
  258. mergeRight();
  259. moveRight();
  260. break;
  261. case GLUT_KEY_UP:
  262. moveUp();
  263. mergeUp();
  264. moveUp();
  265. break;
  266. case GLUT_KEY_DOWN:
  267. moveDown();
  268. mergeDown();
  269. moveDown();
  270. break;
  271. }
  272.  
  273. bool moved = false;
  274. for (int i = 0; i < SIZE; ++i) {
  275. for (int j = 0; j < SIZE; ++j) {
  276. if (originalBoard[i][j] != board[i][j]) {
  277. moved = true;
  278. break;
  279. }
  280. }
  281. if (moved)
  282. break;
  283. }
  284.  
  285. if (moved) {
  286. generateTile();
  287. glutPostRedisplay();
  288. if (checkWin()) {
  289. std::cout << "You win! Your score: " << score << std::endl;
  290. exit(0);
  291. }
  292. if (checkLose()) {
  293. std::cout << "Game over! Your score: " << score << std::endl;
  294. exit(0);
  295. }
  296. }
  297. }
  298.  
  299. void drawBoard() {
  300. for (int i = 0; i < SIZE; ++i) {
  301. for (int j = 0; j < SIZE; ++j) {
  302. int x = j * TILE_SIZE;
  303. int y = (SIZE - 1 - i) * TILE_SIZE;
  304. drawTile(x, y, board[i][j]);
  305. }
  306. }
  307. }
  308.  
  309. void timer(int value) {
  310. glutTimerFunc(1000, timer, 0);
  311. glutPostRedisplay();
  312. }
  313.  
  314. void display() {
  315. glClear(GL_COLOR_BUFFER_BIT);
  316. drawBoard();
  317. glutSwapBuffers();
  318. }
  319.  
  320. void handleMouseClick(int button, int state, int x, int y) {
  321. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  322. updateOriginalBoard(); // Сохраняем оригинальную доску перед перемещением клеток
  323.  
  324. int row = SIZE - 1 - (y / TILE_SIZE);
  325. int col = x / TILE_SIZE;
  326.  
  327. if ((row == 0 && col == 0) || // левый верхний угол
  328. (row == 0 && col == SIZE - 1) || // правый верхний угол
  329. (row == SIZE - 1 && col == 0) || // левый нижний угол
  330. (row == SIZE - 1 && col == SIZE - 1)) // правый нижний угол
  331. {
  332. // Если нажата угловая клетка внешнего слоя, ничего не делаем
  333. glutPostRedisplay();
  334. return;
  335. }
  336.  
  337. if (row == 0 || row == SIZE - 1 || col == 0 || col == SIZE - 1) {
  338. // Нажатие на остальные клетки внешнего слоя
  339. if (row == 0) {
  340. // Нажатие на верхние клетки внешнего слоя, двигаем клетки вверх
  341. moveDown();
  342. mergeDown();
  343. moveDown();
  344. }
  345. else if (row == SIZE - 1) {
  346. // Нажатие на нижние клетки внешнего слоя, двигаем клетки вниз
  347. moveUp();
  348. mergeUp();
  349. moveUp();
  350. }
  351. else if (col == 0) {
  352. // Нажатие на левые клетки внешнего слоя, двигаем клетки влево
  353. moveLeft();
  354. mergeLeft();
  355. moveLeft();
  356. }
  357. else {
  358. // Нажатие на правые клетки внешнего слоя, двигаем клетки вправо
  359. moveRight();
  360. mergeRight();
  361. moveRight();
  362. }
  363.  
  364. bool moved = false;
  365. for (int i = 0; i < SIZE; ++i) {
  366. for (int j = 0; j < SIZE; ++j) {
  367. if (board[i][j] != originalBoard[i][j]) {
  368. moved = true;
  369. break;
  370. }
  371. }
  372. if (moved)
  373. break;
  374. }
  375.  
  376. if (moved) {
  377. generateTile();
  378. glutPostRedisplay();
  379. if (checkWin()) {
  380. std::cout << "You win! Your score: " << score << std::endl;
  381. exit(0);
  382. }
  383. if (checkLose()) {
  384. std::cout << "Game over! Your score: " << score << std::endl;
  385. exit(0);
  386. }
  387. }
  388. else {
  389. // Если клетки не изменили своего положения, новые клетки не появятся
  390. glutPostRedisplay();
  391. }
  392. }
  393. }
  394. }
  395.  
  396.  
  397. int main(int argc, char** argv) {
  398. srand(time(nullptr));
  399. glutInit(&argc, argv);
  400. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  401. glutInitWindowSize(SIZE * TILE_SIZE, SIZE * TILE_SIZE);
  402. glutCreateWindow("2048 Game");
  403. glutDisplayFunc(display);
  404. glutKeyboardFunc(handleKeypress);
  405. glutSpecialFunc(handleSpecialKeypress);
  406. glutMouseFunc(handleMouseClick); // Добавленная строка для обработки щелчков мыши
  407. init();
  408. generateTile();
  409. generateTile();
  410. glutTimerFunc(1000, timer, 0);
  411. glutMainLoop();
  412. return 0;
  413. }
  414.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement