-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpooderPal.cpp
228 lines (186 loc) · 4.83 KB
/
SpooderPal.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
#include <cstdlib>
#include <time.h>
#include <GL/glut.h>
#include "Cityscape.h"
#include "Spooder.h"
#include "Scene.h"
int x = 0;
int y = 380;
int hookX;
int hookY;
const int nDarkBuilding = 20;
DarkBuilding dkb[nDarkBuilding];
const int nBlueBuilding = 15;
BlueBuilding blb[nBlueBuilding];
const int nBlackBuilding = 25;
BlackBuilding bkb[nBlackBuilding];
const int nSmallBuilding = 25;
SmallBuilding smb[nSmallBuilding];
void buildCityscape()
{
srand(time(NULL));
int previousBuildingPosition = -200; //to fill the left side
for(int i = 0; i < nDarkBuilding; i++)
{
int currentBuildingPosition = previousBuildingPosition + (rand() % 2000)/10 + 100;
dkb[i] = DarkBuilding(currentBuildingPosition);
previousBuildingPosition = currentBuildingPosition;
}
previousBuildingPosition = -140; //to fill the left side
for(int i = 0; i < nBlueBuilding; i++)
{
int currentBuildingPosition = previousBuildingPosition + (rand() % 2000)/10 + 200;
blb[i] = BlueBuilding(currentBuildingPosition);
previousBuildingPosition = currentBuildingPosition;
}
previousBuildingPosition = -10; //to fill the left side
for(int i = 0; i < nBlackBuilding; i++)
{
int currentBuildingPosition = previousBuildingPosition + (rand() % 2000)/10 + 20;
bkb[i] = BlackBuilding(currentBuildingPosition);
previousBuildingPosition = currentBuildingPosition;
}
previousBuildingPosition = -10; //to fill the left side
for(int i = 0; i < nSmallBuilding; i++)
{
int currentBuildingPosition = previousBuildingPosition + (rand() % 2000)/10 + 10;
smb[i] = SmallBuilding(currentBuildingPosition);
previousBuildingPosition = currentBuildingPosition;
}
}
void spooder()
{
leftHand(x, y);
leftLeg(x, y);
body(x, y);
rightLeg(x, y);
rightHand(x, y);
}
void jump()
{
if(!isFalling)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2i(hookX, hookY);
glVertex2i(x+5, y+14);
glEnd();
}
if(x < hookX && y < hookY)
{
x+=5;
y+=4;
}
else if(y > 380)
{
if(x > hookX+12)
isFalling = true;
x+=4;
y-=5;
}
else
isJumping = false;
}
void myDisplay(void)
{
glClear (GL_COLOR_BUFFER_BIT);
background();
for (int n = 0; n < nDarkBuilding; n++)
{
dkb[n].drawBuilding();
}
for (int n = 0; n < nBlueBuilding; n++)
{
blb[n].drawBuilding();
}
road();
spooder();
if(isJumping)
jump();
for (int n = 0; n < nBlackBuilding; n++)
{
bkb[n].drawBuilding();
}
for (int n = 0; n < nSmallBuilding; n++)
{
smb[n].drawBuilding();
}
//glFlush ();
glutSwapBuffers(); //swaps buffered drawings, replaces glFlush()
//Autorun
/* if(x < 2560)
x+=10;
else
x=0;
*/
if(x > 2560)
{
x=0;
y=380;
isJumping = false;
}
}
void myTimer(int unused)
{
glutPostRedisplay();
glutTimerFunc(20, myTimer, unused);
}
void myKeyboard(unsigned char key, int a, int b)
{
switch(key)
{
case 'd':
x+=15;/*
if(x > 2560)
{
glutDestroyWindow(glutGetWindow());
exit (0);
}*/
break;
case 'w':
for (int n = 0; n < nBlueBuilding; n++)
{
if(blb[n].hPosition > x) //the next blue building
{
hookX = blb[n].hPosition + blb[n].width/2;
hookY = blb[n].vPosition + blb[n].height*9/10;
break;
}
}
isJumping = true;
isFalling = false;
jump();
break;
case 27:
glutDestroyWindow(glutGetWindow());
exit(0);
break;
}
glutPostRedisplay();
}
void myInit (void)
{
glLineWidth(1);
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(0.0, 1280.0, 0.0, 544.0); //True scaling
gluOrtho2D(0.0, 2560.0, 0.0, 1088.0); //
//gluOrtho2D(0.0, 160.0, 0.0, 45.0); //Preview large Spooder
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); //Double buffer reduces flickering
glutInitWindowSize (1280, 544);
glutInitWindowPosition (100, 150);
glutCreateWindow ("SpooderPal");
glutDisplayFunc(myDisplay);
glutKeyboardFunc(myKeyboard);
myInit();
buildCityscape();
myTimer(0);
glutMainLoop();
return 0;
}