-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
144 lines (120 loc) · 3.08 KB
/
Copy pathGame.cpp
File metadata and controls
144 lines (120 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by mario on 2/13/24.
//
#include "Game.h"
#include <cstdlib>
#include <ctime>
Snake Game::snake;
int Game::appleX, Game::appleY;
int Game::score;
char Game::board[26][26];
void Game::getSnake() {
Node *node = snake.getHead();
board[node->x][node->y] = 'O';
node = node->next;
while (node) {
board[node->x][node->y] = '#';
node = node->next;
}
}
void Game::render() {
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
board[i][j] = ' ';
}
}
getSnake();
board[appleX][appleY] = '*';
cout << "Score:" << score << endl;
cout << " ";
for (int i = 0; i <= 51; ++i) {
cout << "_";
}
cout << endl;
for (int i = 0; i < 26; ++i) {
cout << "|";
for (int j = 0; j < 26; ++j) {
if (board[i][j] == '*')
cout << "\033[1;31m" << board[i][j] << "\033[0m" << " ";
else if (board[i][j] == '#')
cout << "\033[1;32m" << board[i][j] << "\033[0m" << " ";
else if (board[i][j] == 'O')
cout << "\033[1;33m" << board[i][j] << "\033[0m" << " ";
else
cout << board[i][j] << " ";
}
cout << "|";
cout << endl;
}
cout << " ";
for (int i = 0; i <= 51; ++i) {
cout << "_";
}
cout << endl;
}
bool Game::step() {
Node *node = snake.getTail();
snake.setTail(node->x, node->y);
while (node->prev != nullptr) {
node->x = node->prev->x;
node->y = node->prev->y;
node = node->prev;
}
node->x = (node->x + snake.getX()) % 26;
if (node->x == -1)
node->x = 25;
node->y = (node->y + snake.getY()) % 26;
if (node->y == -1)
node->y = 25;
if (board[node->x][node->y] == '#' && (node->x != snake.getCx() || node->y != snake.getCy())) {
return false;
}
if (board[node->x][node->y] == '*') {
snake.eat();
int apple = rand() % 625;
appleX = apple / 25;
appleY = apple % 25;
score++;
}
return true;
}
void Game::start() {
step();
snake.eat();
step();
snake.eat();
step();
snake.eat();
step();
snake.eat();
srand(time(0));
int apple = rand() % 625;
appleX = apple / 25;
appleY = apple % 25;
score = 0;
int ok = true;
set_raw_mode(true);
while (ok) {
int ch = quick_read();
if (ch == ERR);
else if (ch == 'w') {
if (snake.getDirection() != 2)
snake.move(1);
} else if (ch == 's') {
if (snake.getDirection() != 1)
snake.move(2);
} else if (ch == 'a') {
if (snake.getDirection() != 3)
snake.move(4);
} else if (ch == 'd') {
if (snake.getDirection() != 4)
snake.move(3);
} else if (ch == 'e') { snake.eat(); }
ok = step();
render();
usleep(150000);
cout << "\033[H\033[2J\033[3J";
}
cout << "Game over!" << endl;
cout << "Your score:" << score << endl;
}