-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
304 lines (238 loc) · 6.16 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
#include "game.h"
Game::Game(SDL_Renderer *_renderer) :
renderer(_renderer),
ball(renderer, 10.0f),
paddle(renderer, 20.0f),
text(renderer, "assets/fonts/D2Coding.ttf", 25),
text_bold(renderer, "assets/fonts/D2CodingBold.ttf", 25)
{
is_show_fps = false;
is_play = false;
key_left = false;
key_right = false;
// Set FPS
FPS = 60.0f;
// Get screen size.
SDL_GetRendererOutputSize(renderer, &screen_width, &screen_height);
// Init blocks vector.
block_size = 49;
blocks = (Block*)malloc(sizeof(Block) * block_size);
// Initialize Audio
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 500);
// Load audio files.
audio_block = Mix_LoadWAV("assets/audio/523762__matrixxx__select-granted-05.wav");
audio_paddle = Mix_LoadWAV("assets/audio/515823__matrixxx__select-granted-04.wav");
audio_start = Mix_LoadWAV("assets/audio/523763__matrixxx__select-granted-06.wav");
audio_gameover = Mix_LoadMUS("assets/audio/364929__jofae__game-die.wav");
audio_gameclear = Mix_LoadMUS("assets/audio/518308__mrthenoronha__world-clear-8-bit.wav");
}
void Game::init() {
is_gameover = false;
timer = 3;
fps_ticks = SDL_GetTicks();
timer_ticks = SDL_GetTicks();
// Set ball position.
ball.setPosition(screen_width/2, screen_height/2);
// Set ball initial direction.
ball.move(1, -1);
// Set paddle position.
paddle.setPosition(screen_width/2-50, screen_height-50);
// Set block arguments.
SDL_FPoint position = {0, 50};
SDL_Color *color;
// Vector index
int index = 0;
// Create blocks.
for (int y=0; y<7; y++) {
position.x = 50;
// Set color by lines
color = (SDL_Color*)(RANDOM_BLOCK_COLORS+y);
for (int x=0; x<7; x++) {
// Add new block.
blocks[index++] = Block(renderer, position, *color);
position.x += 100;
}
position.y += 20;
}
// Set block count.
block_count = block_size;
// Play start audio
Mix_PlayChannel(-1, audio_start, 0);
}
void Game::play() {
// Initalize the game.
init();
// Set game play flag.
is_play = true;
while (is_play) {
// Game loop
update();
// Check Events
event_listener();
}
}
void Game::stop() {
// Close the fonts
text.close();
text_bold.close();
// Free the audio files.
Mix_FreeChunk(audio_block);
Mix_FreeChunk(audio_paddle);
Mix_FreeChunk(audio_start);
Mix_FreeMusic(audio_gameover);
// Free memory
free(blocks);
// Game stop
is_play = false;
}
void Game::draw_entities() {
// Draw a ball.
ball.draw();
// Draw a paddle.
paddle.draw();
// Draw blocks.
for (int i=0; i<block_size; i++) {
blocks[i].draw();
}
}
void Game::show_fps() {
// Show FPS
text.setPosition(10, 10);
text.setAlign(-1);
text.print(std::to_string(SDL_GetTicks() - fps_ticks));
}
void Game::show_timer() {
text.setPosition(screen_width/2, screen_height/2-50);
text.setAlign(0);
text.print(std::to_string(timer));
}
void Game::show_gameover() {
// Game Over Message
text.setPosition(screen_width/2, screen_height/2 - (35*3));
text.setAlign(0);
text.print_lines("Game Over!\nR - Restart\nESC - Exit the game.", 35);
}
void Game::show_gameclear() {
// Game Clear Message
text.setPosition(screen_width/2, screen_height/2 - (35*2));
text.setAlign(0);
text.print_lines("- CONGRATURATION -\nYOU ARE WINNER!", 35);
}
void Game::display() {
if (is_show_fps) show_fps();
if (block_count <= 0) {
show_gameclear();
} else if (is_gameover) {
show_gameover();
}
// Start timer
if (timer > 0) {
show_timer();
// Timer
if (SDL_GetTicks()-timer_ticks >= 1000) {
timer --;
timer_ticks = SDL_GetTicks();
}
}
}
void Game::clear() {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
}
void Game::update() {
// FPS
if (SDL_GetTicks()-fps_ticks < (1000/FPS)) return;
fps_ticks = SDL_GetTicks();
// Paddle Move
if (key_right) paddle.moveRight();
if (key_left) paddle.moveLeft();
// Check paddle collision with a ball.
if (ball.collision(paddle.position, paddle.width, paddle.height, false)) {
// Play audio
Mix_PlayChannel(-1, audio_paddle, 0);
// Set position
SDL_FPoint position = ball.getPosition();
ball.setPosition(position.x, position.y-5);
// Ball flipping
ball.flip(false, true);
}
for (int i=0; i<block_size; i++) {
Block *block = &blocks[i];
// Check blocks collision with a ball.
if (!block->is_broken && ball.collision(block->position, block->width, block->height)) {
// Play audio
Mix_PlayChannel(-1, audio_block, 0);
// Check a block broken flag.
block->is_broken = true;
// Count
block_count --;
// Stop checking.
break;
}
}
if (timer <= 0 && !is_gameover) {
// Update ball position.
is_gameover = ball.update();
// Play gameover audio
if (is_gameover) Mix_PlayMusic(audio_gameover, 1);
}
if (block_count <= 0 && !is_gameover) {
// Game Clear!
// Play audio
Mix_PlayMusic(audio_gameclear, 1);
// Set gameover
is_gameover = true;
}
// Clear Screen
clear();
// Display
display();
// Draw
draw_entities();
SDL_RenderPresent(renderer);
}
void Game::event_listener() {
while (SDL_PollEvent(&event)) {
// Press 'X' button on window
if (event.type == SDL_QUIT) {
// Stop
stop();
}
// Keydown
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
// EXIT
stop();
break;
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
key_left = true;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
key_right = true;
break;
case SDL_SCANCODE_R:
// Restart the game.
init();
break;
default:
break;
}
} else if (event.type == SDL_KEYUP) {
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_A:
case SDL_SCANCODE_LEFT:
key_left = false;
break;
case SDL_SCANCODE_D:
case SDL_SCANCODE_RIGHT:
key_right = false;
break;
default:
break;
}
}
}
}