-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cpp
329 lines (288 loc) · 9.22 KB
/
game.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
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <iomanip>
#include <SDL_ttf.h>
#include "game.h"
double lastTime = 0;
double nowTime = 0;
double unprocessedstartTime = 0;
double unprocessedendTime = 0;
double pauseTime = 0;
int abstartTime = 0;
int abnowTime = 0;
int dtime = 0;
bool halfquit = false;
void normalmode()
{
lastTime = SDL_GetTicks();
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//The dot that will be moving around on the screen
Dot dot;
//mapobject
mapobject mapobject[5];
for(int i=1;i<5;i++){
mapobject[i].arrayinit(5,3,5,i);
mapobject[i].reset();
}
//The background scrolling offset
int scrollingOffset = 0;
//start
int start = 0;
//In memory text stream
std::stringstream timeText;
std::stringstream timeText2;
std::stringstream timeText3;
int turn = 0;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}else if( e.type == SDL_KEYDOWN && e.key.repeat == 0 ){
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_p: pausemode(); break;
}
if(halfquit){
return;
}
}
//Handle input for the dot
dot.handleEvent( e );
}
dot.move();
for(int i=0;i<5;i++){
mapobject[i].checkcollision(dot);
mapobject[i].move(mapspeed);
mapobject[i].appmove(dot);
}
//Scroll background
scrollingOffset+=mapspeed;
if( scrollingOffset < -(gBGTexture.getWidth()) )
{
dot.checkpoint();
for(int i=1;i<5;i++){
mapobject[i].reset();
}
dot.reset();
scrollingOffset = 0;
}
//Set text to be rendered
timeText.str( "" );
nowTime = SDL_GetTicks()-3000;
timeText <<"Score: "<<std::setw(8)<<score<<" "<<"Gold: $ "<<std::setw(8)<<asset
<< " Distance: "<<std::setw(8)<<(int)( ((nowTime-lastTime)-pauseTime) / 100 );
int tempdistance ;
tempdistance = (int)( ((nowTime-lastTime)-pauseTime) / 100 );
//timeText2 << "Distance: "<<" "<<"m"<<" "<<"Score: "<<" "<<"$ ";
/*timeText3 << " "<<score<<" "<<asset;*/
unprocessedendTime = unprocessedstartTime =0;
//Render text
if( !gTimeTextTexture.loadFromRenderedText( timeText.str().c_str(), { 49, 49, 49 } ) )
{
printf( "Unable to render time texture!\n" );
}
/*if( !gTimeTextTexture2.loadFromRenderedText( timeText2.str().c_str(), { 49, 49, 49 } ) )
{
printf( "Unable to render time texture!\n" );
}
if( !gTimeTextTexture3.loadFromRenderedText( timeText3.str().c_str(), { 49, 49, 49 } ) )
{
printf( "Unable to render time texture!\n" );
}*/
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Render background
gBGTexture.render( scrollingOffset, 0 );
gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 );
//Render objects
if(dot.isdown){
dot.render();
for(int i=1;i<5;i++){
mapobject[i].render(dot);
}
for(int i=1;i<5;i++){
mapobject[i].renderstack(dot);
}
}else{
for(int i=1;i<5;i++){
mapobject[i].render(dot);
}
dot.render();
for(int i=1;i<5;i++){
mapobject[i].renderstack(dot);
}
}
while(start<1){
gTextTexture1.render( ( SCREEN_WIDTH - gTextTexture1.getWidth() ) / 2, ( SCREEN_HEIGHT - gTextTexture1.getHeight() ) / 2 );
SDL_RenderPresent( gRenderer );
SDL_Delay(1000);
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Render background
gBGTexture.render( scrollingOffset, 0 );
gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 );
//Render objects
dot.render();
gTextTexture2.render( ( SCREEN_WIDTH - gTextTexture2.getWidth() ) / 2, ( SCREEN_HEIGHT - gTextTexture2.getHeight() ) / 2 );
SDL_RenderPresent( gRenderer );
SDL_Delay(1000);
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Render background
gBGTexture.render( scrollingOffset, 0 );
gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 );
//Render objects
dot.render();
gTextTexture3.render( ( SCREEN_WIDTH - gTextTexture3.getWidth() ) / 2, ( SCREEN_HEIGHT - gTextTexture3.getHeight() ) / 2 );
SDL_RenderPresent( gRenderer );
SDL_Delay(1000);
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Render background
gBGTexture.render( scrollingOffset, 0 );
gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 );
//Render objects
dot.render();
start++;
gFont = TTF_OpenFont( "pic/AGENCYB.ttf", 48 );
}
if(tempdistance>1500) mapspeed = -6;
if(tempdistance==(500+500*triggeredtime)) {
triggered = true;
abstartTime = SDL_GetTicks();
triggeredtime++;
}
abnowTime = SDL_GetTicks();
dtime = abnowTime - abstartTime;
if(billionaire||timemachine||champion||superman){
dot.mapevent(dtime);
}
dot.checkborder(scrollingOffset);
if(dot.isdead){
Mix_PlayChannel( -1, gdead, 0 );
distance = (int)( ((nowTime-lastTime)-pauseTime) / 100 );
gTextTexture4.render( ( SCREEN_WIDTH - gTextTexture4.getWidth() ) / 2, ( SCREEN_HEIGHT - gTextTexture4.getHeight() ) / 2 );
SDL_RenderPresent( gRenderer );
SDL_Delay(3000);
break;
}
gTimeTextTexture.render( 0,0 );
/*gTimeTextTexture2.render( SCREEN_WIDTH/2-200,0 );
gTimeTextTexture3.render( 0,0 );*/
//Update screen
SDL_RenderPresent( gRenderer );
}
}
void pausemode()
{
//Main loop flag
bool pausestate = true;
//Event handler
SDL_Event e;
//Calculate the paused ticks
unprocessedstartTime = SDL_GetTicks();
while(pausestate){
while( SDL_PollEvent( &e ) != 0 )
{
if( e.type == SDL_KEYDOWN && e.key.repeat == 0 ){
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_p: pausestate = false; break;
case SDLK_q: pausestate = false; halfquit = true; break;
}
}
}
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
gPausePromptTexture.render(0,0);
//Update screen
SDL_RenderPresent( gRenderer );
}
//Reset the paused ticks
unprocessedendTime = SDL_GetTicks();
pauseTime+=(unprocessedendTime-unprocessedstartTime);
}
void reset()
{
asset = 0;
score = 0;
distance = 0;
nowTime = 0;
lastTime = 0;
pauseTime = 0;
triggeredtime = 0;
halfquit = false;
}
int main( int argc, char* args[] )
{
loadfile();
srand(time(0));
//The application timer
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
Mix_VolumeMusic(MIX_MAX_VOLUME/2);
Mix_PlayMusic( gMusic, -1 );
int a;
do{
a = menu();
switch(a){
case 1:
Mix_VolumeMusic(MIX_MAX_VOLUME/4);
normalmode();
if(!halfquit){
totalmoney+=asset;
if(score>highscore) highscore = score;
if(distance>highdistance) highdistance = distance;
substract();
}
reset();
savefile();
break;
case 2 :
shop();
savefile();
break;
case 3:
mission();
break;
case 4:
break;
}
}while(a!=4);
}
}
//Free resources and close SDL
close();
return 0;
}