forked from flipperdevices/flipperzero-firmware
-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy patharkanoid_game.c
412 lines (345 loc) · 12.1 KB
/
arkanoid_game.c
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
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <stdlib.h>
#include <gui/view.h>
#define TAG "Arkanoid"
unsigned int COLUMNS = 13; //Columns of bricks
unsigned int ROWS = 4; //Rows of bricks
int dx = -1; //Initial movement of ball
int dy = -1; //Initial movement of ball
int xb; //Balls starting possition
int yb; //Balls starting possition
bool released; //If the ball has been released by the player
bool paused = false; //If the game has been paused
int xPaddle; //X position of paddle
bool isHit[4][13]; //Array of if bricks are hit or not
bool bounced = false; //Used to fix double bounce glitch
int lives = 3; //Amount of lives
int level = 1; //Current level
unsigned int score = 0; //Score for the game
unsigned int brickCount; //Amount of bricks hit
int pad1, pad2, pad3; //Button press buffer used to stop pause repeating
int oldpad, oldpad2, oldpad3;
char text[16]; //General string buffer
bool start = false; //If in menu or in game
bool initialDraw = false; //If the inital draw has happened
char initials[3]; //Initials used in high score
//Ball Bounds used in collision detection
int leftBall;
int rightBall;
int topBall;
int bottomBall;
//Brick Bounds used in collision detection
int leftBrick;
int rightBrick;
int topBrick;
int bottomBrick;
int tick;
#define FLIPPER_LCD_WIDTH 128
#define FLIPPER_LCD_HEIGHT 64
typedef enum { EventTypeTick, EventTypeKey } EventType;
typedef enum { DirectionUp, DirectionRight, DirectionDown, DirectionLeft } Direction;
typedef enum { GameStatePlaying, GameStateGameOver } GameState;
typedef struct {
int x;
int y;
} Point;
typedef struct {
GameState game_state;
} ArkanoidState;
typedef struct {
EventType type;
InputEvent input;
} GameEvent;
// generate number in range [min,max)
int rand_range(int min, int max) {
int number = min + rand() % (max - min);
return number;
}
void intro(Canvas* canvas) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 46, 0, "Arkanoid");
//arduboy.tunes.tone(987, 160);
//delay(160);
//arduboy.tunes.tone(1318, 400);
//delay(2000);
}
void move_ball(Canvas* canvas) {
tick++;
if(released) {
//Move ball
if(abs(dx) == 2) {
xb += dx / 2;
// 2x speed is really 1.5 speed
if(tick % 2 == 0) xb += dx / 2;
} else {
xb += dx;
}
yb = yb + dy;
//Set bounds
leftBall = xb;
rightBall = xb + 2;
topBall = yb;
bottomBall = yb + 2;
//Bounce off top edge
if(yb <= 0) {
yb = 2;
dy = -dy;
// arduboy.tunes.tone(523, 250);
}
//Lose a life if bottom edge hit
if(yb >= FLIPPER_LCD_HEIGHT) {
canvas_draw_frame(canvas, xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
xPaddle = 54;
yb = 60;
released = false;
lives--;
snprintf(text, sizeof(text), "LIVES:%u", lives);
canvas_draw_str(canvas, 0, 90, text);
// arduboy.tunes.tone(175, 250);
if(rand_range(0, 2) == 0) {
dx = 1;
} else {
dx = -1;
}
}
//Bounce off left side
if(xb <= 0) {
xb = 2;
dx = -dx;
// arduboy.tunes.tone(523, 250);
}
//Bounce off right side
if(xb >= FLIPPER_LCD_WIDTH - 2) {
xb = FLIPPER_LCD_WIDTH - 4;
dx = -dx;
// arduboy.tunes.tone(523, 250);
}
//Bounce off paddle
if(xb + 1 >= xPaddle && xb <= xPaddle + 12 && yb + 2 >= FLIPPER_LCD_HEIGHT - 1 &&
yb <= FLIPPER_LCD_HEIGHT) {
dy = -dy;
dx = ((xb - (xPaddle + 6)) / 3); //Applies spin on the ball
// prevent straight bounce
if(dx == 0) {
dx = (rand_range(0, 2) == 1) ? 1 : -1;
}
// arduboy.tunes.tone(200, 250);
}
//Bounce off Bricks
for(unsigned int row = 0; row < ROWS; row++) {
for(unsigned int column = 0; column < COLUMNS; column++) {
if(!isHit[row][column]) {
//Sets Brick bounds
leftBrick = 10 * column;
rightBrick = 10 * column + 10;
topBrick = 6 * row + 1;
bottomBrick = 6 * row + 7;
//If A collison has occured
if(topBall <= bottomBrick && bottomBall >= topBrick &&
leftBall <= rightBrick && rightBall >= leftBrick) {
// Draw score
score += (level * 10);
snprintf(text, sizeof(text), "SCORE:%u", score);
canvas_draw_str(canvas, 80, 90, text);
brickCount++;
isHit[row][column] = true;
canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
//Vertical collision
if(bottomBall > bottomBrick || topBall < topBrick) {
//Only bounce once each ball move
if(!bounced) {
dy = -dy;
yb += dy;
bounced = true;
// arduboy.tunes.tone(261, 250);
}
}
//Hoizontal collision
if(leftBall < leftBrick || rightBall > rightBrick) {
//Only bounce once brick each ball move
if(!bounced) {
dx = -dx;
xb += dx;
bounced = true;
// arduboy.tunes.tone(261, 250);
}
}
}
}
}
}
//Reset Bounce
bounced = false;
} else {
//Ball follows paddle
xb = xPaddle + 5;
}
}
void draw_lives(Canvas* canvas) {
sprintf(text, "LIVES:%u", lives);
canvas_draw_str(canvas, 0, 90, text);
}
void draw_ball(Canvas* canvas) {
canvas_draw_dot(canvas, xb, yb);
canvas_draw_dot(canvas, xb + 1, yb);
canvas_draw_dot(canvas, xb, yb + 1);
canvas_draw_dot(canvas, xb + 1, yb + 1);
move_ball(canvas);
}
void draw_paddle(Canvas* canvas) {
canvas_draw_frame(canvas, xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
}
void reset_level(Canvas* canvas) {
//Undraw paddle
canvas_draw_frame(canvas, xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
//Undraw ball
canvas_draw_dot(canvas, xb, yb);
canvas_draw_dot(canvas, xb + 1, yb);
canvas_draw_dot(canvas, xb, yb + 1);
canvas_draw_dot(canvas, xb + 1, yb + 1);
//Alter various variables to reset the game
xPaddle = 54;
yb = 60;
brickCount = 0;
released = false;
// Reset all brick hit states
for(unsigned int row = 0; row < ROWS; row++) {
for(unsigned int column = 0; column < COLUMNS; column++) {
isHit[row][column] = false;
}
}
}
static void arkanoid_state_init(ArkanoidState* const arkanoid_state) {
// Set the initial game state
arkanoid_state->game_state = GameStatePlaying;
// Reset initial state
initialDraw = false;
}
static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) {
const ArkanoidState* arkanoid_state = acquire_mutex((ValueMutex*)ctx, 25);
if(arkanoid_state == NULL) {
return;
}
//Initial level draw
if(!initialDraw) {
initialDraw = true;
// Set default font for text
canvas_set_font(canvas, FontPrimary);
//Draws the new level
reset_level(canvas);
}
//Draws new bricks and resets their values
for(unsigned int row = 0; row < ROWS; row++) {
for(unsigned int column = 0; column < COLUMNS; column++) {
if(!isHit[row][column]) {
canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
}
}
}
if(lives > 0) {
draw_paddle(canvas);
draw_ball(canvas);
if(brickCount == ROWS * COLUMNS) {
level++;
reset_level(canvas);
}
} else {
reset_level(canvas);
initialDraw = false;
start = false;
lives = 3;
score = 0;
}
release_mutex((ValueMutex*)ctx, arkanoid_state);
}
static void arkanoid_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
GameEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
static void arkanoid_update_timer_callback(FuriMessageQueue* event_queue) {
furi_assert(event_queue);
GameEvent event = {.type = EventTypeTick};
furi_message_queue_put(event_queue, &event, 0);
}
int32_t arkanoid_game_app(void* p) {
UNUSED(p);
// Set random seed from interrupts
srand(DWT->CYCCNT);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
ArkanoidState* arkanoid_state = malloc(sizeof(ArkanoidState));
arkanoid_state_init(arkanoid_state);
ValueMutex state_mutex;
if(!init_mutex(&state_mutex, arkanoid_state, sizeof(ArkanoidState))) {
FURI_LOG_E(TAG, "Cannot create mutex\r\n");
free(arkanoid_state);
return 255;
}
// Set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, arkanoid_draw_callback, &state_mutex);
view_port_input_callback_set(view_port, arkanoid_input_callback, event_queue);
FuriTimer* timer =
furi_timer_alloc(arkanoid_update_timer_callback, FuriTimerTypePeriodic, event_queue);
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
// Open GUI and register view_port
Gui* gui = furi_record_open("gui");
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
GameEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
ArkanoidState* arkanoid_state = (ArkanoidState*)acquire_mutex_block(&state_mutex);
if(event_status == FuriStatusOk) {
// Key events
if(event.type == EventTypeKey) {
if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
event.input.type == InputTypeRepeat) {
switch(event.input.key) {
case InputKeyBack:
processing = false;
break;
case InputKeyRight:
if(xPaddle < FLIPPER_LCD_WIDTH - 12) {
xPaddle += 8;
}
break;
case InputKeyLeft:
if(xPaddle > 0) {
xPaddle -= 8;
}
break;
case InputKeyUp:
break;
case InputKeyDown:
break;
case InputKeyOk:
//Release ball if FIRE pressed
released = true;
//Apply random direction to ball on release
if(rand_range(0, 2) == 0) {
dx = 1;
} else {
dx = -1;
}
//Makes sure the ball heads upwards
dy = -1;
break;
}
}
}
} else {
// Event timeout
FURI_LOG_D(TAG, "osMessageQueue: Event timeout");
}
view_port_update(view_port);
release_mutex(&state_mutex, arkanoid_state);
}
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close("gui");
view_port_free(view_port);
furi_message_queue_free(event_queue);
return 0;
}