-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
305 lines (291 loc) · 7.5 KB
/
snake.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream> //For Printing and stuff
#include <filesystem> //For checking if a file exists
#include <fstream> //For reading/writing to files;
#include <cstdlib>
#include <ctime>
/* SDL stuff */
#include <SDL2/SDL.h>
#include <GL/glu.h>
#include <GL/gl.h>
/*Assert*/
#include <cassert>
/*
* Snake.cpp
* Author: Daniel Hannon (danielh2942)
* Version: 1
* Last Edited: 19/05/2021
* Brief: I rewrote My previous implementation of Snake in OOP fashion
* It uses C++17, SDL2, and Open GL
* I might return to this to add sounds
*/
#define TILE_SIZE 10
#define BOARD_X 64
#define BOARD_Y 64
typedef struct Point2D {
int x;
int y;
} Point2D;
typedef struct bodypart {
int xPos;
int yPos;
bodypart * next;
bodypart * prev;
} bodypart;
class Snake {
public:
Snake();
~Snake();
void start();
void reset();
void draw();
void gameOver();
private:
SDL_Window * Window;
SDL_GLContext glContext;
bool running;
bool isPaused;
bool foodEaten;
bool gameOverReached;
int score;
int highscore;
Point2D board;
Point2D food;
bodypart * snake;
bodypart * snakeEnd;
int snakeLength;
int direction[2];
};
Snake::Snake() {
//Have the board set to 30 x 30 as a default;
this->board = {BOARD_X,BOARD_Y};
this->gameOverReached = false;
//Move along the X axis :)
this->direction[1] = 0;
this->direction[0] = 1;
this->isPaused = true;
std::fstream myFile;
this->running = false;
this->score = 0;
std::cout << "Checking for a Highscore file" << std::endl;
std::filesystem::path f{"score.txt"};
if(std::filesystem::exists(f)) {
//File exists so read in highscore
myFile.open("score.txt",std::ios::in);
myFile >> this->highscore;
printf("Highscore: %d\n",this->highscore);
myFile.close();
} else {
//Create a file called score and open in write mode
myFile.open("score.txt",std::ios::out);
if(!myFile) {
std::cout << "Failed to create a Save File!" << std::endl;
} else {
myFile << "0";
myFile.close();
}
this->highscore = 0;
}
std::cout << "Creating a Snake" << std::endl;
this->snakeLength = 1;
this->snake = new bodypart;
this->snake->xPos = this->board.x/2;
this->snake->yPos = this->board.y/2;
this->snake->next = NULL;
this->snake->prev = NULL;
this->snakeEnd = this->snake;
std::cout<<"Seeding Random number"<<std::endl;
srand(time(NULL));
std::cout<<"Creating Window"<<std::endl;
SDL_Init(SDL_INIT_VIDEO);
this->Window = SDL_CreateWindow("Snake",0,0,this->board.x * TILE_SIZE, this->board.y * TILE_SIZE,SDL_WINDOW_OPENGL);
assert(Window);
std::cout<<"Window Exists"<<std::endl;
this->glContext = SDL_GL_CreateContext(this->Window);
std::cout<<"Done!"<<std::endl;
}
Snake::~Snake() {
std::cout<<"Closing Game!"<<std::endl;
/*Write new Highscore to the document*/
std::fstream myFile;
myFile.open("score.txt", std::ios::out);
if(!myFile) {
std::cout<<"Could not Write new High score to the file!"<<std::endl;
} else {
myFile<<this->highscore;
myFile.close();
}
SDL_DestroyWindow(this->Window);
SDL_GL_DeleteContext(this->glContext);
SDL_Quit();
}
void Snake::start() {
this->running = true;
this->food = {rand()%this->board.x,rand()%this->board.y};
bodypart * checker;
while(this->running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
this->running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
this->running = false;
break;
case SDLK_w:
case SDLK_UP:
if(this->direction[1]!=-1) {
this->direction[1] = 1;
this->direction[0] = 0;
}
break;
case SDLK_s:
case SDLK_DOWN:
if(this->direction[1] != 1) {
this->direction[1] = -1;
this->direction[0] = 0;
}
break;
case SDLK_a:
case SDLK_LEFT:
if(this->direction[0] != 1) {
this->direction[1] = 0;
this->direction[0] = -1;
}
break;
case SDLK_d:
case SDLK_RIGHT:
if(this->direction[0] != -1) {
this->direction[1] = 0;
this->direction[0] = 1;
}
break;
case SDLK_SPACE:
//Toggle Play/Pause
this->isPaused ^= true;
std::cout<<"Play/Pause toggled"<<std::endl;
break;
}
break;
}
}
if(!isPaused) {
this->foodEaten = false;
/*Perform game logic here*/
Point2D temp = {this->snake->xPos + direction[0], this->snake->yPos + direction[1]};
if(temp.x < 0 || temp.x > this->board.x || temp.y < 0 || temp.y > this->board.y ) {
std::cout<<"Board edge reached"<<std::endl;
this->gameOver();
} else if (temp.x == this->food.x && temp.y == this->food.y) {
this->score += 10;
this->food.x = rand() % this->board.x;
this->food.y = rand() % this->board.y;
this->foodEaten = true;
}
checker = this->snake;
while(checker != NULL) {
if(temp.x == checker->xPos && temp.y == checker->yPos) {
std::cout<<"Collision"<<std::endl;
this->gameOver();
}
checker = checker->next;
}
if(!this->gameOverReached) {
if(this->snakeLength < 4 || this->foodEaten) {
bodypart * tempSnake = new bodypart;
tempSnake->next = this->snake;
tempSnake->prev = NULL;
this->snake->prev = tempSnake;
this->snake = tempSnake;
this->snakeLength++;
} else {
bodypart * part = this->snakeEnd;
this->snakeEnd = part->prev;
this->snakeEnd->next = NULL;
part->prev = NULL;
part->next = this->snake;
this->snake->prev = part;
this->snake = part;
}
this->snake->xPos = temp.x;
this->snake->yPos = temp.y;
} else {
this->gameOverReached = false;
}
}
this->draw();
SDL_Delay(100);
}
}
void Snake::reset() {
bodypart * temp = this->snakeEnd->prev;
do {
delete temp->next;
if(temp->prev == NULL) {
delete temp;
break;
} else {
temp = temp->prev;
}
} while(1);
this->snakeLength = 1;
this->snake = new bodypart;
this->snake->xPos = this->board.x/2;
this->snake->yPos = this->board.y/2;
this->snake->next = NULL;
this->snake->prev = NULL;
this->snakeEnd = this->snake;
this->score = 0;
this->direction[0] = 1;
this->direction[1] = 0;
}
void Snake::draw() {
glViewport(0,0,this->board.x * TILE_SIZE, this->board.y * TILE_SIZE);
//R G B Alpha
glClearColor(0.f,0.f,0.f,0.f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,this->board.x,0,this->board.y,-1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(1,1,1); // White
bodypart * temp = this->snake;
while(temp!=NULL) {
//This has to be done in clockwise or it doesn't work
glBegin(GL_QUADS);
glVertex2d(temp->xPos + 1, temp->yPos);
glVertex2d(temp->xPos, temp->yPos);
glVertex2d(temp->xPos, temp->yPos + 1);
glVertex2d(temp->xPos+1,temp->yPos+1);
glEnd();
temp = temp->next;
}
glColor3d(1,0,0); //Red
glBegin(GL_QUADS);
glVertex2d(this->food.x+1,this->food.y);
glVertex2d(this->food.x,this->food.y);
glVertex2d(this->food.x,this->food.y+1);
glVertex2d(this->food.x+1,this->food.y+1);
glEnd();
glFlush();
SDL_GL_SwapWindow(this->Window);
}
void Snake::gameOver() {
char gameOverString[255];
if(this->score > this->highscore) {
sprintf(gameOverString, "Game Over!\nNew High Score: %d", this->score);
this->highscore = this->score;
} else {
sprintf(gameOverString, "Game Over!\n Your Score was %d\nThe Highscore is %d", this->score, this->highscore);
}
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION,"Game Over",gameOverString,this->Window);
this->isPaused = true;
this->gameOverReached = true;
this->reset();
}
int main() {
Snake myGame = Snake();
myGame.start();
return 0;
}