-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
93 lines (73 loc) · 2.84 KB
/
Game.cpp
File metadata and controls
93 lines (73 loc) · 2.84 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
#include"Game.h"
namespace Game {
Snake* snake;
Square* apple;
bool isGrowing = false;
bool isSnakeOnScreen();
bool hasSnakeCollided();
void updateApplePos();
void handleEvents(const SDL_Keycode& key) {
switch (key) {
case SDLK_UP: snake->direction = Snake::Direction::UP; break;
case SDLK_DOWN: snake->direction = Snake::Direction::DOWN; break;
case SDLK_LEFT: snake->direction = Snake::Direction::LEFT; break;
case SDLK_RIGHT: snake->direction = Snake::Direction::RIGHT; break;
case SDLK_SPACE: snake->grow(); isGrowing = true; break;
}
}
void start() {
Main::window.changeBackgroundColor(SDL_Color{196, 196, 196});
int snakeInitialPos = GRID_SIZE * Main::window.getGridSize() / 2;
int appleInitialPos = Main::window.getGridSize() * 3;
snake = new Snake(snakeInitialPos, snakeInitialPos, Main::window.getGridSize(), Main::window.getGridSize());
apple = new Square(Main::window.getRenderer(), appleInitialPos, appleInitialPos, Main::window.getGridSize(), Main::window.getGridSize(), SDL_Color{252, 61, 61});
}
void update() {
if (isSnakeOnScreen() && (!hasSnakeCollided() || isGrowing)) {
isGrowing = false;
snake->move();
}
else {
int snakeInitialPos = GRID_SIZE * Main::window.getGridSize() / 2;
snake->reset(snakeInitialPos, snakeInitialPos);
}
if (snake->getX() == apple->getX() && snake->getY() == apple->getY()) {
snake->grow();
isGrowing = true;
updateApplePos();
}
for (const Square* square : snake->getParts()) {
Main::window.drawTexture(square->getTexture(), square->getRect());
}
Main::window.drawTexture(apple->getTexture(), apple->getRect());
}
bool isSnakeOnScreen() {
return (snake->direction == Snake::Direction::UP && snake->getY() > 0) || (snake->direction == Snake::Direction::DOWN && snake->getY() < Main::window.getHeight() - Main::window.getGridSize()) || (snake->direction == Snake::Direction::LEFT && snake->getX() > 0) || (snake->direction == Snake::Direction::RIGHT && snake->getX() < Main::window.getWidth() - Main::window.getGridSize());
}
bool hasSnakeCollided() {
const Square* const head = snake->getHead();
// Check collision with body
for (auto i = ++snake->getParts().cbegin(); i != snake->getParts().cend(); ++i) {
if (head->getX() == (*i)->getX() && head->getY() == (*i)->getY()) return true;
}
return false;
}
void updateApplePos() {
srand(time(nullptr));
int newX = rand() % GRID_SIZE * Main::window.getGridSize();
int newY = rand() % GRID_SIZE * Main::window.getGridSize();
while (true) {
bool stop = true;
for (Square* const part : snake->getParts()) {
if (part->getX() == newX && part->getY() == newY) {
newX = rand() % GRID_SIZE * Main::window.getGridSize();
newY = rand() % GRID_SIZE * Main::window.getGridSize();
stop = false;
}
}
if (stop) break;
}
apple->setX(newX);
apple->setY(newY);
}
}