-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
459 lines (386 loc) · 12.8 KB
/
main.cpp
File metadata and controls
459 lines (386 loc) · 12.8 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*This source code copyrighted by Lazy Foo' Productions (2004-2013)
and may not be redistributed without written permission.*/
//The headers
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>
#include <iostream>
#include <vector>
#include "Personaje.h"
#include "Enemygo.h"
#include "Fantasmita.h"
#include "bomba.h"
#include "Mina.h"
#include "Cocodrilo.h"
#include "SDL_mixer.h"
//using namespace std;
//Screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *up = NULL;
SDL_Surface *down = NULL;
SDL_Surface *left = NULL;
SDL_Surface *right = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The font
TTF_Font *font = NULL;
//The color of the font
SDL_Color textColor = { 0, 0, 0 };
SDL_Surface *load_image( std::string filename )
{
return IMG_Load( filename.c_str() );
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Press an Arrow Key", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
background = load_image( "Menu/Menu.png" );
//Open the font
font = TTF_OpenFont( "lazy.ttf", 72 );
//If there was a problem in loading the background
if( background == NULL )
{
return false;
}
//If there was an error in loading the font
if( font == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( up );
SDL_FreeSurface( down );
SDL_FreeSurface( left );
SDL_FreeSurface( right );
//Close the font
TTF_CloseFont( font );
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
Personaje *personaje=new Personaje(0 ,0);
SDL_Surface* meta=load_image("metass.png");
SDL_Surface* victoria=load_image("victoria.png");
SDL_Surface* game_over=load_image("Endless Run/game over.png");
SDL_Surface* Fondo=load_image("fondo_mario.png");
SDL_Surface* instrucciones=load_image("Menu/instrMos.png");
SDL_Surface* botonPlay=load_image("Menu/jugar.png");
SDL_Surface* botonSalir=load_image("Menu/salirya.png");
SDL_Surface* botonIntrucciones=load_image("Menu/instruccionesya.png");
SDL_Surface* seleccionPlay=load_image("Menu/jugarya.png");
SDL_Surface* seleccionSalir=load_image("Menu/seleccionSalir.png");
SDL_Surface* Menu=load_image("Menu/instr.png");
SDL_Surface* selecionIntrucciones=load_image("Menu/selecionIntrucciones.png");
std::vector<Enemygo*>enemigos;
enemigos.push_back(new Fantasmita(personaje));
enemigos.push_back(new bomba(personaje));
enemigos.push_back(new Cocodrilo(personaje));
Mina *mina=new Mina(personaje);
bool jugar=false;
bool gano=false;
bool perdio=false;
int duracion_animacion=20;
int cuadro_actual=0;
int iteracion=0;
int menu=0;
int seleccion=0;
bool moviendose=false;
int tiempo=0;
//Render the text
up = TTF_RenderText_Solid( font, "Arriba", textColor );
down = TTF_RenderText_Solid( font, "Abajo", textColor );
left = TTF_RenderText_Solid( font, "Izquierda", textColor );
right = TTF_RenderText_Solid( font, "Derecha", textColor );
//While the user hasn't quit
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
Uint8 *keystates = SDL_GetKeyState( NULL );
if (menu==0||menu==1||menu==3&&jugar==false&&seleccion==0&&perdio==false&&gano==false)
apply_surface( 0, 0, Menu, screen );
if (menu==0&&jugar==false&&seleccion==0){
apply_surface( 100, 50, seleccionPlay, screen );
apply_surface( 100, 200, botonIntrucciones, screen );
apply_surface( 100, 400, botonSalir, screen );
}
if (menu==2&&jugar==false&&seleccion==0){
apply_surface( 100, 50, botonPlay, screen );
apply_surface( 100, 200, selecionIntrucciones, screen );
apply_surface( 100, 400, botonSalir, screen );
}
if (menu==1&&jugar==false&&seleccion==0){
apply_surface( 370, 50, botonPlay, screen );
apply_surface( 10, 200, botonIntrucciones, screen );
apply_surface( 650, 400, seleccionSalir, screen );
}
if( keystates[ SDLK_SPACE ] )
{
menu++;
}
if (menu==3)
menu=0;
if( keystates[ SDLK_RETURN ] )
{
if (menu==0&&jugar==false){
jugar=true;
}
if (menu==2&&jugar==false){
seleccion=1;
menu=10;
}
if (menu==1&&jugar==false){
exit(0);
}
if (seleccion==1&&jugar==false)
apply_surface( 0, 0, instrucciones, screen );
}
if( keystates[ SDLK_ESCAPE ] ){
if (seleccion==1){
seleccion=0;
menu=0;
}
}
if (jugar==true&&perdio==false&&gano==false){
apply_surface( 0, 0, Fondo, screen );
apply_surface(590,400,meta,screen);
mina->Dibujar(screen);
mina->logica();
if(moviendose)
apply_surface(personaje->x-128,personaje->y-128,personaje->sprites[cuadro_actual],screen);
else
apply_surface(personaje->x-128,personaje->y-128,personaje->sprites[0],screen);
for (int i=0 ;i<enemigos.size();i++){
enemigos[i]->sprites[0]=enemigos[i]->sprites[cuadro_actual];
enemigos[i]->Dibujar(screen);
}
for (int i=0 ;i<enemigos.size();i++){
enemigos[i]->logica();
}
}
//Get the keystates
if (perdio==true){
apply_surface( 0, 0, game_over, screen );
}
if (perdio==true){
//Pointer to the main screen surface
Mix_Chunk *sound = NULL; //Pointer to our sound, in memory
int channel; //Channel on which our sound is played
int audio_rate = 22050; //Frequency of audio playback
Uint16 audio_format = AUDIO_S16SYS; //Format of the audio we're playing
int audio_channels = 2; //2 channels = stereo
int audio_buffers = 4096; //Size of the audio buffers in memory
//Initialize BOTH SDL video and SDL audio
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//Initialize SDL_mixer with our chosen audio settings
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
printf("Unable to initialize audio: %s\n", Mix_GetError());
exit(1);
}
//Load our WAV file from disk
sound = Mix_LoadWAV("game over.wav");
if(sound == NULL) {
printf("Unable to load WAV file: %s\n", Mix_GetError());
}
//Set the video mode to anything, just need a window
//Play our sound file, and capture the channel on which it is played
channel = Mix_PlayChannel(-1, sound, 0);
if(channel == -1) {
printf("Unable to play WAV file: %s\n", Mix_GetError());
}
//Wait until the sound has stopped playing
while(Mix_Playing(channel) != 0);
//Release the memory allocated to our sound
Mix_FreeChunk(sound);
//Need to make sure that SDL_mixer and SDL have a chance to clean up
Mix_CloseAudio();
tiempo++;
}
if (gano==true){
apply_surface( 200,80, victoria, screen );
}
if (gano==true){
//Pointer to the main screen surface
Mix_Chunk *sound = NULL; //Pointer to our sound, in memory
int channel; //Channel on which our sound is played
int audio_rate = 22050; //Frequency of audio playback
Uint16 audio_format = AUDIO_S16SYS; //Format of the audio we're playing
int audio_channels = 2; //2 channels = stereo
int audio_buffers = 4096; //Size of the audio buffers in memory
//Initialize BOTH SDL video and SDL audio
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//Initialize SDL_mixer with our chosen audio settings
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
printf("Unable to initialize audio: %s\n", Mix_GetError());
exit(1);
}
//Load our WAV file from disk
sound = Mix_LoadWAV("gnatattack_levelcomplete.wav");
if(sound == NULL) {
printf("Unable to load WAV file: %s\n", Mix_GetError());
}
//Set the video mode to anything, just need a window
//Play our sound file, and capture the channel on which it is played
channel = Mix_PlayChannel(-1, sound, 0);
if(channel == -1) {
printf("Unable to play WAV file: %s\n", Mix_GetError());
}
//Wait until the sound has stopped playing
while(Mix_Playing(channel) != 0);
//Release the memory allocated to our sound
Mix_FreeChunk(sound);
//Need to make sure that SDL_mixer and SDL have a chance to clean up
Mix_CloseAudio();
tiempo++;
}
moviendose=false;
//If up is pressed
if( keystates[ SDLK_UP ] )
{
apply_surface( ( SCREEN_WIDTH - up->w ) / 2, ( SCREEN_HEIGHT / 2 - up->h ) / 2, up, screen );
personaje->y--;
moviendose=true;
}
//If down is pressed
if( keystates[ SDLK_DOWN ] )
{
apply_surface( ( SCREEN_WIDTH - down->w ) / 2, ( SCREEN_HEIGHT / 2 - down->h ) / 2 + ( SCREEN_HEIGHT / 2 ), down, screen );
personaje->y++;
personaje->y++;
personaje->y++;
moviendose=true;
}
//If left is pressed
if( keystates[ SDLK_LEFT ] )
{
apply_surface( ( SCREEN_WIDTH / 2 - left->w ) / 2, ( SCREEN_HEIGHT - left->h ) / 2, left, screen );
personaje->x--;
moviendose=true;
}
//If right is pressed
if( keystates[ SDLK_RIGHT ] )
{
apply_surface( ( SCREEN_WIDTH / 2 - right->w ) / 2 + ( SCREEN_WIDTH / 2 ), ( SCREEN_HEIGHT - right->h ) / 2, right, screen );
personaje->x++;
moviendose=true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
if (tiempo==2){
if (perdio==true){
perdio=false;
menu=0;
jugar=false;
tiempo=0;
}
if (gano==true){
jugar=false;
menu=0;
gano=false;
tiempo=0;
}
}
iteracion++;
if(iteracion==duracion_animacion)
{
cuadro_actual++;
if(cuadro_actual==3)
{
cuadro_actual=0;
}
iteracion=0;
}
if(personaje->x-150>=590&&personaje->x-150<=800&&personaje->y>=400&&personaje->y<=600){
gano =true;
personaje->x=0;
personaje->y=0;
}
for (int i=0;i<enemigos.size();i++){
if(personaje->x>=enemigos[i]->x&&personaje->y>=enemigos[i]->y){
perdio=true;
personaje->x=0;
personaje->y=0;
}
}
}
//Clean up
clean_up();
return 0;
}