forked from marioOrtegaGarcia/Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cpp
executable file
·338 lines (270 loc) · 7.79 KB
/
App.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "App.h"
#include <vector>
using namespace std;
static App* singleton;
void app_timer(int value){
if (singleton->leonidas->alive) {
singleton->leonidas->move();
singleton->leonidas->collisionCheck();
}
//Draws animation for Gameover calls itself at a rate of 100ms
if (singleton->game_over) {
singleton->redraw();
glutTimerFunc(100, app_timer, value);
} else {
//Calls itself while game isnt over at a rate of 16 ms
if (true){
singleton->redraw();
glutTimerFunc(100, app_timer, value);
}
}
}
struct gameInfo{
int gameMode = 0; // 0 for home screen, 1 to play game, 2 for high scores
int turn = 0;
int gameBoard[9];
bool gameOver;
float x, y, w, h;
gameInfo(){
gameOver = false; // Game is not over when started
for (int i = 0; i < 9; i ++){
gameBoard[i] = 0;
}
}
bool gameState(){
return gameOver;
}
void endGame(){
gameOver = true;
}
void checkIfOver(){
// Horizontal wins
if (gameBoard[0] == 1 && gameBoard[1] == 1 && gameBoard[2] == 1) gameOver = true, cout << "Player 1 wins" << endl;
}
};
struct Rect{
float x, y, width, height;
bool pressed = false;
Rect(float a, float b, float c, float d){
x = a;
y = b;
width = c;
height = d;
}
float getX(){
return x;
}
float getY(){
return y;
}
float getWidth(){
return width;
}
float getHeight(){
return height;
}
void setX(float input){
x = input;
}
void setY(float input){
y = input;
}
void setWidth(float input){
width = input;
}
void setHeight(float input){
height = input;
}
void click(){
if (!pressed) pressed = true;
else pressed = false;
}
void unClick() {
pressed = false;
}
bool contains(float inputX, float inputY){
return ((inputX > x && inputX < (x + width)) && (inputY < y && inputY > (y - height)));
}
~Rect(){
}
};
void writeText(const char *text, int x, int y, int length){
glMatrixMode(GL_PROJECTION);
double *matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x,y);
for(int i = 0; i < length; i++){
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
//Can We Just call this functions giving it two different arrays?
void writeText2(const char *text2, int x, int y, int length){
glMatrixMode(GL_PROJECTION);
double *matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x,y);
for(int i = 0; i < length; i++){
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text2[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
vector<Rect*> home;
gameInfo *game = new gameInfo;
App::App(const char* label, int x, int y, int w, int h): GlutApp(label, x, y, w, h){
// Initialize state variables
singleton = this;
mx = 0.0;
my = 0.0;
// Draw the home buttons
home.push_back(new Rect(-0.3, 0.55, 0.6, 0.25));
home.push_back(new Rect(-0.3, -0.35, 0.6, 0.25));
game_over = false;
score = new Score();
board = new Board();
board->placeMice();
background = new TexRect("images/grass.jpeg", -1, .83, 2, 2);
reset = new resetButton("images/reset.png", -1, 1, .167, .167);
pause = new pauseButton("images/pause.png", .83, 1, .167, .167);
leonidas = new Snake();
pause->pauseClicked = false;
reset->resetClicked = false;
}
void App::specialKeyPress(int key){
if (key >= 100 && key <= 103) {
leonidas->changeDirection(key);
}
}
void App::specialKeyUp(int key){
}
void App::draw() {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set background color to black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Set up the transformations stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float oneThird = 0.333;
float twoThirds = 0.667;
if (game->gameMode == 0){ // Home Screen
// Draw the two home buttons
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < home.size(); i++){
glBegin(GL_POLYGON);
glVertex2f(home[i]->getX(), home[i]->getY());
glVertex2f(home[i]->getX() + home[i]->getWidth(), home[i]->getY());
glVertex2f(home[i]->getX() + home[i]->getWidth(), home[i]->getY() - home[i]->getHeight());
glVertex2f(home[i]->getX(), home[i]->getY() - home[i]->getHeight());
glEnd();
}
//Write "Play"
string text;
text = "Play";
glColor3f(1.0, 1.0, 1.0);
writeText(text.data(), 380, 430, 15);
//Write "High Scores"
string text2;
text2 = "High Scores";
glColor3f(1.0, 1.0, 1.0);
writeText2(text2.data(), 365, 160, 15);
}else if (game->gameMode == 1){ // Snake game
background->draw();
score->draw();
reset->draw();
pause->draw();
board->draw();
leonidas->draw();
app_timer(1);
}else if (game->gameMode == 2){ // High score screen
// Draw the high score box
glColor3f(1.0, 1.0, 1.0);
glLineWidth(2.5);
glBegin(GL_LINES);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.3);
glVertex2f(0.5, 0.3);
glVertex2f(-0.5, 0.1);
glVertex2f(0.5, 0.1);
glVertex2f(-0.5, -0.1);
glVertex2f(0.5, -0.1);
glVertex2f(-0.5, -0.3);
glVertex2f(0.5, -0.3);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
// Write "High Scores"
string text2;
text2 = "High Scores";
glColor3f(1.0, 1.0, 1.0);
writeText2(text2.data(), 360, 460, 15);
reset->draw();
}
// We have been drawing everything to the back buffer
// Swap the buffers to see the result of what we drew
glFlush();
glutSwapBuffers();
}
void App::mouseDown(float x, float y){
// Update app state
mx = x;
my = y;
if (game->gameMode == 0){ // Title Screen
if (home[0]->contains(x, y)) game->gameMode = 1;
else if (home[1]->contains(x, y)) game->gameMode = 2;
}else if (game->gameMode == 1 && !game->gameOver){ // Snake Game
game->checkIfOver();
}else if (game->gameMode == 2 && !game->gameOver){ // High Scores
game->checkIfOver();
}
if (pause->contains(x, y)){
pause->pauseClicked = true;
}
if (reset->contains(x, y)){
game->gameMode = 0;
}
// Redraw the scene
redraw();
}
void App::mouseDrag(float x, float y){
// Update app state
mx = x;
my = y;
}
void App::idle(){
//leonidas->move();
//redraw();
}
void App::keyPress(unsigned char key) {
if (key == 27){
// Exit the app when Esc key is pressed
delete background;
delete this;
exit(0);
}
if (key == ' '){
}
}