-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.h
175 lines (156 loc) · 3.07 KB
/
game.h
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
// Game Logic //
#include <iostream>
#include <math.h>
#include <GL/glut.h>
#define WIDTH 20
#define HEIGHT 21
#define DEMX 1.0
#define DEMY 1.0
__time_t t;
int gameplay = 1;
int score = 0;
float playerPosX = 0;
float playerPosY = 0;
float fruitPosX = 0;
float fruitPosY = 0;
float delay_action = 0;
int last_Key = 3;
int previous_Key;
// Spawn Fruit //
// Spawns the fruit in a random position.
void spawnFruit()
{
while (1)
{
srand((unsigned)time(&t));
if (fruitPosX == playerPosX && fruitPosY == playerPosY)
{
fruitPosX = rand() % WIDTH;
fruitPosX = fruitPosX - 1;
fruitPosY = rand() % HEIGHT;
fruitPosY = fruitPosY - 2;
}
else
{
fruitPosX = rand() % (WIDTH - 1);
//fruitPosX = fruitPosX - 1;
fruitPosY = rand() % (HEIGHT - 2);
//fruitPosY = fruitPosY - 2;
break;
}
}
}
// Death //
// Checks whether the player is colliding with the walls.
void death(void)
{
if ((playerPosX + 1) < 1)
{
gameplay = 0;
}
else if ((playerPosX + 1) > WIDTH - 1)
{
gameplay = 0;
}
else if ((playerPosY + 1) < 1)
{
gameplay = 0;
}
else if ((playerPosY + 1) > HEIGHT - 2)
{
gameplay = 0;
}
else
{
gameplay = 1;
}
}
// Eating Fruit //
void eatFruit()
{
if (playerPosX == fruitPosX && playerPosY == fruitPosY)
{
spawnFruit();
score = score + 1;
}
}
// Player Movement
// KEY MAP
// -------
// Right Arrow Key == 1
// Left Arrow Key == 2
// Up Arrow Key == 3
// Down Arrow Key == 4
void movement()
{
delay_action = delay_action + 0.05;
if (previous_Key != last_Key)
{
delay_action = 0.9;
previous_Key = last_Key;
}
if (delay_action > 1)
{
if (last_Key == 1)
{
playerPosX = playerPosX + 1;
death();
}
else if (last_Key == 2)
{
playerPosX = playerPosX - 1;
death();
}
else if (last_Key == 3)
{
playerPosY = playerPosY + 1;
death();
}
else if (last_Key == 4)
{
playerPosY = playerPosY - 1;
death();
}
eatFruit();
delay_action = 0;
}
glutPostRedisplay();
}
// Player Logic //
void logic(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT:
if (last_Key != 2)
{
last_Key = 1;
}
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
if (last_Key != 1)
{
last_Key = 2;
}
glutPostRedisplay();
break;
case GLUT_KEY_UP:
if (last_Key != 4)
{
last_Key = 3;
}
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
if (last_Key != 3)
{
last_Key = 4;
}
glutPostRedisplay();
break;
default:
last_Key = last_Key;
break;
}
}