unleashed-firmware/applications/plugins/arkanoid/arkanoid_game.c

475 lines
17 KiB
C
Raw Normal View History

2022-06-21 05:50:34 +03:00
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <stdlib.h>
#include <gui/view.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
2022-06-21 05:50:34 +03:00
#define TAG "Arkanoid"
#define FLIPPER_LCD_WIDTH 128
#define FLIPPER_LCD_HEIGHT 64
2022-10-07 23:09:17 +03:00
#define MAX_SPEED 3
2022-06-21 05:50:34 +03:00
typedef enum { EventTypeTick, EventTypeKey } EventType;
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 05:50:34 +03:00
typedef struct {
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 05:50:34 +03:00
typedef struct {
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
bool gameStarted; // Did the game start?
2022-10-07 23:09:17 +03:00
int speed; // Ball speed
2022-06-21 05:50:34 +03:00
} ArkanoidState;
typedef struct {
EventType type;
InputEvent input;
} GameEvent;
static const NotificationSequence sequence_short_sound = {
&message_note_c5,
&message_delay_50,
&message_sound_off,
NULL,
};
2022-06-21 05:50:34 +03:00
// generate number in range [min,max)
int rand_range(int min, int max) {
return min + rand() % (max - min);
2022-06-21 05:50:34 +03:00
}
void move_ball(Canvas* canvas, ArkanoidState* st) {
st->tick++;
2022-10-07 23:09:17 +03: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 23:09:17 +03:00
return;
}
if(st->ball_state.released) {
2022-06-21 05:50:34 +03:00
//Move ball
if(abs(st->ball_state.dx) == 2) {
st->ball_state.xb += st->ball_state.dx / 2;
2022-06-21 05:50:34 +03:00
// 2x speed is really 1.5 speed
2022-10-07 23:09:17 +03:00
if((st->tick / current_speed) % 2 == 0) st->ball_state.xb += st->ball_state.dx / 2;
2022-06-21 05:50:34 +03:00
} else {
st->ball_state.xb += st->ball_state.dx;
2022-06-21 05:50:34 +03:00
}
st->ball_state.yb = st->ball_state.yb + st->ball_state.dy;
2022-06-21 05:50:34 +03:00
//Set bounds
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 05:50:34 +03:00
//Bounce off top edge
if(st->ball_state.yb <= 0) {
st->ball_state.yb = 2;
st->ball_state.dy = -st->ball_state.dy;
2022-06-21 05:50:34 +03:00
}
//Lose a life if bottom edge hit
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--;
st->gameStarted = false;
2022-06-21 05:50:34 +03:00
if(rand_range(0, 2) == 0) {
st->ball_state.dx = 1;
2022-06-21 05:50:34 +03:00
} else {
st->ball_state.dx = -1;
2022-06-21 05:50:34 +03:00
}
}
//Bounce off left side
if(st->ball_state.xb <= 0) {
st->ball_state.xb = 2;
st->ball_state.dx = -st->ball_state.dx;
2022-06-21 05:50:34 +03:00
}
//Bounce off right side
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 05:50:34 +03:00
// arduboy.tunes.tone(523, 250);
}
//Bounce off paddle
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 05:50:34 +03:00
}
}
//Bounce off Bricks
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 05:50:34 +03:00
//Sets Brick bounds
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 05:50:34 +03:00
//If A collison has occured
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);
//notification_message(st->notify, &sequence_blink_white_100);
st->brickCount++;
st->brick_state.isHit[row][column] = true;
2022-06-21 05:50:34 +03:00
canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
//Vertical collision
if(st->ball_state.bottomBall > st->brick_state.bottomBrick ||
st->ball_state.topBall < st->brick_state.topBrick) {
2022-06-21 05:50:34 +03:00
//Only bounce once each ball move
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 05:50:34 +03:00
}
}
//Hoizontal collision
if(st->ball_state.leftBall < st->brick_state.leftBrick ||
st->ball_state.rightBall > st->brick_state.rightBrick) {
2022-06-21 05:50:34 +03:00
//Only bounce once brick each ball move
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 05:50:34 +03:00
}
}
}
}
}
}
//Reset Bounce
st->bounced = false;
2022-06-21 05:50:34 +03:00
} else {
//Ball follows paddle
st->ball_state.xb = st->xPaddle + 5;
2022-06-21 05:50:34 +03:00
}
}
void draw_lives(Canvas* canvas, ArkanoidState* arkanoid_state) {
if(arkanoid_state->lives == 3) {
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);
} else if(arkanoid_state->lives == 2) {
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);
}
}
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 05:50:34 +03: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 05:50:34 +03:00
move_ball(canvas, ast);
2022-06-21 05:50:34 +03: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 05:50:34 +03:00
}
void reset_level(Canvas* canvas, ArkanoidState* arkanoid_state) {
2022-06-21 05:50:34 +03:00
//Undraw paddle
canvas_draw_frame(canvas, arkanoid_state->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
2022-06-21 05:50:34 +03:00
//Undraw ball
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 05:50:34 +03:00
//Alter various variables to reset the game
arkanoid_state->xPaddle = 54;
arkanoid_state->ball_state.yb = 60;
arkanoid_state->brickCount = 0;
arkanoid_state->ball_state.released = false;
2022-06-21 05:50:34 +03:00
// Reset all brick hit states
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 05:50:34 +03:00
}
}
}
static void arkanoid_state_init(ArkanoidState* arkanoid_state) {
// Init notification
arkanoid_state->notify = furi_record_open(RECORD_NOTIFICATION);
2022-06-21 05:50:34 +03:00
// Set the initial game state
arkanoid_state->COLUMNS = 13;
arkanoid_state->ROWS = 4;
arkanoid_state->ball_state.dx = -1;
arkanoid_state->ball_state.dy = -1;
2022-10-07 23:09:17 +03:00
arkanoid_state->speed = 2;
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 05:50:34 +03:00
// Reset initial state
arkanoid_state->initialDraw = false;
arkanoid_state->gameStarted = false;
2022-06-21 05:50:34 +03:00
}
static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) {
ArkanoidState* arkanoid_state = acquire_mutex((ValueMutex*)ctx, 25);
2022-06-21 05:50:34 +03:00
if(arkanoid_state == NULL) {
return;
}
//Initial level draw
if(!arkanoid_state->initialDraw) {
arkanoid_state->initialDraw = true;
2022-06-21 05:50:34 +03:00
// Set default font for text
canvas_set_font(canvas, FontSecondary);
2022-06-21 05:50:34 +03:00
//Draws the new level
reset_level(canvas, arkanoid_state);
2022-06-21 05:50:34 +03:00
}
//Draws new bricks and resets their values
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 05:50:34 +03:00
canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
}
}
}
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 05:50:34 +03:00
if(arkanoid_state->brickCount == arkanoid_state->ROWS * arkanoid_state->COLUMNS) {
arkanoid_state->level++;
reset_level(canvas, arkanoid_state);
2022-06-21 05:50:34 +03:00
}
} else {
reset_level(canvas, arkanoid_state);
arkanoid_state->initialDraw = false;
arkanoid_state->lives = 3;
arkanoid_state->score = 0;
2022-06-21 05:50:34 +03:00
}
release_mutex((ValueMutex*)ctx, arkanoid_state);
}
2022-07-20 19:01:38 +03:00
static void arkanoid_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
2022-06-21 05:50:34 +03:00
furi_assert(event_queue);
GameEvent event = {.type = EventTypeKey, .input = *input_event};
2022-07-20 19:01:38 +03:00
furi_message_queue_put(event_queue, &event, FuriWaitForever);
2022-06-21 05:50:34 +03:00
}
2022-07-20 19:01:38 +03:00
static void arkanoid_update_timer_callback(FuriMessageQueue* event_queue) {
2022-06-21 05:50:34 +03:00
furi_assert(event_queue);
GameEvent event = {.type = EventTypeTick};
2022-07-20 19:01:38 +03:00
furi_message_queue_put(event_queue, &event, 0);
2022-06-21 05:50:34 +03:00
}
int32_t arkanoid_game_app(void* p) {
UNUSED(p);
int32_t return_code = 0;
2022-07-20 19:01:38 +03:00
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
2022-06-21 05:50:34 +03: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");
return_code = 255;
goto free_and_exit;
2022-06-21 05:50:34 +03: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 19:01:38 +03: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 05:50:34 +03:00
// Open GUI and register view_port
Gui* gui = furi_record_open(RECORD_GUI);
2022-06-21 05:50:34 +03:00
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
GameEvent event;
for(bool processing = true; processing;) {
2022-07-20 19:01:38 +03:00
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
2022-06-21 05:50:34 +03:00
ArkanoidState* arkanoid_state = (ArkanoidState*)acquire_mutex_block(&state_mutex);
2022-07-20 19:01:38 +03:00
if(event_status == FuriStatusOk) {
2022-06-21 05:50:34 +03: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:
if(arkanoid_state->xPaddle < FLIPPER_LCD_WIDTH - 12) {
arkanoid_state->xPaddle += 8;
2022-06-21 05:50:34 +03:00
}
break;
case InputKeyLeft:
if(arkanoid_state->xPaddle > 0) {
arkanoid_state->xPaddle -= 8;
2022-06-21 05:50:34 +03:00
}
break;
case InputKeyUp:
2022-10-07 23:09:17 +03:00
if(arkanoid_state->speed < MAX_SPEED) {
arkanoid_state->speed++;
}
2022-06-21 05:50:34 +03:00
break;
case InputKeyDown:
2022-10-07 23:09:17 +03:00
if(arkanoid_state->speed > 1) {
arkanoid_state->speed--;
}
2022-06-21 05:50:34 +03:00
break;
case InputKeyOk:
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 05:50:34 +03:00
//Makes sure the ball heads upwards
arkanoid_state->ball_state.dy = -1;
//start the game flag
arkanoid_state->gameStarted = true;
}
2022-11-02 23:19:33 +03:00
break;
default:
break;
2022-06-21 05:50:34 +03:00
}
}
}
}
view_port_update(view_port);
release_mutex(&state_mutex, arkanoid_state);
}
2022-07-20 19:01:38 +03:00
furi_timer_free(timer);
2022-06-21 05:50:34 +03:00
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);
2022-06-21 05:50:34 +03:00
view_port_free(view_port);
2022-07-20 19:01:38 +03:00
delete_mutex(&state_mutex);
free_and_exit:
2022-07-20 19:01:38 +03:00
free(arkanoid_state);
furi_message_queue_free(event_queue);
2022-06-21 05:50:34 +03:00
return return_code;
}