-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
260 lines (227 loc) · 5.78 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <raylib.h>
#include <deque>
#include <raymath.h>
using namespace std;
static bool allowMove = false;
Color green = {173, 204, 96, 255};
Color darkGreen = {43, 51, 24, 255};
int cellSize = 30;
int cellCount = 25;
int offset = 75;
double lastUpdateTime = 0;
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;
}
bool EventTriggered(double interval)
{
double currentTime = GetTime();
if (currentTime - lastUpdateTime >= interval)
{
lastUpdateTime = currentTime;
return true;
}
return false;
}
class Snake
{
public:
deque<Vector2> body = {Vector2{6, 9}, Vector2{5, 9}, Vector2{4, 9}};
Vector2 direction = {1, 0};
bool addSegment = false;
void 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 * cellSize, offset + y * cellSize, (float)cellSize, (float)cellSize};
DrawRectangleRounded(segment, 0.5, 6, darkGreen);
}
}
void Update()
{
body.push_front(Vector2Add(body[0], direction));
if (addSegment == true)
{
addSegment = false;
}
else
{
body.pop_back();
}
}
void 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()
{
DrawTexture(texture, offset + position.x * cellSize, offset + position.y * cellSize, WHITE);
}
Vector2 GenerateRandomCell()
{
float x = GetRandomValue(0, cellCount - 1);
float y = GetRandomValue(0, cellCount - 1);
return Vector2{x, y};
}
Vector2 GenerateRandomPos(deque<Vector2> snakeBody)
{
Vector2 position = GenerateRandomCell();
while (ElementInDeque(position, snakeBody))
{
position = GenerateRandomCell();
}
return position;
}
};
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();
CheckCollisionWithFood();
CheckCollisionWithEdges();
CheckCollisionWithTail();
}
}
void CheckCollisionWithFood()
{
if (Vector2Equals(snake.body[0], food.position))
{
food.position = food.GenerateRandomPos(snake.body);
snake.addSegment = true;
score++;
PlaySound(eatSound);
}
}
void CheckCollisionWithEdges()
{
if (snake.body[0].x == cellCount || snake.body[0].x == -1)
{
GameOver();
}
if (snake.body[0].y == cellCount || snake.body[0].y == -1)
{
GameOver();
}
}
void GameOver()
{
snake.Reset();
food.position = food.GenerateRandomPos(snake.body);
running = false;
score = 0;
PlaySound(wallSound);
}
void CheckCollisionWithTail()
{
deque<Vector2> headlessBody = snake.body;
headlessBody.pop_front();
if (ElementInDeque(snake.body[0], headlessBody))
{
GameOver();
}
}
};
int main()
{
cout << "Starting the game..." << endl;
InitWindow(2 * offset + cellSize * cellCount, 2 * offset + cellSize * cellCount, "Retro Snake");
SetTargetFPS(60);
Game game = Game();
while (WindowShouldClose() == false)
{
BeginDrawing();
if (EventTriggered(0.2))
{
allowMove = true;
game.Update();
}
if (IsKeyPressed(KEY_UP) && game.snake.direction.y != 1 && allowMove)
{
game.snake.direction = {0, -1};
game.running = true;
allowMove = false;
}
if (IsKeyPressed(KEY_DOWN) && game.snake.direction.y != -1 && allowMove)
{
game.snake.direction = {0, 1};
game.running = true;
allowMove = false;
}
if (IsKeyPressed(KEY_LEFT) && game.snake.direction.x != 1 && allowMove)
{
game.snake.direction = {-1, 0};
game.running = true;
allowMove = false;
}
if (IsKeyPressed(KEY_RIGHT) && game.snake.direction.x != -1 && allowMove)
{
game.snake.direction = {1, 0};
game.running = true;
allowMove = false;
}
// Drawing
ClearBackground(green);
DrawRectangleLinesEx(Rectangle{(float)offset - 5, (float)offset - 5, (float)cellSize * cellCount + 10, (float)cellSize * cellCount + 10}, 5, darkGreen);
DrawText("Retro Snake", offset - 5, 20, 40, darkGreen);
DrawText(TextFormat("%i", game.score), offset - 5, offset + cellSize * cellCount + 10, 40, darkGreen);
game.Draw();
EndDrawing();
}
CloseWindow();
return 0;
}