forked from compro-itkmitl/Animal-Fighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1427 lines (1243 loc) · 31.2 KB
/
main.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL, SDL_image, standard IO, vectors, and strings
#include <SDL.h> // main sdl lib
#include <SDL_image.h> // convert img lib
#include <stdio.h>
#include <string>
#include <SDL_mixer.h> // sound lib
//Screen dimension constants
const int SCREEN_WIDTH = 700;
const int SCREEN_HEIGHT = 540;
//A circle stucture
struct Circle
{
int x, y;
int r;
};
//Texture wrapper class
class LTexture
{
public:
//Initializes variables
LTexture();
//Deallocates memory
~LTexture();
//Loads image at specified path
bool loadFromFile(std::string path);
//Deallocates texture
void free();
//Set color modulation
void setColor(Uint8 red, Uint8 green, Uint8 blue);
//Set blending
void setBlendMode(SDL_BlendMode blending);
//Set alpha modulation
void setAlpha(Uint8 alpha);
//Renders texture at given point
void render(int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
void render2(int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
//Gets image dimensions
int getWidth();
int getHeight();
private:
//The actual hardware texture
SDL_Texture * mTexture;
//Image dimensions
int mWidth;
int mHeight;
};
//The dot that will move around on the screen
class Dot
{
public:
//The dimensions of the dot
static const int DOT_WIDTH = 20;
static const int DOT_HEIGHT = 20;
static const int count999 = 20;
//speed of the dot
float DOT_VEL = 0.3;
//Initializes the variables
Dot(float x, float y);
//Takes key presses and adjusts the dot's speed
void handleEvent(SDL_Event& e);
//Moves the dot and checks collision
int move(SDL_Rect& square, Circle& circle);
int move2(SDL_Rect& square, Circle& circle);
//Shows the dot on the screen
void render(int bird);
void render2();
//Gets collision circle
Circle& getCollider();
private:
//The X and Y offsets of the dot
float mPosX, mPosY;
//The speed of the dot
float mVelX, mVelY;
//Dot's collision circle
Circle mCollider;
//Moves the collision circle relative to the dot's offset
void shiftColliders();
};
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
bool loadMedia2();
//Frees media and shuts down SDL
void close();
//Circle/Circle collision detector
bool checkCollision(Circle& a, Circle& b);
//Circle/Box collision detector
bool checkCollision(Circle& a, SDL_Rect& b);
//Calculates distance squared between two points
double distanceSquared(int x1, int y1, int x2, int y2);
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
//The music that will be played
Mix_Music *musicstart = NULL;
Mix_Music *musicgameplay = NULL;
Mix_Music *music_victory = NULL;
Mix_Music *music_lose = NULL;
//The sound effects that will be used
Mix_Chunk *sound_crush = NULL;
Mix_Chunk *sound_explode = NULL;
// health of hero
int hp = 50;
// frame fo pic
const int picbird = 4;
SDL_Rect bird1[picbird]; // bird
// health bar
LTexture hpbar1, hpbar2, hpbar3, hpbar4, hpbar5, hpbar6;
//Scene textures
LTexture gDotTexture;
LTexture gBGTexture;
LTexture Oreo;
LTexture mon;
LTexture picbomb;
LTexture gboom;
LTexture gtower;
LTexture gameover;
LTexture gamewin;
LTexture gmenubar1, gmenubar2, gmenubar3;
LTexture::LTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture()
{
//Deallocate
free();
}
bool LTexture::loadFromFile(std::string path)
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
//Color key image
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
//Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
//Return success
mTexture = newTexture;
return mTexture != NULL;
}
void LTexture::free()
{
//Free texture if it exists
if (mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{
//Modulate texture rgb
SDL_SetTextureColorMod(mTexture, red, green, blue);
}
void LTexture::setBlendMode(SDL_BlendMode blending)
{
//Set blending function
SDL_SetTextureBlendMode(mTexture, blending);
}
void LTexture::setAlpha(Uint8 alpha)
{
//Modulate texture alpha
SDL_SetTextureAlphaMod(mTexture, alpha);
}
void LTexture::render(int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip)
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyEx(gRenderer, mTexture, clip, &renderQuad, angle, center, flip);
}
int LTexture::getWidth()
{
return mWidth;
}
int LTexture::getHeight()
{
return mHeight;
}
Dot::Dot(float x, float y)
{
//Initialize the offsets
mPosX = x;
mPosY = y;
//Set collision circle size
mCollider.r = DOT_WIDTH / 2;
//Initialize the velocity
mVelX = 0;
mVelY = 0;
//Move collider relative to the circle
shiftColliders();
}
void Dot::handleEvent(SDL_Event& e)
{
//If a key was pressed
if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
//Adjust the velocity
switch (e.key.keysym.sym)
{
case SDLK_UP: mVelY -= DOT_VEL; break;
case SDLK_DOWN: mVelY += DOT_VEL; break;
case SDLK_LEFT: mVelX -= DOT_VEL; break;
case SDLK_RIGHT: mVelX += DOT_VEL; break;
}
}
//If a key was released
else if (e.type == SDL_KEYUP && e.key.repeat == 0)
{
//Adjust the velocity
switch (e.key.keysym.sym)
{
case SDLK_UP: mVelY += DOT_VEL; break;
case SDLK_DOWN: mVelY -= DOT_VEL; break;
case SDLK_LEFT: mVelX += DOT_VEL; break;
case SDLK_RIGHT: mVelX -= DOT_VEL; break;
}
}
}
int y = 0;
int Dot::move(SDL_Rect& square, Circle& circle)
{
int x = 0;
//Move the dot left or right
mPosX += mVelX;
shiftColliders();
//If the dot collided or went too far to the left or right
if ((mPosX - mCollider.r < 0) || (mPosX + mCollider.r > SCREEN_WIDTH))
{
printf("wall");
//Move back
mPosX -= mVelX + 3;
x++;
y++;
if (mPosX <= 0) {
mPosX = 15;
}
/*if (y >= 3) {
printf("gameover");
}*/
shiftColliders();
}
//Move the dot up or down
mPosY += mVelY;
shiftColliders();
if (checkCollision(mCollider, square) || checkCollision(mCollider, circle)) {
// play sound when crush a monster;
Mix_PlayChannel(-1, sound_crush, 0);
printf("mon");
mPosX -= mVelX + 10;
hp--;
x = 1;
}
//If the dot collided or went too far up or down
if ((mPosY - mCollider.r < 0) || (mPosY + mCollider.r > SCREEN_HEIGHT))
{
printf("wall");
//Move back
mPosY -= mVelY;
x++;
y++;
if (mPosY <= 0) {
mPosY = 15;
}
shiftColliders();
}
return x;
}
int Dot::move2(SDL_Rect& square, Circle& circle)
{
int x = 0;
//Move the dot left or right
mPosX += mVelX;
shiftColliders();
//If the dot collided or went too far to the left or right
if ((mPosX - mCollider.r < 0) || (mPosX + mCollider.r > SCREEN_WIDTH))
{
printf("wall");
//Move back
mPosX -= mVelX + 3;
y++;
if (mPosX <= 0) {
mPosX = 15;
}
/*if (y >= 3) {
printf("gameover");
}*/
shiftColliders();
}
if (checkCollision(mCollider, square) || checkCollision(mCollider, circle)) {
printf("Bomb");
x = 1;
// play boom! sound;
Mix_PlayChannel(-1, sound_explode, 0);
printf("%d", x);
}
//Move the dot up or down
mPosY += mVelY;
shiftColliders();
//If the dot collided or went too far up or down
if ((mPosY - mCollider.r < 0) || (mPosY + mCollider.r > SCREEN_HEIGHT))
{
printf("wall");
//Move back
mPosY -= mVelY;
y++;
if (mPosY <= 0) {
mPosY = 15;
}
shiftColliders();
}
return x;
}
void Dot::render(int bird)
{
SDL_Rect* bird01 = &bird1[bird / 4];
gDotTexture.render(mPosX - mCollider.r, mPosY - mCollider.r, bird01);
}
void Dot::render2()
{
//Show the dot
mon.render(mPosX - mCollider.r, mPosY - mCollider.r);
}
Circle& Dot::getCollider()
{
return mCollider;
}
void Dot::shiftColliders()
{
//Align collider to center of dot
mCollider.x = mPosX;
mCollider.y = mPosY;
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
//Create window
gWindow = SDL_CreateWindow("Animal Fighter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create vsynced renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
//Initialize SDL_mixer
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
}
}
}
return success;
}
const int WALKING_ANIMATION_FRAMES = 4, picbomb2 = 7;
SDL_Rect gSpriteClips[WALKING_ANIMATION_FRAMES];
SDL_Rect pic01[picbomb2];
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load dot texture
if (!mon.loadFromFile("graphics/ani-two.png"))
{
printf("Failed to load walking animation texture!\n");
success = false;
}
if (!gtower.loadFromFile("graphics/tower.png"))
{
printf("Fail to loada tower.png");
success = false;
}
else
{
gSpriteClips[0].x = 0;
gSpriteClips[0].y = 0;
gSpriteClips[0].w = 57;
gSpriteClips[0].h = 44;
gSpriteClips[1].x = 57;
gSpriteClips[1].y = 0;
gSpriteClips[1].w = 57;
gSpriteClips[1].h = 44;
gSpriteClips[2].x = 0;
gSpriteClips[2].y = 0;
gSpriteClips[2].w = 57;
gSpriteClips[2].h = 44;
gSpriteClips[3].x = 57;
gSpriteClips[3].y = 0;
gSpriteClips[3].w = 57;
gSpriteClips[3].h = 44;
gSpriteClips[4].x = 0;
gSpriteClips[4].y = 0;
gSpriteClips[4].w = 57;
gSpriteClips[4].h = 44;
}
if (!picbomb.loadFromFile("graphics/bomb.png"))
{
printf("Failed to load dot texture!\n");
success = false;
}
else {
pic01[0].x = 0;
pic01[0].y = 0;
pic01[0].w = 167;
pic01[0].h = 158;
pic01[1].x = 167;
pic01[1].y = 0;
pic01[1].w = 167;
pic01[1].h = 158;
pic01[2].x = 334;
pic01[2].y = 0;
pic01[2].w = 167;
pic01[2].h = 158;
pic01[3].x = 0;
pic01[3].y = 316;
pic01[3].w = 167;
pic01[3].h = 158;
pic01[4].x = 167;
pic01[4].y = 316;
pic01[4].w = 167;
pic01[4].h = 158;
pic01[5].x = 334;
pic01[5].y = 316;
pic01[5].w = 167;
pic01[5].h = 158;
pic01[6].x = 0;
pic01[6].y = 474;
pic01[6].w = 167;
pic01[6].h = 158;
}
if (!gDotTexture.loadFromFile("graphics/ani-one.png"))
{
printf("Failed to load dot texture!\n");
success = false;
}
else {
bird1[0].x = 0;
bird1[0].y = 0;
bird1[0].w = 67;
bird1[0].h = 67;
bird1[1].x = 65;
bird1[1].y = 0;
bird1[1].w = 65;
bird1[1].h = 67;
bird1[2].x = 132;
bird1[2].y = 0;
bird1[2].w = 65;
bird1[2].h = 67;
bird1[3].x = 65;
bird1[3].y = 0;
bird1[3].w = 65;
bird1[3].h = 67;
}
//Load dot texture
if (!Oreo.loadFromFile("graphics/dot.bmp"))
{
printf("Failed to load dot texture!\n");
success = false;
}
//Load background texture
if (!gBGTexture.loadFromFile("graphics/bg3.png"))
{
printf("Failed to load background texture!\n");
success = false;
}
if (!gamewin.loadFromFile("graphics/gamewin.png"))
{
printf("Failed to load background texture!\n");
success = false;
}
if (!gameover.loadFromFile("graphics/gameover.png"))
{
printf("Failed to load background texture!\n");
success = false;
}
//Load sound effects
musicstart = Mix_LoadMUS("sound/musicstart.wav");
if (musicstart == NULL)
{
printf("Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
sound_crush = Mix_LoadWAV("sound/crush.wav");
if (sound_crush == NULL)
{
printf("Failed to load high sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
musicgameplay = Mix_LoadMUS("sound/musicgameplay.wav");
if (musicgameplay == NULL)
{
printf("Failed to load medium sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
music_victory = Mix_LoadMUS("sound/victory_sound.wav");
if (music_victory == NULL)
{
printf("Failed to load medium sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
music_lose = Mix_LoadMUS("sound/lose_sound.wav");
if (music_lose == NULL)
{
printf("Failed to load medium sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
sound_explode = Mix_LoadWAV("sound/explode.wav");
if (sound_explode == NULL)
{
printf("Failed to load low sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
if (!hpbar1.loadFromFile("graphics/hptap1.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
if (!hpbar2.loadFromFile("graphics/hptap2.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
if (!hpbar4.loadFromFile("graphics/hptap4.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
if (!hpbar5.loadFromFile("graphics/hptap5.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
if (!hpbar6.loadFromFile("graphics/hptap6.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
if (!gboom.loadFromFile("graphics/boom.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
if (!gmenubar1.loadFromFile("graphics/menu1.png"))
{
printf("Failed to load hp bar\n");
success = false;
}
return success;
}
//Walking animation
void close()
{
//Free loaded images
gDotTexture.free();
//Free the sound effects
Mix_FreeChunk(sound_crush);
Mix_FreeChunk(sound_explode);
musicgameplay = NULL;
sound_crush = NULL;
musicstart = NULL;
sound_explode = NULL;
//Free the music
Mix_FreeMusic(musicstart);
musicstart = NULL;
//Free the music
Mix_FreeMusic(musicgameplay);
musicgameplay = NULL;
//Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
bool checkCollision(Circle& a, Circle& b)
{
//Calculate total radius squared
int totalRadiusSquared = a.r + b.r;
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
//If the distance between the centers of the circles is less than the sum of their radii
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
{
//The circles have collided
return true;
}
//If not
return false;
}
bool checkCollision(Circle& a, SDL_Rect& b)
{
//Closest point on collision box
int cX, cY;
//Find closest x offset
if (a.x < b.x)
{
cX = b.x;
}
else if (a.x > b.x + b.w)
{
cX = b.x + b.w;
}
else
{
cX = a.x;
}
//Find closest y offset
if (a.y < b.y)
{
cY = b.y;
}
else if (a.y > b.y + b.h)
{
cY = b.y + b.h;
}
else
{
cY = a.y;
}
//If the closest point is inside the circle
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
{
//This box and the circle have collided
return true;
}
//If the shapes have not collided
return false;
}
double distanceSquared(int x1, int y1, int x2, int y2)
{
int deltaX = x2 - x1;
int deltaY = y2 - y1;
return deltaX * deltaX + deltaY * deltaY;
}
int main(int argc, char* args[])
{ //Start up SDL and create window
int i[5] = { 0,0,0,0,0 }, frame2[10] = {}, count = 0, count2 = 0, count3 = 0, count4 = 2, level = 2, value = 0, count5 = 4, frame = 0, time = 0, time1 = -1, time2 = -1, damage = 0, bossmove = 2, countbomb = 0;
struct aa {
SDL_Rect a{ 150,0,30,30 };
SDL_Rect a1{ 200,100,30,30 };
SDL_Rect a2{ 350,225,30,30 };
SDL_Rect a3{ 470,350,30,30 };
SDL_Rect a4{ 580,450,30,30 };
SDL_Rect ha{ 680,0,30,30 };
SDL_Rect h1{ 680,50,30,30 };
SDL_Rect h2{ 680,110,30,30 };
SDL_Rect h3{ 680,180,30,30 };
SDL_Rect h4{ 680,260,30,30 };
SDL_Rect h5{ 680,300,30,30 };
SDL_Rect h6{ 680,380,30,30 };
SDL_Rect h7{ 680,410,30,30 };
SDL_Rect boss{ 590,50,50,400 };
SDL_Rect bomb1{ 590,50,50,50 };
SDL_Rect bomb2{ 590,50,50,400 };
SDL_Rect bomb3{ 590,50,50,400 };
SDL_Rect bomb4{ 590,50,50,400 };
}a[99];
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
//Load media
if (!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
//Main loop flag
bool quit = false;
int menubar = 0;
//Event handler
SDL_Event e;
//The background scrolling offset
int scrollingOffset = 0;
int scrollingmenu = 0;
//The dot that will be moving around on the screen
Dot dot(Dot::DOT_WIDTH / 2, Dot::DOT_HEIGHT / 2);
Dot otherDot(3000, 3000);
//While application is running
a[count + 1].a4.y = 30;
a[count + 1].h6.y = 65;
a[count + 2].a4.y = 150;
a[count + 3].a4.y = 30;
a[count + 3].h6.y = 65;
a[count + 4].a4.y = 150;
a[count + 3].a4.x = 900;
a[count + 3].h6.x = 900;
a[count + 4].a4.x = 900;
a[count + 5].a4.y = 210;
a[count + 5].h6.y = 250;
a[count + 6].a4.y = 280;
a[count + 5].a4.x = 900;
a[count + 5].h6.x = 900;
a[count + 6].a4.x = 900;
a[count + 7].a4.x = 500;
while (menubar < 1 && !quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
if (e.key.keysym.sym == SDLK_RETURN) {
menubar++;
Mix_HaltMusic();
}
}
if (menubar == 0)
{
if(Mix_PlayingMusic() == 0){
Mix_PlayMusic(musicstart, -1);
}
}
//Scroll background
--scrollingOffset;
if (scrollingOffset < -gBGTexture.getWidth())
{
scrollingOffset = 0;
}
printf("%d", menubar);
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
//Render background
gBGTexture.render(scrollingOffset, 0);
gBGTexture.render(scrollingOffset + gBGTexture.getWidth(), 0);
gmenubar1.render(0, 0);
//Update screen
SDL_RenderPresent(gRenderer);
}
while (menubar == 1)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
//Handle input for the dot
dot.handleEvent(e);
}
if (Mix_PlayingMusic() == 0) {
Mix_PlayMusic(musicgameplay, -1);
}
//Scroll background
--scrollingOffset;
if (scrollingOffset < -gBGTexture.getWidth())
{
scrollingOffset = 0;
}
SDL_RenderClear(gRenderer);
//Clear screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
//Render background
gBGTexture.render(scrollingOffset, 0);
gBGTexture.render(scrollingOffset + gBGTexture.getWidth(), 0);
//
//boss
a[0].boss.y -= bossmove;
if (a[0].boss.y <= 3)
{
bossmove = -1;
}
else if (a[0].boss.y == 80)
{
bossmove = 2;
}
//
a[count].a.x -= level;
a[count].a1.x -= level;
a[count].a2.x -= level;
a[count].a3.x -= level;
a[count].a4.x -= level;
a[count].h3.x -= level;
a[count].ha.x -= level;