-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
220 lines (183 loc) · 5.47 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
//
// Created by kayle on 1/19/24.
//
#include <iostream>
#include <raylib.h>
#include <deque>
#include <raymath.h>
using namespace std;
Color green = {173, 204, 96, 255};
Color darkGreen = {43, 51, 24, 255};
int cellSize = 30;
int cellCount = 25;
int offset = 70;
double lastUpdateTime = 0;
bool eventTriggered(double interval) {
double currentTime = GetTime();
if (currentTime - lastUpdateTime >= interval) {
lastUpdateTime = currentTime;
return true;
}
return false;
}
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};
bool addSegment = false;
void Draw() {
for (unsigned 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) {
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);
position = GenerateRandomPosition(snakeBody);
UnloadImage(image);
}
~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 GenerateRandomPosition(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 wallSound;
Sound eatSound;
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.GenerateRandomPosition(snake.body);
snake.addSegment = true;
score++;
PlaySound(eatSound);
}
}
void CheckCollisionWithEdges() {
if (snake.body[0].x == cellCount || snake.body[0].x == -1) {
GameOver();
PlaySound(wallSound);
}
if (snake.body[0].y == cellCount || snake.body[0].y == -1) {
GameOver();
PlaySound(wallSound);
}
}
void CheckCollisionWithTail() {
deque<Vector2> headlessBody = snake.body;
headlessBody.pop_front();
if (ElementInDeque(snake.body[0], headlessBody)) {
GameOver();
}
}
void GameOver() {
snake.Reset();
food.position = food.GenerateRandomPosition(snake.body);
running = false;
score = 0;
}
};
int main() {
cout << "Starting Game" << endl;
InitWindow(2 * offset + cellSize * cellCount, 2 * offset + cellSize * cellCount, "Retro Snake By Kayle");
SetTargetFPS(60);
Game game = Game();
while (!WindowShouldClose()) {
BeginDrawing();
if (eventTriggered(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) cellSize * cellCount + 10,
(float) cellSize * cellCount + 10}, 5, darkGreen);
DrawText("Retro Snake By KVS", offset - 5, 20, 40, darkGreen);
DrawText(TextFormat("Score: %i", game.score), offset - 5, offset+cellSize*cellCount+10, 40, darkGreen);
game.Draw();
EndDrawing();
}
CloseWindow();
return 0;
}