-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake Game.cpp
240 lines (194 loc) · 6.01 KB
/
Snake Game.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Snake Game.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include "raylib.h"
#include "raymath.h"
#include <deque> //data structure
using namespace std;
Color Green = { 173,204,69,255 };
Color Dark_Green = { 43,51,24,255 };
//size of screen
int cell_size = 30;
int cell_count = 25;
int offset = 75; //width of edge borders
double lastUpdateTime = 0;
//set the speed of the snake
bool eventTrigger(double interval) {
double currentTime = GetTime();
if (currentTime - lastUpdateTime >= interval) {
lastUpdateTime = currentTime;
return true;
}
return false;
}
//check if food generated is on the snake's body
bool ElementInDeque(Vector2 element,deque<Vector2> deque) {
for (unsigned int i = 0; i < deque.size(); i++) {
if (Vector2Equals(deque[i], element)) {
return true;
}
}
return false;
}
class Snake {
public:
deque<Vector2> body = { Vector2{6, 9}, Vector2{5, 9}, Vector2{4, 9} };
Vector2 direction = { 1,0 };
void Draw();
void Update();
bool addSegment = false;
void Reset();
};
void Snake::Draw() {
for (unsigned int i = 0; i < body.size(); i++) {
float x = body[i].x;
float y = body[i].y;
Rectangle segment = Rectangle{offset+ x * cell_size,offset+ y * cell_size, (float)cell_size, (float)cell_size };
DrawRectangleRounded(segment, 0.5, 6, Dark_Green);
}
}
void Snake::Update() {
body.push_front(Vector2Add(body[0], direction)); //add a segment when food is eaten.
if (addSegment == true) {
addSegment = false;
}
else {
body.pop_back();
}
}
void Snake::Reset() {
body = { Vector2{6,9},Vector2{5,9},Vector2{4,9} };
direction = { 1,0 };
}
class Food {
public:
Vector2 position;
Texture2D texture;
Food(deque<Vector2> snakeBody) {
Image image = LoadImage("graphics/food.png");
texture = LoadTextureFromImage(image);
UnloadImage(image);
position = GenerateRandomPos(snakeBody);
}
~Food() {
UnloadTexture(texture);
}
void Draw();
Vector2 GenerateRandomCell() {
float x = GetRandomValue(0, cell_count - 1);
float y = GetRandomValue(0, cell_count - 1);
return Vector2{ x, y };
}
Vector2 GenerateRandomPos(deque<Vector2> snakeBody) {
Vector2 position = GenerateRandomCell();
//if food gen is on the snake's body, gen a new food elsewhere
while (ElementInDeque(position, snakeBody)) {
position = GenerateRandomCell();
}
return position;
}
};
void Food::Draw() {
DrawTexture(texture, offset+ position.x * cell_size, offset+ position.y * cell_size, WHITE);
}
class Game {
public:
Snake snake = Snake();
Food food = Food(snake.body);
bool running = true;
int score = 0;
Sound eatSound;
Sound wallSound;
Game() {
InitAudioDevice();
eatSound = LoadSound("sounds/eat.mp3");
wallSound = LoadSound("sounds/wall.mp3");
}
~Game() {
UnloadSound(eatSound);
UnloadSound(wallSound);
CloseAudioDevice();
}
void Draw() {
food.Draw();
snake.Draw();
}
void Update() {
if (running) {
snake.Update();
CheckCollisionFood();
CheckCollisionWithEdges();
CheckCollisionWithTail();
}
}
void CheckCollisionFood() {
if (Vector2Equals(snake.body[0], food.position)) {
food.position = food.GenerateRandomPos(snake.body);
snake.addSegment = true;
score++;
PlaySound(eatSound);
}
}
void CheckCollisionWithEdges() {
//snake.body[0].x == cell_count; //pass beyond right edge of grid
//snake.body[0].x == -1; //pass the left edge of grid
if (snake.body[0].x == cell_count || snake.body[0].x == -1) { //horizontal limit
GameOver();
}
if (snake.body[0].y == cell_count || snake.body[0].y == -1) { //vertical limit
GameOver();
}
}
void CheckCollisionWithTail() {
deque<Vector2> headlessBody = snake.body; //copy of snake body without head named headlessBody
headlessBody.pop_front(); //remove head
if (ElementInDeque(snake.body[0], headlessBody)) { //if snake head in headleeBody deque, collided with tail
GameOver();
}
}
void GameOver() {
snake.Reset();
food.position = food.GenerateRandomPos(snake.body);
running = false;
score = 0;
PlaySound(wallSound);
}
};
int main()
{
cout << "Starting the game..." << endl;
InitWindow(2 * offset + cell_size * cell_count, 2 * offset + cell_size * cell_count, "Retro Snake");
SetTargetFPS(60);
Game game;
while (!WindowShouldClose()) {
BeginDrawing();
if (eventTrigger(0.2)) {
game.Update();
}
if (IsKeyPressed(KEY_UP) && game.snake.direction.y != 1) {
game.snake.direction = { 0,-1 };
game.running = true;
}
if (IsKeyPressed(KEY_DOWN) && game.snake.direction.y != -1) {
game.snake.direction = { 0,1 };
game.running = true;
}
if (IsKeyPressed(KEY_LEFT) && game.snake.direction.x != 1) {
game.snake.direction = { -1,0 };
game.running = true;
}
if (IsKeyPressed(KEY_RIGHT) && game.snake.direction.x != -1) {
game.snake.direction = { 1,0 };
game.running = true;
}
//Drawing
ClearBackground(Green);
DrawRectangleLinesEx(Rectangle{ (float)offset - 5,(float)offset - 5,(float)cell_size * cell_count + 10,(float)cell_size * cell_count + 10 }, 5, Dark_Green);
DrawText("Retro Snake", offset - 5, 20, 40, Dark_Green);
DrawText(TextFormat("%i", game.score), offset - 5, offset + cell_size * cell_count + 10, 40, Dark_Green);
game.Draw();
EndDrawing();
}
CloseWindow();
return 0;
}