-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
146 lines (128 loc) · 3.15 KB
/
Copy pathgame.cpp
File metadata and controls
146 lines (128 loc) · 3.15 KB
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
#include <GL/gl.h>
#include <GL/glut.h>
#include <ctime>
#include "game.h"
extern int score;
extern bool gameOver;
extern bool gameStarted;
int gridX, gridY;
int snake_length = 5;
bool food = true;
int foodX, foodY;
short sDirection = RIGHT;
int posX[60] = {20, 20, 20, 20, 20}, posY[60] = {20, 19, 18, 17, 16};
void initGrid(int x, int y) {
gridX = x;
gridY = y;
}
void drawGrid() {
// Left line
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex2d(0, 0);
glColor3d(1, 0, 0);
glVertex2d(1, 0);
glColor3d(1, 0, 0);
glVertex2d(1, 40);
glColor3d(1, 0, 0);
glVertex2d(0, 40);
glEnd();
// Right line
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex2d(39, 0);
glColor3d(1, 0, 0);
glVertex2d(40, 0);
glColor3d(1, 0, 0);
glVertex2d(40, 40);
glColor3d(1, 0, 0);
glVertex2d(39, 40);
glEnd();
// Top line
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex2d(0, 39);
glColor3d(1, 0, 0);
glVertex2d(40, 39);
glColor3d(1, 0, 0);
glVertex2d(40, 40);
glColor3d(1, 0, 0);
glVertex2d(0, 40);
glEnd();
// Bottom line
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex2d(0, 0);
glColor3d(1, 0, 0);
glVertex2d(40, 0);
glColor3d(1, 0, 0);
glVertex2d(40, 1);
glColor3d(1, 0, 0);
glVertex2d(0, 1);
glEnd();
}
void drawFood() {
if (food)
random(foodX, foodY);
food = false;
glColor3f(1.0, 0.0, 1.0);
glRectd(foodX, foodY, foodX + 1, foodY + 1);
}
void drawSnake() {
for (int i = snake_length - 1; i > 0; i--) {
posX[i] = posX[i - 1];
posY[i] = posY[i - 1];
}
if (sDirection == UP)
posY[0]++;
else if (sDirection == DOWN)
posY[0]--;
else if (sDirection == RIGHT)
posX[0]++;
else if (sDirection == LEFT)
posX[0]--;
for (int i = 0; i < snake_length; i++) {
if (i == 0) {
glColor3f(1.0, 1.0, 1.0);
} else {
glColor3f(0.0, 1.0, 0.0);
}
glRectd(posX[i], posY[i], posX[i] + 1, posY[i] + 1);
}
if (posX[0] == 0 || posX[0] == gridX - 1 || posY[0] == 0 || posY[0] == gridY - 1)
gameOver = true;
if (posX[0] == foodX && posY[0] == foodY) {
score++;
snake_length++;
if (snake_length > MAX) {
snake_length = MAX;
}
food = true;
}
for (int j = 1; j < snake_length; j++) {
if (posX[j] == posX[0] && posY[j] == posY[0])
gameOver = true;
}
}
void random(int &x, int &y) {
int _maxX = gridX - 2;
int _maxY = gridY - 2;
int _min = 1;
srand(time(NULL));
x = _min + rand() % (_maxX - _min);
y = _min + rand() % (_maxY - _min);
}
// Reset game to initial state - implementation only in game.cpp
void resetGame() {
score = 0;
snake_length = 5;
food = true;
sDirection = RIGHT;
gameOver = false;
gameStarted = false;
// Reset snake position
for (int i = 0; i < snake_length; i++) {
posX[i] = 20 - i;
posY[i] = 20;
}
}