-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
351 lines (310 loc) · 11.8 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
// LIBRARY
#include <iostream>
#include <ctime>
#include "painter.h"
#include "number.h"
#include "music.h"
#include "check and init.h"
using namespace std;
// LIST CONST
//--------------------------------------------------------------------------------
const int dlevel = 1, dlife = 5,dscore = 0, djump = 4,dloop = 99,AddJump = 4;
const int scoreLevel2 = 21, scoreLevel3 = 41, scoreLevel4 = 61;
int level = dlevel,life = dlife, score = dscore, jump = djump;
const int widthArray = 3, heightArray = 9;
const int lengthPic = 100,lengthBreakHeat = 600,heighNumber = 50;
const int distanceLeft = 50,distanceRight = 450,distanceStartPointOfTowPic = 150;
const SDL_Rect startRedHeart{50,200,150,300};
const SDL_Rect startBlackHeart{550,200,650,300};
//---------------------------------------------------------------------------------
// LIST TYPE AND FUNTIONS
//---------------------------------------------------------------------------------
typedef struct picture
{
int x = NULL;
int y = NULL;
PictureID arr ;
};
int startScreen(Painter painter, Music music, SDL_Renderer* renderer);
void check(picture Array[][widthArray], Painter painter, Music music, int& counts,
const bool& checkHeart, const bool& checkHeartAtLast);
void mouse(picture Array[][widthArray], Music music, const int& counts);
void randomPicture(picture Array[][widthArray], const bool& random,const bool& move);
//-------------------------------------------------------------------------------
// MAIN
//*******************************************************************************
int main(int argc,char* argv[]){
srand(time(0));
//INITIALIZE
SDL_Window* window;
SDL_Renderer* renderer;
initSDL(window, renderer);
Painter painter(window, renderer);
Music music;
RightBackGround rightBackGround(renderer);
// PLAY
while(true)
{
int t = 0, counts = 0;
if(!startScreen(painter, music, renderer))
{
quitSDL(window, renderer);
return 0;
}
// init ARRAY
picture Array[heightArray][widthArray];
for (int i = 0; i < heightArray; i++)
{
for (int j = 0; j < widthArray; j++)
{
Array[i][j].arr = PIC_NULL;
Array[i][j].x = distanceLeft + distanceStartPointOfTowPic*j;
Array[i][j].y = lengthPic*(i-1);
}
}
for (int a = 0; a < heightArray - 4; a++)
randomPicture(Array, true, true);
// START
while(life > 0)
{
// BACKGROUND MUSIC
if (level == 1 ) music.sound(level1, dloop);
else if (level == 2) music.sound(level2, dloop);
else if (level == 3) music.sound(level3, dloop);
else if (level == 4) music.sound(level4, dloop);
while(life > 0)
{
bool checks = true;
randomPicture(Array, true, false);
// RUN
while(counts <= lengthPic)
{
SDL_RenderClear(renderer);
painter.DrawImage(0, 0, SCREEN_WIDTH, BackGround);
mouse(Array, music, counts);
check(Array, painter, music, counts, true, false);
// CONDITION PLAY KIND OF BACKGROUND MUSIC
if ((score == scoreLevel2 && t == 0)||(score == scoreLevel3 && t == 1)
||(score == scoreLevel4 && t == 2))
{
t++;
checks = false;
break;
}
// JUMP IMAGE
counts = counts + jump;
// PRINT SCORE, LIFE, LEVEL AT RIGHT
if (life > 0)
{
rightBackGround.createRightBackGround(positionScore, heighNumber ,score);
rightBackGround.createRightBackGround(positionLevel, heighNumber, level);
rightBackGround.createRightBackGround(positionLife, heighNumber, life);
SDL_RenderPresent(renderer);
}
}
if (counts >= lengthPic) counts = 0;
if (checks == false) break;
// MOVE ARRAY
randomPicture(Array, false, true);
// CHECK
check(Array, painter, music, counts, false, true);
// CHECK LIFE
if (life < 1)
{
painter.DrawImage(0, 0, lengthBreakHeat, BreakHeart);
rightBackGround.createRightBackGround(positionScore, heighNumber, score);
rightBackGround.createRightBackGround(positionLevel, heighNumber, level);
rightBackGround.createRightBackGround(positionLife, heighNumber, life);
SDL_RenderPresent(renderer);
}
}
Mix_HaltChannel(-1);
Mix_CloseAudio();
}
// FINISH PLAYING
Mix_HaltChannel(-1);
music.sound(gameOver,0);
SDL_Delay(3000);
Mix_HaltChannel(-1);
Mix_CloseAudio();
// init NEW ROUND
life = dlife;
score = dscore;
level = dlevel;
jump = djump;
}
waitUntilKeyPressed();
quitSDL(window, renderer);
return 0;
}
//**********************************************************************************
//----------------------------------------------------------------------------------
int startScreen(Painter painter, Music music, SDL_Renderer* renderer)
{
SDL_Event es;
music.sound(startscreen, dloop);
while (true)
{
painter.DrawImage(0, 0, SCREEN_WIDTH, StartScreen);
if (SDL_WaitEvent(&es) == 0) break;
else
{
if (es.button.x >= startRedHeart.x && es.button.x <= startRedHeart.w
&& es.button.y >= startRedHeart.y && es.button.y <= startRedHeart.h)
{
painter.DrawImage(startRedHeart.x, startRedHeart.y, lengthPic, RedHeart);
SDL_RenderPresent(renderer);
}
if (es.button.x >= startBlackHeart.x && es.button.x <= startBlackHeart.w
&& es.button.y >= startBlackHeart.y && es.button.y <= startBlackHeart.h)
{
painter.DrawImage(startBlackHeart.x, startBlackHeart.y, lengthPic, BlackHeart);
SDL_RenderPresent(renderer);
}
if (es.type == SDL_MOUSEBUTTONDOWN && es.button.y >= startRedHeart.y && es.button.y <= startRedHeart.h)
{
if(es.button.x >= startRedHeart.x && es.button.x <= startRedHeart.w)
{
music.sound(redHeart, 0);
break;
}
else if (es.button.x >= startBlackHeart.x && es.button.x <= startBlackHeart.w)
{
music.sound(blackHeart, 0);
SDL_Delay(1000);
return 0;
}
}
}
SDL_RenderPresent(renderer);
}
SDL_Delay(500);
Mix_HaltChannel(-1);
return 1;
}
//-------------------------------------------------------------------------------------------------------
void check(picture Array[][widthArray], Painter painter, Music music, int& counts,
const bool& checkHeart, const bool& checkHeartAtLast)
{
if (checkHeart)
{
// LOAD IMAGE TO SCREEN
for (int i = 0; i < heightArray; i++)
{
for (int j = 0; j < widthArray; j++)
{
if (Array[i][j].arr == PIC_RED_HEART)
painter.DrawImage(Array[i][j].x, Array[i][j].y + counts, lengthPic, RedHeart);
else if (Array[i][j].arr == PIC_BLACK_HEART)
painter.DrawImage(Array[i][j].x, Array[i][j].y + counts, lengthPic, BlackHeart);
}
}
// CHECK LEVEL
if (level == 1 && score >= scoreLevel2)
{
//music.sound(levelUp,0);
level = 2;
jump += AddJump;
}
else if (level == 2 && score >= scoreLevel3)
{
//music.sound(levelUp,0);
level = 3;
jump += AddJump;
}
else if (level == 3 && score >= scoreLevel4)
{
//music.sound(levelUp,0);
level = 4;
jump += AddJump;
}
}
if (checkHeartAtLast)
{
for (int j = 0; j < widthArray; j++)
if(Array[heightArray-1][j].arr != PIC_NULL)
{
if (life > 0) life --;
music.sound(blackHeart, 0);
}
}
}
//---------------------------------------------------------------------------------------------------------
void mouse(picture Array[][widthArray], Music music, const int& counts)
{
SDL_Event e;
while(SDL_PollEvent(&e))
{
if ( e.type == SDL_MOUSEBUTTONDOWN && e.button.x >= distanceLeft && e.button.x <= distanceRight)
{
int i,j = 4;
for (int a = 0; a < widthArray; a++)
{
if ( e.button.x >= distanceLeft + distanceStartPointOfTowPic*a
&& e.button.x <= distanceStartPointOfTowPic*(a+1))
{
j = a;
break;
}
}
for (int a=-1; a < heightArray-1; a++)
if ( e.button.y >= lengthPic*a + counts && e.button.y <= lengthPic*(a+1) -1 + counts) i = a+1;
if ( counts == 0 && e.button.y % lengthPic == 0 ) i++;
if ( e.button.button == SDL_BUTTON_LEFT )
{
if ( Array[i][j].arr == PIC_RED_HEART )
{
score++;
music.sound(redHeart, 0);
}
else if ( Array[i][j].arr == PIC_BLACK_HEART )
{
life--;
music.sound(blackHeart, 0);
}
}
else
{
if ( Array[i][j].arr == PIC_BLACK_HEART )
{
score++;
music.sound(redHeart, 0);
}
else if ( Array[i][j].arr == PIC_RED_HEART )
{
life--;
music.sound(blackHeart, 0);
}
}
Array[i][j].arr = PIC_NULL;
}
}
}
//-------------------------------------------------------------------------------------------------------
void randomPicture( picture Array[][widthArray], const bool& random,const bool& move)
{
if(random)
{
int levelHeart = level;
if ( level > 3 ) levelHeart = widthArray;
// INIT heart for array
for (int j=0; j < widthArray; j++) Array[0][j].arr = PIC_NULL;
for (int i=0; i < levelHeart; i++)
{
int x = rand() % widthArray,y = rand() % 2;
if (Array[0][x].arr != PIC_BLACK_HEART&&Array[0][x].arr != PIC_RED_HEART)
{
if (y) Array[0][x].arr = PIC_RED_HEART;
else Array[0][x].arr = PIC_BLACK_HEART;
}
else i--;
}
}
if (move)
{
for (int i = heightArray-1; i > 0; i--)
for (int j = 0; j< widthArray; j++)
Array[i][j].arr = Array[i-1][j].arr;
}
}
//**********************************************************************************************************