2022-06-21 02:50:34 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <gui/view.h>
|
2022-08-07 06:59:03 +00:00
|
|
|
#include <notification/notification.h>
|
|
|
|
#include <notification/notification_messages.h>
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
#define TAG "Arkanoid"
|
|
|
|
|
|
|
|
#define FLIPPER_LCD_WIDTH 128
|
|
|
|
#define FLIPPER_LCD_HEIGHT 64
|
2022-10-07 20:09:17 +00:00
|
|
|
#define MAX_SPEED 3
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
typedef enum { EventTypeTick, EventTypeKey } EventType;
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
typedef struct {
|
|
|
|
//Brick Bounds used in collision detection
|
|
|
|
int leftBrick;
|
|
|
|
int rightBrick;
|
|
|
|
int topBrick;
|
|
|
|
int bottomBrick;
|
|
|
|
bool isHit[4][13]; //Array of if bricks are hit or not
|
|
|
|
} BrickState;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2022-08-07 06:59:03 +00:00
|
|
|
int dx; //Initial movement of ball
|
|
|
|
int dy; //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
|
|
|
|
//Ball Bounds used in collision detection
|
|
|
|
int leftBall;
|
|
|
|
int rightBall;
|
|
|
|
int topBall;
|
|
|
|
int bottomBall;
|
|
|
|
} BallState;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2022-08-07 06:59:03 +00:00
|
|
|
BallState ball_state;
|
|
|
|
BrickState brick_state;
|
|
|
|
NotificationApp* notify;
|
|
|
|
unsigned int COLUMNS; //Columns of bricks
|
|
|
|
unsigned int ROWS; //Rows of bricks
|
|
|
|
bool initialDraw; //If the inital draw has happened
|
|
|
|
int xPaddle; //X position of paddle
|
|
|
|
char text[16]; //General string buffer
|
|
|
|
bool bounced; //Used to fix double bounce glitch
|
|
|
|
int lives; //Amount of lives
|
|
|
|
int level; //Current level
|
|
|
|
unsigned int score; //Score for the game
|
|
|
|
unsigned int brickCount; //Amount of bricks hit
|
|
|
|
int tick; //Tick counter
|
2022-09-11 08:15:03 +00:00
|
|
|
bool gameStarted; // Did the game start?
|
2022-10-07 20:09:17 +00:00
|
|
|
int speed; // Ball speed
|
2022-06-21 02:50:34 +00:00
|
|
|
} ArkanoidState;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
EventType type;
|
|
|
|
InputEvent input;
|
|
|
|
} GameEvent;
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
static const NotificationSequence sequence_short_sound = {
|
|
|
|
&message_note_c5,
|
|
|
|
&message_delay_50,
|
|
|
|
&message_sound_off,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2022-06-21 02:50:34 +00:00
|
|
|
// generate number in range [min,max)
|
|
|
|
int rand_range(int min, int max) {
|
2022-09-14 05:46:40 +00:00
|
|
|
return min + rand() % (max - min);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
void move_ball(Canvas* canvas, ArkanoidState* st) {
|
|
|
|
st->tick++;
|
2022-10-07 20:09:17 +00:00
|
|
|
|
2022-10-12 02:01:37 +00:00
|
|
|
int current_speed = abs(st->speed - 1 - MAX_SPEED);
|
|
|
|
if(st->tick % current_speed != 0 && st->tick % (current_speed + 1) != 0) {
|
2022-10-07 20:09:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.released) {
|
2022-06-21 02:50:34 +00:00
|
|
|
//Move ball
|
2022-08-07 06:59:03 +00:00
|
|
|
if(abs(st->ball_state.dx) == 2) {
|
|
|
|
st->ball_state.xb += st->ball_state.dx / 2;
|
2022-06-21 02:50:34 +00:00
|
|
|
// 2x speed is really 1.5 speed
|
2022-10-07 20:09:17 +00:00
|
|
|
if((st->tick / current_speed) % 2 == 0) st->ball_state.xb += st->ball_state.dx / 2;
|
2022-06-21 02:50:34 +00:00
|
|
|
} else {
|
2022-08-07 06:59:03 +00:00
|
|
|
st->ball_state.xb += st->ball_state.dx;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
2022-08-07 06:59:03 +00:00
|
|
|
st->ball_state.yb = st->ball_state.yb + st->ball_state.dy;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
//Set bounds
|
2022-08-07 06:59:03 +00:00
|
|
|
st->ball_state.leftBall = st->ball_state.xb;
|
|
|
|
st->ball_state.rightBall = st->ball_state.xb + 2;
|
|
|
|
st->ball_state.topBall = st->ball_state.yb;
|
|
|
|
st->ball_state.bottomBall = st->ball_state.yb + 2;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
//Bounce off top edge
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.yb <= 0) {
|
|
|
|
st->ball_state.yb = 2;
|
|
|
|
st->ball_state.dy = -st->ball_state.dy;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Lose a life if bottom edge hit
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.yb >= FLIPPER_LCD_HEIGHT) {
|
|
|
|
canvas_draw_frame(canvas, st->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
|
|
|
|
st->xPaddle = 54;
|
|
|
|
st->ball_state.yb = 60;
|
|
|
|
st->ball_state.released = false;
|
|
|
|
st->lives--;
|
2022-09-11 08:15:03 +00:00
|
|
|
st->gameStarted = false;
|
2022-08-07 06:59:03 +00:00
|
|
|
|
2022-06-21 02:50:34 +00:00
|
|
|
if(rand_range(0, 2) == 0) {
|
2022-08-07 06:59:03 +00:00
|
|
|
st->ball_state.dx = 1;
|
2022-06-21 02:50:34 +00:00
|
|
|
} else {
|
2022-08-07 06:59:03 +00:00
|
|
|
st->ball_state.dx = -1;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Bounce off left side
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.xb <= 0) {
|
|
|
|
st->ball_state.xb = 2;
|
|
|
|
st->ball_state.dx = -st->ball_state.dx;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Bounce off right side
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.xb >= FLIPPER_LCD_WIDTH - 2) {
|
|
|
|
st->ball_state.xb = FLIPPER_LCD_WIDTH - 4;
|
|
|
|
st->ball_state.dx = -st->ball_state.dx;
|
2022-06-21 02:50:34 +00:00
|
|
|
// arduboy.tunes.tone(523, 250);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Bounce off paddle
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.xb + 1 >= st->xPaddle && st->ball_state.xb <= st->xPaddle + 12 &&
|
|
|
|
st->ball_state.yb + 2 >= FLIPPER_LCD_HEIGHT - 1 &&
|
|
|
|
st->ball_state.yb <= FLIPPER_LCD_HEIGHT) {
|
|
|
|
st->ball_state.dy = -st->ball_state.dy;
|
|
|
|
st->ball_state.dx =
|
|
|
|
((st->ball_state.xb - (st->xPaddle + 6)) / 3); //Applies spin on the ball
|
|
|
|
// prevent straight bounce, but not prevent roguuemaster from stealing
|
|
|
|
if(st->ball_state.dx == 0) {
|
|
|
|
st->ball_state.dx = (rand_range(0, 2) == 1) ? 1 : -1;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Bounce off Bricks
|
2022-08-07 06:59:03 +00:00
|
|
|
for(unsigned int row = 0; row < st->ROWS; row++) {
|
|
|
|
for(unsigned int column = 0; column < st->COLUMNS; column++) {
|
|
|
|
if(!st->brick_state.isHit[row][column]) {
|
2022-06-21 02:50:34 +00:00
|
|
|
//Sets Brick bounds
|
2022-08-07 06:59:03 +00:00
|
|
|
st->brick_state.leftBrick = 10 * column;
|
|
|
|
st->brick_state.rightBrick = 10 * column + 10;
|
|
|
|
st->brick_state.topBrick = 6 * row + 1;
|
|
|
|
st->brick_state.bottomBrick = 6 * row + 7;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
//If A collison has occured
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.topBall <= st->brick_state.bottomBrick &&
|
|
|
|
st->ball_state.bottomBall >= st->brick_state.topBrick &&
|
|
|
|
st->ball_state.leftBall <= st->brick_state.rightBrick &&
|
|
|
|
st->ball_state.rightBall >= st->brick_state.leftBrick) {
|
|
|
|
st->score += st->level;
|
|
|
|
// Blink led when we hit some brick
|
|
|
|
notification_message(st->notify, &sequence_short_sound);
|
2022-08-07 07:52:03 +00:00
|
|
|
//notification_message(st->notify, &sequence_blink_white_100);
|
2022-08-07 06:59:03 +00:00
|
|
|
|
|
|
|
st->brickCount++;
|
|
|
|
st->brick_state.isHit[row][column] = true;
|
2022-06-21 02:50:34 +00:00
|
|
|
canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
|
|
|
|
|
|
|
|
//Vertical collision
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.bottomBall > st->brick_state.bottomBrick ||
|
|
|
|
st->ball_state.topBall < st->brick_state.topBrick) {
|
2022-06-21 02:50:34 +00:00
|
|
|
//Only bounce once each ball move
|
2022-08-07 06:59:03 +00:00
|
|
|
if(!st->bounced) {
|
|
|
|
st->ball_state.dy = -st->ball_state.dy;
|
|
|
|
st->ball_state.yb += st->ball_state.dy;
|
|
|
|
st->bounced = true;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Hoizontal collision
|
2022-08-07 06:59:03 +00:00
|
|
|
if(st->ball_state.leftBall < st->brick_state.leftBrick ||
|
|
|
|
st->ball_state.rightBall > st->brick_state.rightBrick) {
|
2022-06-21 02:50:34 +00:00
|
|
|
//Only bounce once brick each ball move
|
2022-08-07 06:59:03 +00:00
|
|
|
if(!st->bounced) {
|
|
|
|
st->ball_state.dx = -st->ball_state.dx;
|
|
|
|
st->ball_state.xb += st->ball_state.dx;
|
|
|
|
st->bounced = true;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Reset Bounce
|
2022-08-07 06:59:03 +00:00
|
|
|
st->bounced = false;
|
2022-06-21 02:50:34 +00:00
|
|
|
} else {
|
|
|
|
//Ball follows paddle
|
2022-08-07 06:59:03 +00:00
|
|
|
st->ball_state.xb = st->xPaddle + 5;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
void draw_lives(Canvas* canvas, ArkanoidState* arkanoid_state) {
|
|
|
|
if(arkanoid_state->lives == 3) {
|
2022-08-07 01:37:26 +00:00
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
|
|
|
|
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 11);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 11);
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 12);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 12);
|
|
|
|
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 15);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 15);
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 16);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 16);
|
2022-08-07 06:59:03 +00:00
|
|
|
} else if(arkanoid_state->lives == 2) {
|
2022-08-07 01:37:26 +00:00
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
|
|
|
|
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 11);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 11);
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 12);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 12);
|
|
|
|
} else {
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
|
|
|
|
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
|
|
|
|
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
void draw_score(Canvas* canvas, ArkanoidState* arkanoid_state) {
|
|
|
|
snprintf(arkanoid_state->text, sizeof(arkanoid_state->text), "%u", arkanoid_state->score);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas,
|
|
|
|
FLIPPER_LCD_WIDTH - 2,
|
|
|
|
FLIPPER_LCD_HEIGHT - 6,
|
|
|
|
AlignRight,
|
|
|
|
AlignBottom,
|
|
|
|
arkanoid_state->text);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
void draw_ball(Canvas* canvas, ArkanoidState* ast) {
|
|
|
|
canvas_draw_dot(canvas, ast->ball_state.xb, ast->ball_state.yb);
|
|
|
|
canvas_draw_dot(canvas, ast->ball_state.xb + 1, ast->ball_state.yb);
|
|
|
|
canvas_draw_dot(canvas, ast->ball_state.xb, ast->ball_state.yb + 1);
|
|
|
|
canvas_draw_dot(canvas, ast->ball_state.xb + 1, ast->ball_state.yb + 1);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
move_ball(canvas, ast);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
void draw_paddle(Canvas* canvas, ArkanoidState* arkanoid_state) {
|
|
|
|
canvas_draw_frame(canvas, arkanoid_state->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
void reset_level(Canvas* canvas, ArkanoidState* arkanoid_state) {
|
2022-06-21 02:50:34 +00:00
|
|
|
//Undraw paddle
|
2022-08-07 06:59:03 +00:00
|
|
|
canvas_draw_frame(canvas, arkanoid_state->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
//Undraw ball
|
2022-08-07 06:59:03 +00:00
|
|
|
canvas_draw_dot(canvas, arkanoid_state->ball_state.xb, arkanoid_state->ball_state.yb);
|
|
|
|
canvas_draw_dot(canvas, arkanoid_state->ball_state.xb + 1, arkanoid_state->ball_state.yb);
|
|
|
|
canvas_draw_dot(canvas, arkanoid_state->ball_state.xb, arkanoid_state->ball_state.yb + 1);
|
|
|
|
canvas_draw_dot(canvas, arkanoid_state->ball_state.xb + 1, arkanoid_state->ball_state.yb + 1);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
//Alter various variables to reset the game
|
2022-08-07 06:59:03 +00:00
|
|
|
arkanoid_state->xPaddle = 54;
|
|
|
|
arkanoid_state->ball_state.yb = 60;
|
|
|
|
arkanoid_state->brickCount = 0;
|
|
|
|
arkanoid_state->ball_state.released = false;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
// Reset all brick hit states
|
2022-08-07 06:59:03 +00:00
|
|
|
for(unsigned int row = 0; row < arkanoid_state->ROWS; row++) {
|
|
|
|
for(unsigned int column = 0; column < arkanoid_state->COLUMNS; column++) {
|
|
|
|
arkanoid_state->brick_state.isHit[row][column] = false;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
static void arkanoid_state_init(ArkanoidState* arkanoid_state) {
|
|
|
|
// Init notification
|
|
|
|
arkanoid_state->notify = furi_record_open(RECORD_NOTIFICATION);
|
|
|
|
|
2022-06-21 02:50:34 +00:00
|
|
|
// Set the initial game state
|
2022-08-07 06:59:03 +00:00
|
|
|
arkanoid_state->COLUMNS = 13;
|
|
|
|
arkanoid_state->ROWS = 4;
|
|
|
|
arkanoid_state->ball_state.dx = -1;
|
|
|
|
arkanoid_state->ball_state.dy = -1;
|
2022-10-07 20:09:17 +00:00
|
|
|
arkanoid_state->speed = 2;
|
2022-08-07 06:59:03 +00:00
|
|
|
arkanoid_state->bounced = false;
|
|
|
|
arkanoid_state->lives = 3;
|
|
|
|
arkanoid_state->level = 1;
|
|
|
|
arkanoid_state->score = 0;
|
|
|
|
arkanoid_state->COLUMNS = 13;
|
|
|
|
arkanoid_state->COLUMNS = 13;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
// Reset initial state
|
2022-08-07 06:59:03 +00:00
|
|
|
arkanoid_state->initialDraw = false;
|
2022-09-11 08:15:03 +00:00
|
|
|
arkanoid_state->gameStarted = false;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) {
|
2022-08-07 06:59:03 +00:00
|
|
|
ArkanoidState* arkanoid_state = acquire_mutex((ValueMutex*)ctx, 25);
|
2022-06-21 02:50:34 +00:00
|
|
|
if(arkanoid_state == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Initial level draw
|
2022-08-07 06:59:03 +00:00
|
|
|
if(!arkanoid_state->initialDraw) {
|
|
|
|
arkanoid_state->initialDraw = true;
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
// Set default font for text
|
2022-08-07 06:59:03 +00:00
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
//Draws the new level
|
2022-08-07 06:59:03 +00:00
|
|
|
reset_level(canvas, arkanoid_state);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Draws new bricks and resets their values
|
2022-08-07 06:59:03 +00:00
|
|
|
for(unsigned int row = 0; row < arkanoid_state->ROWS; row++) {
|
|
|
|
for(unsigned int column = 0; column < arkanoid_state->COLUMNS; column++) {
|
|
|
|
if(!arkanoid_state->brick_state.isHit[row][column]) {
|
2022-06-21 02:50:34 +00:00
|
|
|
canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
if(arkanoid_state->lives > 0) {
|
|
|
|
draw_paddle(canvas, arkanoid_state);
|
|
|
|
draw_ball(canvas, arkanoid_state);
|
|
|
|
draw_score(canvas, arkanoid_state);
|
|
|
|
draw_lives(canvas, arkanoid_state);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
2022-08-07 06:59:03 +00:00
|
|
|
if(arkanoid_state->brickCount == arkanoid_state->ROWS * arkanoid_state->COLUMNS) {
|
|
|
|
arkanoid_state->level++;
|
|
|
|
reset_level(canvas, arkanoid_state);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-07 06:59:03 +00:00
|
|
|
reset_level(canvas, arkanoid_state);
|
|
|
|
arkanoid_state->initialDraw = false;
|
|
|
|
arkanoid_state->lives = 3;
|
|
|
|
arkanoid_state->score = 0;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
release_mutex((ValueMutex*)ctx, arkanoid_state);
|
|
|
|
}
|
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
static void arkanoid_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
2022-06-21 02:50:34 +00:00
|
|
|
furi_assert(event_queue);
|
|
|
|
|
|
|
|
GameEvent event = {.type = EventTypeKey, .input = *input_event};
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
static void arkanoid_update_timer_callback(FuriMessageQueue* event_queue) {
|
2022-06-21 02:50:34 +00:00
|
|
|
furi_assert(event_queue);
|
|
|
|
|
|
|
|
GameEvent event = {.type = EventTypeTick};
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_message_queue_put(event_queue, &event, 0);
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t arkanoid_game_app(void* p) {
|
2022-06-26 17:57:29 +00:00
|
|
|
UNUSED(p);
|
2022-08-07 01:37:26 +00:00
|
|
|
int32_t return_code = 0;
|
2022-06-26 17:57:29 +00:00
|
|
|
// Set random seed from interrupts
|
|
|
|
srand(DWT->CYCCNT);
|
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
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");
|
2022-08-07 01:37:26 +00:00
|
|
|
return_code = 255;
|
|
|
|
goto free_and_exit;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriTimer* timer =
|
|
|
|
furi_timer_alloc(arkanoid_update_timer_callback, FuriTimerTypePeriodic, event_queue);
|
|
|
|
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
|
|
|
// Open GUI and register view_port
|
2022-08-07 06:59:03 +00:00
|
|
|
Gui* gui = furi_record_open(RECORD_GUI);
|
2022-06-21 02:50:34 +00:00
|
|
|
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
|
|
|
|
|
|
|
GameEvent event;
|
|
|
|
for(bool processing = true; processing;) {
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
|
2022-06-21 02:50:34 +00:00
|
|
|
ArkanoidState* arkanoid_state = (ArkanoidState*)acquire_mutex_block(&state_mutex);
|
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
if(event_status == FuriStatusOk) {
|
2022-06-21 02:50:34 +00:00
|
|
|
// 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:
|
2022-08-07 06:59:03 +00:00
|
|
|
if(arkanoid_state->xPaddle < FLIPPER_LCD_WIDTH - 12) {
|
|
|
|
arkanoid_state->xPaddle += 8;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case InputKeyLeft:
|
2022-08-07 06:59:03 +00:00
|
|
|
if(arkanoid_state->xPaddle > 0) {
|
|
|
|
arkanoid_state->xPaddle -= 8;
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case InputKeyUp:
|
2022-10-07 20:09:17 +00:00
|
|
|
if(arkanoid_state->speed < MAX_SPEED) {
|
|
|
|
arkanoid_state->speed++;
|
|
|
|
}
|
2022-06-21 02:50:34 +00:00
|
|
|
break;
|
|
|
|
case InputKeyDown:
|
2022-10-07 20:09:17 +00:00
|
|
|
if(arkanoid_state->speed > 1) {
|
|
|
|
arkanoid_state->speed--;
|
|
|
|
}
|
2022-06-21 02:50:34 +00:00
|
|
|
break;
|
|
|
|
case InputKeyOk:
|
2022-09-11 08:15:03 +00:00
|
|
|
if(arkanoid_state->gameStarted == false) {
|
|
|
|
//Release ball if FIRE pressed
|
|
|
|
arkanoid_state->ball_state.released = true;
|
|
|
|
|
|
|
|
//Apply random direction to ball on release
|
|
|
|
if(rand_range(0, 2) == 0) {
|
|
|
|
arkanoid_state->ball_state.dx = 1;
|
|
|
|
} else {
|
|
|
|
arkanoid_state->ball_state.dx = -1;
|
|
|
|
}
|
2022-06-21 02:50:34 +00:00
|
|
|
|
2022-09-11 08:15:03 +00:00
|
|
|
//Makes sure the ball heads upwards
|
|
|
|
arkanoid_state->ball_state.dy = -1;
|
|
|
|
//start the game flag
|
|
|
|
arkanoid_state->gameStarted = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Event timeout
|
2022-08-07 06:59:03 +00:00
|
|
|
FURI_LOG_D(TAG, "furi_message_queue: Event timeout");
|
2022-06-21 02:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
view_port_update(view_port);
|
|
|
|
release_mutex(&state_mutex, arkanoid_state);
|
|
|
|
}
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_timer_free(timer);
|
2022-06-21 02:50:34 +00:00
|
|
|
view_port_enabled_set(view_port, false);
|
|
|
|
gui_remove_view_port(gui, view_port);
|
2022-08-07 06:59:03 +00:00
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
furi_record_close(RECORD_NOTIFICATION);
|
2022-06-21 02:50:34 +00:00
|
|
|
view_port_free(view_port);
|
2022-07-20 16:01:38 +00:00
|
|
|
delete_mutex(&state_mutex);
|
2022-08-07 01:37:26 +00:00
|
|
|
|
|
|
|
free_and_exit:
|
2022-07-20 16:01:38 +00:00
|
|
|
free(arkanoid_state);
|
2022-08-07 01:37:26 +00:00
|
|
|
furi_message_queue_free(event_queue);
|
2022-06-21 02:50:34 +00:00
|
|
|
|
2022-08-07 01:37:26 +00:00
|
|
|
return return_code;
|
|
|
|
}
|