From 53183f855e8e0bc7709115adac7c4a1511849837 Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Mon, 9 Jan 2023 11:19:59 -0500 Subject: [PATCH 1/8] ufbt build (no changes) --- .gitignore | 4 + application.fam | 13 +++ flipper_pong.c | 298 ++++++++++++++++++++++++++++++++++++++++++++++++ pong.png | Bin 0 -> 6459 bytes 4 files changed, 315 insertions(+) create mode 100644 .gitignore create mode 100644 application.fam create mode 100644 flipper_pong.c create mode 100644 pong.png diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aceea17 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode/ +*.elf +.editorconfig +.clang-format diff --git a/application.fam b/application.fam new file mode 100644 index 0000000..95484b6 --- /dev/null +++ b/application.fam @@ -0,0 +1,13 @@ +App( + appid="flipper_pong", + name="Pong", + apptype=FlipperAppType.EXTERNAL, + entry_point="flipper_pong_app", + cdefines=["APP_FLIPPER_PONG"], + requires=[ + "gui", + ], + stack_size=1 * 1024, + fap_icon="pong.png", + fap_category="Games", +) diff --git a/flipper_pong.c b/flipper_pong.c new file mode 100644 index 0000000..c25e46b --- /dev/null +++ b/flipper_pong.c @@ -0,0 +1,298 @@ +// CC0 1.0 Universal (CC0 1.0) +// Public Domain Dedication +// https://github.com/nmrr + +#include +#include +#include +#include +#include +#include + +#define SCREEN_SIZE_X 128 +#define SCREEN_SIZE_Y 64 +#define FPS 20 + +#define PAD_SIZE_X 3 +#define PAD_SIZE_Y 8 +#define PLAYER1_PAD_SPEED 2 +#define PLAYER2_PAD_SPEED 2 +#define BALL_SIZE 4 + +typedef enum { + EventTypeInput, + ClockEventTypeTick, +} EventType; + +typedef struct { + EventType type; + InputEvent input; +} EventApp; + +typedef struct Players +{ + uint8_t player1_X,player1_Y,player2_X,player2_Y; + uint16_t player1_score,player2_score; + uint8_t ball_X,ball_Y,ball_X_speed,ball_Y_speed,ball_X_direction,ball_Y_direction; +} Players; + +static void draw_callback(Canvas* canvas, void* ctx) +{ + UNUSED(ctx); + Players* playersMutex = (Players*)acquire_mutex_block((ValueMutex*)ctx); + + canvas_draw_frame(canvas, 0, 0, 128, 64); + canvas_draw_box(canvas, playersMutex->player1_X, playersMutex->player1_Y, PAD_SIZE_X, PAD_SIZE_Y); + canvas_draw_box(canvas, playersMutex->player2_X, playersMutex->player2_Y, PAD_SIZE_X, PAD_SIZE_Y); + canvas_draw_box(canvas, playersMutex->ball_X, playersMutex->ball_Y, BALL_SIZE, BALL_SIZE); + + canvas_set_font(canvas, FontPrimary); + canvas_set_font_direction(canvas, CanvasDirectionBottomToTop); + char buffer[16]; + snprintf(buffer, sizeof(buffer), "%u - %u", playersMutex->player1_score, playersMutex->player2_score); + canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2+15, SCREEN_SIZE_Y/2+2, AlignCenter, AlignTop, buffer); + + release_mutex((ValueMutex*)ctx, playersMutex); +} + +static void input_callback(InputEvent* input_event, void* ctx) +{ + furi_assert(ctx); + FuriMessageQueue* event_queue = ctx; + EventApp event = {.type = EventTypeInput, .input = *input_event}; + furi_message_queue_put(event_queue, &event, FuriWaitForever); +} + +static void clock_tick(void* ctx) { + furi_assert(ctx); + FuriMessageQueue* queue = ctx; + EventApp event = {.type = ClockEventTypeTick}; + furi_message_queue_put(queue, &event, 0); +} + +bool insidePad(uint8_t x, uint8_t y, uint8_t playerX, uint8_t playerY) +{ + if (x >= playerX && x <= playerX+PAD_SIZE_X && y >= playerY && y <= playerY+PAD_SIZE_Y) return true; + return false; +} + +uint8_t changeSpeed() +{ + uint8_t randomuint8[1]; + while(1) + { + furi_hal_random_fill_buf(randomuint8,1); + randomuint8[0] &= 0b00000011; + if (randomuint8[0] >= 1) break; + } + return randomuint8[0]; +} + +uint8_t changeDirection() +{ + uint8_t randomuint8[1]; + furi_hal_random_fill_buf(randomuint8,1); + randomuint8[0] &= 0b1; + return randomuint8[0]; +} + +int32_t flipper_pong_app() +{ + EventApp event; + FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp)); + + Players players; + players.player1_X = SCREEN_SIZE_X-PAD_SIZE_X-1; + players.player1_Y = SCREEN_SIZE_Y/2 - PAD_SIZE_Y/2; + players.player1_score = 0; + + players.player2_X = 1; + players.player2_Y = SCREEN_SIZE_Y/2 - PAD_SIZE_Y/2; + players.player2_score = 0; + + players.ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; + players.ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; + players.ball_X_speed = 1; + players.ball_Y_speed = 1; + players.ball_X_direction = changeDirection(); + players.ball_Y_direction = changeDirection(); + + ValueMutex state_mutex; + init_mutex(&state_mutex, &players, sizeof(Players)); + + ViewPort* view_port = view_port_alloc(); + view_port_draw_callback_set(view_port, draw_callback, &state_mutex); + view_port_input_callback_set(view_port, input_callback, event_queue); + + Gui* gui = furi_record_open(RECORD_GUI); + gui_add_view_port(gui, view_port, GuiLayerFullscreen); + + NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); + if (players.ball_X_direction == 0) notification_message(notification, &sequence_set_only_red_255); + else notification_message(notification, &sequence_set_only_blue_255); + + FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue); + furi_timer_start(timer, 1000/FPS); + + while(1) + { + FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever); + Players* playersMutex = (Players*)acquire_mutex_block(&state_mutex); + + if (event_status == FuriStatusOk) + { + if(event.type == EventTypeInput) + { + if(event.input.key == InputKeyBack) + { + release_mutex(&state_mutex, playersMutex); + notification_message(notification, &sequence_set_only_green_255); + break; + } + else if(event.input.key == InputKeyUp) + { + if (playersMutex->player1_Y >= 1+PLAYER1_PAD_SPEED) playersMutex->player1_Y -= PLAYER1_PAD_SPEED; + else playersMutex->player1_Y = 1; + } + else if(event.input.key == InputKeyDown) + { + if (playersMutex->player1_Y <= SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER1_PAD_SPEED -1) playersMutex->player1_Y += PLAYER1_PAD_SPEED; + else playersMutex->player1_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; + } + } + else if (event.type == ClockEventTypeTick) + { + + if (playersMutex->ball_X + BALL_SIZE/2 <= SCREEN_SIZE_X*0.35 && playersMutex->ball_X_direction == 0) + { + if (playersMutex->ball_Y + BALL_SIZE/2 < playersMutex->player2_Y + PAD_SIZE_Y/2) + { + if (playersMutex->player2_Y >= 1+PLAYER2_PAD_SPEED) playersMutex->player2_Y -= PLAYER2_PAD_SPEED; + else playersMutex->player2_Y= 1; + } + else if (playersMutex->ball_Y + BALL_SIZE/2 > playersMutex->player2_Y + PAD_SIZE_Y/2) + { + if (playersMutex->player2_Y <= SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER2_PAD_SPEED -1) playersMutex->player2_Y += PLAYER2_PAD_SPEED; + else playersMutex->player2_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; + } + } + + uint8_t ball_corner_X[4] = {playersMutex->ball_X, playersMutex->ball_X + BALL_SIZE, playersMutex->ball_X + BALL_SIZE, playersMutex->ball_X}; + uint8_t ball_corner_Y[4] = {playersMutex->ball_Y, playersMutex->ball_Y, playersMutex->ball_Y + BALL_SIZE, playersMutex->ball_Y + BALL_SIZE}; + bool insidePlayer1 = false, insidePlayer2 = false; + + for (int i=0;i<4;i++) + { + if (insidePad(ball_corner_X[i], ball_corner_Y[i], playersMutex->player1_X, playersMutex->player1_Y) == true) + { + insidePlayer1 = true; + break; + } + + if (insidePad(ball_corner_X[i], ball_corner_Y[i], playersMutex->player2_X, playersMutex->player2_Y) == true) + { + insidePlayer2 = true; + break; + } + } + + if (insidePlayer1 == true) + { + playersMutex->ball_X_direction = 0; + playersMutex->ball_X -= playersMutex->ball_X_speed; + playersMutex->ball_X_speed = changeSpeed(); + playersMutex->ball_Y_speed = changeSpeed(); + notification_message(notification, &sequence_set_only_red_255); + } + else if (insidePlayer2 == true) + { + playersMutex->ball_X_direction = 1; + playersMutex->ball_X += playersMutex->ball_X_speed; + playersMutex->ball_X_speed = changeSpeed(); + playersMutex->ball_Y_speed = changeSpeed(); + notification_message(notification, &sequence_set_only_blue_255); + } + else + { + if (playersMutex->ball_X_direction == 1) + { + + if (playersMutex->ball_X <= SCREEN_SIZE_X - BALL_SIZE - 1 - playersMutex->ball_X_speed) + { + playersMutex->ball_X += playersMutex->ball_X_speed; + } + else + { + playersMutex->ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; + playersMutex->ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; + playersMutex->ball_X_speed = 1; + playersMutex->ball_Y_speed = 1; + playersMutex->ball_X_direction = 0; + playersMutex->player2_score++; + notification_message(notification, &sequence_set_only_red_255); + } + } + else + { + if (playersMutex->ball_X >= 1 + playersMutex->ball_X_speed) + { + playersMutex->ball_X -= playersMutex->ball_X_speed; + } + else + { + playersMutex->ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; + playersMutex->ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; + playersMutex->ball_X_speed = 1; + playersMutex->ball_Y_speed = 1; + playersMutex->ball_X_direction = 1; + playersMutex->player1_score++; + notification_message(notification, &sequence_set_only_blue_255); + } + } + } + + if (playersMutex->ball_Y_direction == 1) + { + if (playersMutex->ball_Y <= SCREEN_SIZE_Y - BALL_SIZE - 1 - playersMutex->ball_Y_speed) + { + playersMutex->ball_Y += playersMutex->ball_Y_speed; + } + else + { + playersMutex->ball_Y = SCREEN_SIZE_Y - BALL_SIZE - 1; + playersMutex->ball_X_speed = changeSpeed(); + playersMutex->ball_Y_speed = changeSpeed(); + playersMutex->ball_Y_direction = 0; + } + } + else + { + if (playersMutex->ball_Y >= 1 + playersMutex->ball_Y_speed) + { + playersMutex->ball_Y -= playersMutex->ball_Y_speed; + } + else + { + playersMutex->ball_Y = 1; + playersMutex->ball_X_speed = changeSpeed(); + playersMutex->ball_Y_speed = changeSpeed(); + playersMutex->ball_Y_direction = 1; + } + } + } + } + + release_mutex(&state_mutex, playersMutex); + view_port_update(view_port); + } + + furi_message_queue_free(event_queue); + delete_mutex(&state_mutex); + gui_remove_view_port(gui, view_port); + view_port_free(view_port); + furi_timer_free(timer); + furi_record_close(RECORD_GUI); + furi_record_close(RECORD_NOTIFICATION); + + return 0; +} \ No newline at end of file diff --git a/pong.png b/pong.png new file mode 100644 index 0000000000000000000000000000000000000000..507ce711c0f97ee36d4c2d76800d0ba5463974da GIT binary patch literal 6459 zcmeHKdpuP879Wq0C_*Q3G)3L}ONN@P9soK05f-PIEUyQzfvUl_z z)CMpKzyX7a>y}A2lPx+*?W68A-S2u^>b2hC#Z~6n?&G0-Bc^pbYhL6mKDUar=go%v zfT4L&793%38X@_m^AcRJ-Oop?!(DdXh-=dwI6*mJorX`ZHfg+VbYs^bAMUZwIP*@P z&`nMAymaeGsdoQwLXE?*Sz(Q+&Qvv>X}$ck!?D*%-VBQ)-}f^#BBVJM8VYOP6NBS- z2k$0=CPjf#Ij8gDv(mK_bNF7Z7wVE@?QF?~d$P0!uRWXP&|J7{@W(T+`+90@bwgh3 z4dL?MyryIZI0P9#AwS>m@;G(C_(#QPYD-6^ea?nuaCNn@V_aQCs~c=vvKY+oY);sD>iF)M7Twt;aw|PNX~Vc- zidj5&_vS8a-ib`6DEq3PQ7Jk*-qOsPY;-rKr88~x$_tm4sg=ViZL2S0;{v7Td}~Sc z55_0}I&YlRs#nU?csuO95@#?-zHB(LYd_YcA!n`dQM+VgW2fYNki&xZC2Zr5+~h~+1{O>Hp4vyXvk#* zs(ZXP-hLp{=NHo*{Lp5_Tqn%K+J-6q>&CmdV~ZXn4;Gb-O#A6tz~ZK@IR#pctM<8V z!Ij_gaJ7lJ5i@Cd^y8w3@xEsAiutHJPhxA93Y!j}toGnao%Vb{JuNG^@yhJ5cs`r(J5nqvyk|F5H*1Y2HQOk6VzD znV43mx%tFUNo{pHD|x6qd2r1G(srlDxPnz;S3i?enejt8R)y^T)+~@=YcA14h;|lZ;E}-nn{JGv@ny zzZ!2mQcf66X>TY{wb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rYE^C*;ojsQ7%1b8fBL85Rx$P)#_ zctyAvIhmkPb`AOr$|P z8VnL)`ezVc5&=@gVA$uQQbO?%C?b_kV$vZ#4kQ9h90exRa11_7#F0oaNaaB^IvJ*@ zpm-2#fk+Y#BH0&&gCQ_MEDTX8lv!YL1qwJUz*&Fqf&RT6F(DB~lp*qB8EVT3}3qf+=H@QY=jG7kWaPNEQLBr=IgBr*WnC!-~>WNZW^ zJV3;g$8ePiWg#>mDFKxe0s&CYLa>R*j0F&SDZV5Ui^yb=n6^YR>pkE{YmvlPBno3=lwqTlfvEf` z)gH()!UJXCU%>$Yi%feDr(6cmc1RDT8aV=~OeBd?iN@B3#qgT3)>t!4pjs5rXw~#) zfzTL7Qcw=_RGty6F%=XF3PWII9eh8NKF9@sQKi8UmCgj1WE{k&(Qys3?`05Wm0hzMlgh9l92QQL?*~!f;>7vA1kzv1pZecGJOFu zi%MaU{+}Tdc@RLQk#R6N7)BNdI)DQqDu^Rfi6EcPhXEqU_%bj5O^9ToFF;`dbQVDR zGK+QuGe!1h!pF_|eOVwW`8z-FXVl;60*(IWh2D&g zI%o*bUTC=3eD>_q7dIyzZe66OP@B_wYneInyMVz~gl#$mIZmndblKLO@gw%`B& literal 0 HcmV?d00001 From c938236e537ff531e4fe6c02e6568ab3203aa2f9 Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Mon, 9 Jan 2023 11:20:14 -0500 Subject: [PATCH 2/8] ufbt build (no code changes) --- flipper_pong/application.fam | 13 -- flipper_pong/flipper_pong.c | 298 ----------------------------------- flipper_pong/pong.png | Bin 6459 -> 0 bytes 3 files changed, 311 deletions(-) delete mode 100644 flipper_pong/application.fam delete mode 100644 flipper_pong/flipper_pong.c delete mode 100644 flipper_pong/pong.png diff --git a/flipper_pong/application.fam b/flipper_pong/application.fam deleted file mode 100644 index 95484b6..0000000 --- a/flipper_pong/application.fam +++ /dev/null @@ -1,13 +0,0 @@ -App( - appid="flipper_pong", - name="Pong", - apptype=FlipperAppType.EXTERNAL, - entry_point="flipper_pong_app", - cdefines=["APP_FLIPPER_PONG"], - requires=[ - "gui", - ], - stack_size=1 * 1024, - fap_icon="pong.png", - fap_category="Games", -) diff --git a/flipper_pong/flipper_pong.c b/flipper_pong/flipper_pong.c deleted file mode 100644 index c25e46b..0000000 --- a/flipper_pong/flipper_pong.c +++ /dev/null @@ -1,298 +0,0 @@ -// CC0 1.0 Universal (CC0 1.0) -// Public Domain Dedication -// https://github.com/nmrr - -#include -#include -#include -#include -#include -#include - -#define SCREEN_SIZE_X 128 -#define SCREEN_SIZE_Y 64 -#define FPS 20 - -#define PAD_SIZE_X 3 -#define PAD_SIZE_Y 8 -#define PLAYER1_PAD_SPEED 2 -#define PLAYER2_PAD_SPEED 2 -#define BALL_SIZE 4 - -typedef enum { - EventTypeInput, - ClockEventTypeTick, -} EventType; - -typedef struct { - EventType type; - InputEvent input; -} EventApp; - -typedef struct Players -{ - uint8_t player1_X,player1_Y,player2_X,player2_Y; - uint16_t player1_score,player2_score; - uint8_t ball_X,ball_Y,ball_X_speed,ball_Y_speed,ball_X_direction,ball_Y_direction; -} Players; - -static void draw_callback(Canvas* canvas, void* ctx) -{ - UNUSED(ctx); - Players* playersMutex = (Players*)acquire_mutex_block((ValueMutex*)ctx); - - canvas_draw_frame(canvas, 0, 0, 128, 64); - canvas_draw_box(canvas, playersMutex->player1_X, playersMutex->player1_Y, PAD_SIZE_X, PAD_SIZE_Y); - canvas_draw_box(canvas, playersMutex->player2_X, playersMutex->player2_Y, PAD_SIZE_X, PAD_SIZE_Y); - canvas_draw_box(canvas, playersMutex->ball_X, playersMutex->ball_Y, BALL_SIZE, BALL_SIZE); - - canvas_set_font(canvas, FontPrimary); - canvas_set_font_direction(canvas, CanvasDirectionBottomToTop); - char buffer[16]; - snprintf(buffer, sizeof(buffer), "%u - %u", playersMutex->player1_score, playersMutex->player2_score); - canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2+15, SCREEN_SIZE_Y/2+2, AlignCenter, AlignTop, buffer); - - release_mutex((ValueMutex*)ctx, playersMutex); -} - -static void input_callback(InputEvent* input_event, void* ctx) -{ - furi_assert(ctx); - FuriMessageQueue* event_queue = ctx; - EventApp event = {.type = EventTypeInput, .input = *input_event}; - furi_message_queue_put(event_queue, &event, FuriWaitForever); -} - -static void clock_tick(void* ctx) { - furi_assert(ctx); - FuriMessageQueue* queue = ctx; - EventApp event = {.type = ClockEventTypeTick}; - furi_message_queue_put(queue, &event, 0); -} - -bool insidePad(uint8_t x, uint8_t y, uint8_t playerX, uint8_t playerY) -{ - if (x >= playerX && x <= playerX+PAD_SIZE_X && y >= playerY && y <= playerY+PAD_SIZE_Y) return true; - return false; -} - -uint8_t changeSpeed() -{ - uint8_t randomuint8[1]; - while(1) - { - furi_hal_random_fill_buf(randomuint8,1); - randomuint8[0] &= 0b00000011; - if (randomuint8[0] >= 1) break; - } - return randomuint8[0]; -} - -uint8_t changeDirection() -{ - uint8_t randomuint8[1]; - furi_hal_random_fill_buf(randomuint8,1); - randomuint8[0] &= 0b1; - return randomuint8[0]; -} - -int32_t flipper_pong_app() -{ - EventApp event; - FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp)); - - Players players; - players.player1_X = SCREEN_SIZE_X-PAD_SIZE_X-1; - players.player1_Y = SCREEN_SIZE_Y/2 - PAD_SIZE_Y/2; - players.player1_score = 0; - - players.player2_X = 1; - players.player2_Y = SCREEN_SIZE_Y/2 - PAD_SIZE_Y/2; - players.player2_score = 0; - - players.ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; - players.ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; - players.ball_X_speed = 1; - players.ball_Y_speed = 1; - players.ball_X_direction = changeDirection(); - players.ball_Y_direction = changeDirection(); - - ValueMutex state_mutex; - init_mutex(&state_mutex, &players, sizeof(Players)); - - ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, draw_callback, &state_mutex); - view_port_input_callback_set(view_port, input_callback, event_queue); - - Gui* gui = furi_record_open(RECORD_GUI); - gui_add_view_port(gui, view_port, GuiLayerFullscreen); - - NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); - if (players.ball_X_direction == 0) notification_message(notification, &sequence_set_only_red_255); - else notification_message(notification, &sequence_set_only_blue_255); - - FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue); - furi_timer_start(timer, 1000/FPS); - - while(1) - { - FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever); - Players* playersMutex = (Players*)acquire_mutex_block(&state_mutex); - - if (event_status == FuriStatusOk) - { - if(event.type == EventTypeInput) - { - if(event.input.key == InputKeyBack) - { - release_mutex(&state_mutex, playersMutex); - notification_message(notification, &sequence_set_only_green_255); - break; - } - else if(event.input.key == InputKeyUp) - { - if (playersMutex->player1_Y >= 1+PLAYER1_PAD_SPEED) playersMutex->player1_Y -= PLAYER1_PAD_SPEED; - else playersMutex->player1_Y = 1; - } - else if(event.input.key == InputKeyDown) - { - if (playersMutex->player1_Y <= SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER1_PAD_SPEED -1) playersMutex->player1_Y += PLAYER1_PAD_SPEED; - else playersMutex->player1_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; - } - } - else if (event.type == ClockEventTypeTick) - { - - if (playersMutex->ball_X + BALL_SIZE/2 <= SCREEN_SIZE_X*0.35 && playersMutex->ball_X_direction == 0) - { - if (playersMutex->ball_Y + BALL_SIZE/2 < playersMutex->player2_Y + PAD_SIZE_Y/2) - { - if (playersMutex->player2_Y >= 1+PLAYER2_PAD_SPEED) playersMutex->player2_Y -= PLAYER2_PAD_SPEED; - else playersMutex->player2_Y= 1; - } - else if (playersMutex->ball_Y + BALL_SIZE/2 > playersMutex->player2_Y + PAD_SIZE_Y/2) - { - if (playersMutex->player2_Y <= SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER2_PAD_SPEED -1) playersMutex->player2_Y += PLAYER2_PAD_SPEED; - else playersMutex->player2_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; - } - } - - uint8_t ball_corner_X[4] = {playersMutex->ball_X, playersMutex->ball_X + BALL_SIZE, playersMutex->ball_X + BALL_SIZE, playersMutex->ball_X}; - uint8_t ball_corner_Y[4] = {playersMutex->ball_Y, playersMutex->ball_Y, playersMutex->ball_Y + BALL_SIZE, playersMutex->ball_Y + BALL_SIZE}; - bool insidePlayer1 = false, insidePlayer2 = false; - - for (int i=0;i<4;i++) - { - if (insidePad(ball_corner_X[i], ball_corner_Y[i], playersMutex->player1_X, playersMutex->player1_Y) == true) - { - insidePlayer1 = true; - break; - } - - if (insidePad(ball_corner_X[i], ball_corner_Y[i], playersMutex->player2_X, playersMutex->player2_Y) == true) - { - insidePlayer2 = true; - break; - } - } - - if (insidePlayer1 == true) - { - playersMutex->ball_X_direction = 0; - playersMutex->ball_X -= playersMutex->ball_X_speed; - playersMutex->ball_X_speed = changeSpeed(); - playersMutex->ball_Y_speed = changeSpeed(); - notification_message(notification, &sequence_set_only_red_255); - } - else if (insidePlayer2 == true) - { - playersMutex->ball_X_direction = 1; - playersMutex->ball_X += playersMutex->ball_X_speed; - playersMutex->ball_X_speed = changeSpeed(); - playersMutex->ball_Y_speed = changeSpeed(); - notification_message(notification, &sequence_set_only_blue_255); - } - else - { - if (playersMutex->ball_X_direction == 1) - { - - if (playersMutex->ball_X <= SCREEN_SIZE_X - BALL_SIZE - 1 - playersMutex->ball_X_speed) - { - playersMutex->ball_X += playersMutex->ball_X_speed; - } - else - { - playersMutex->ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; - playersMutex->ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; - playersMutex->ball_X_speed = 1; - playersMutex->ball_Y_speed = 1; - playersMutex->ball_X_direction = 0; - playersMutex->player2_score++; - notification_message(notification, &sequence_set_only_red_255); - } - } - else - { - if (playersMutex->ball_X >= 1 + playersMutex->ball_X_speed) - { - playersMutex->ball_X -= playersMutex->ball_X_speed; - } - else - { - playersMutex->ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; - playersMutex->ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; - playersMutex->ball_X_speed = 1; - playersMutex->ball_Y_speed = 1; - playersMutex->ball_X_direction = 1; - playersMutex->player1_score++; - notification_message(notification, &sequence_set_only_blue_255); - } - } - } - - if (playersMutex->ball_Y_direction == 1) - { - if (playersMutex->ball_Y <= SCREEN_SIZE_Y - BALL_SIZE - 1 - playersMutex->ball_Y_speed) - { - playersMutex->ball_Y += playersMutex->ball_Y_speed; - } - else - { - playersMutex->ball_Y = SCREEN_SIZE_Y - BALL_SIZE - 1; - playersMutex->ball_X_speed = changeSpeed(); - playersMutex->ball_Y_speed = changeSpeed(); - playersMutex->ball_Y_direction = 0; - } - } - else - { - if (playersMutex->ball_Y >= 1 + playersMutex->ball_Y_speed) - { - playersMutex->ball_Y -= playersMutex->ball_Y_speed; - } - else - { - playersMutex->ball_Y = 1; - playersMutex->ball_X_speed = changeSpeed(); - playersMutex->ball_Y_speed = changeSpeed(); - playersMutex->ball_Y_direction = 1; - } - } - } - } - - release_mutex(&state_mutex, playersMutex); - view_port_update(view_port); - } - - furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); - gui_remove_view_port(gui, view_port); - view_port_free(view_port); - furi_timer_free(timer); - furi_record_close(RECORD_GUI); - furi_record_close(RECORD_NOTIFICATION); - - return 0; -} \ No newline at end of file diff --git a/flipper_pong/pong.png b/flipper_pong/pong.png deleted file mode 100644 index 507ce711c0f97ee36d4c2d76800d0ba5463974da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6459 zcmeHKdpuP879Wq0C_*Q3G)3L}ONN@P9soK05f-PIEUyQzfvUl_z z)CMpKzyX7a>y}A2lPx+*?W68A-S2u^>b2hC#Z~6n?&G0-Bc^pbYhL6mKDUar=go%v zfT4L&793%38X@_m^AcRJ-Oop?!(DdXh-=dwI6*mJorX`ZHfg+VbYs^bAMU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F9@sQKi8UmCgj1WE{k&(Qys3?`05Wm0hzMlgh9l92QQL?*~!f;>7vA1kzv1pZecGJOFu zi%MaU{+}Tdc@RLQk#R6N7)BNdI)DQqDu^Rfi6EcPhXEqU_%bj5O^9ToFF;`dbQVDR zGK+QuGe!1h!pF_|eOVwW`8z-FXVl;60*(IWh2D&g zI%o*bUTC=3eD>_q7dIyzZe66OP@B_wYneInyMVz~gl#$mIZmndblKLO@gw%`B& From 777bf97a7f1463e09717e27a402e19e8ddc6fd0d Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Mon, 9 Jan 2023 11:21:47 -0500 Subject: [PATCH 3/8] binary upload --- dist/flipper_pong.fap | Bin 0 -> 4116 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 dist/flipper_pong.fap diff --git a/dist/flipper_pong.fap b/dist/flipper_pong.fap new file mode 100644 index 0000000000000000000000000000000000000000..09d48894a3fc397df370d4bcab42852670354ef7 GIT binary patch literal 4116 zcma)9e{2)?6@Pbjf`J4Q!YFMS^^XLyh6Iz45gOV?NjQg&iD>O4Ds8%6&gaC!vCY{A z*HoGrR6`vRCP5ld(6Xhi0&1tQtgNY1PLy5XSPTL>b=iT`n zbFgk->3g5==Y8LM-|u_hyZ5!fw|%E12m;wnphjZ$W>NI^GIOS4vk8r~m^?)9zB={N z1@a4{;;3s>@QoPm8sVJ#8z(~*Q{Oe*|GHsRO$%VQPZMEt@WN7w{LuEguDH*OcU`n= z?%%D#xz*B_+cw{w>-oV{g|tk9{-&Vhhi|vk6`-+vfc&*mqx8hCUxfy;zN}|G{m2z) zlx`S@^V6YQ*`H)BJ{=m!`EqW|c0zkPM7~C;4qjZq6~Kujw~kcLdptC{{J9!m7wr2A zS3-X@+<6hJbV`2zE@&?P=48kx1tkwG7EQhPx4A>~{(Uimtab{(3*C!9cC{9|Ju|1e zJs*48PIWK(jJ(NEc^8{r5alN8mMM+OE^~!aS zvt7rhs(c>rThOYMUI~mC^}#hbtA-l#zhg9d1xV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liYrpsyY7yfmqLqwK;x{|v<3wv1(Y=i8Pr-m1$ND$|6YYuj zp!aQ?l+EFY?$cX|1s8tDSnIMG`z$Nzv;lfkxSRQR6w+<(kMALl0pe1df#A<&hU|x1 SEqb|L82|0Jn={tK*82}X-(Qyi literal 0 HcmV?d00001 From 24027d2928dbf06e51bef319542496b8d262c2d6 Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Mon, 9 Jan 2023 11:48:15 -0500 Subject: [PATCH 4/8] Change P1 paddle speed for quick change --- flipper_pong.c | 237 ++++++++++++++++++++++++------------------------- 1 file changed, 115 insertions(+), 122 deletions(-) diff --git a/flipper_pong.c b/flipper_pong.c index c25e46b..f1afe77 100644 --- a/flipper_pong.c +++ b/flipper_pong.c @@ -15,7 +15,7 @@ #define PAD_SIZE_X 3 #define PAD_SIZE_Y 8 -#define PLAYER1_PAD_SPEED 2 +#define PLAYER1_PAD_SPEED 8 #define PLAYER2_PAD_SPEED 2 #define BALL_SIZE 4 @@ -29,34 +29,39 @@ typedef struct { InputEvent input; } EventApp; -typedef struct Players -{ - uint8_t player1_X,player1_Y,player2_X,player2_Y; - uint16_t player1_score,player2_score; - uint8_t ball_X,ball_Y,ball_X_speed,ball_Y_speed,ball_X_direction,ball_Y_direction; +typedef struct Players { + uint8_t player1_X, player1_Y, player2_X, player2_Y; + uint16_t player1_score, player2_score; + uint8_t ball_X, ball_Y, ball_X_speed, ball_Y_speed, ball_X_direction, ball_Y_direction; } Players; -static void draw_callback(Canvas* canvas, void* ctx) -{ +static void draw_callback(Canvas* canvas, void* ctx) { UNUSED(ctx); Players* playersMutex = (Players*)acquire_mutex_block((ValueMutex*)ctx); canvas_draw_frame(canvas, 0, 0, 128, 64); - canvas_draw_box(canvas, playersMutex->player1_X, playersMutex->player1_Y, PAD_SIZE_X, PAD_SIZE_Y); - canvas_draw_box(canvas, playersMutex->player2_X, playersMutex->player2_Y, PAD_SIZE_X, PAD_SIZE_Y); + canvas_draw_box( + canvas, playersMutex->player1_X, playersMutex->player1_Y, PAD_SIZE_X, PAD_SIZE_Y); + canvas_draw_box( + canvas, playersMutex->player2_X, playersMutex->player2_Y, PAD_SIZE_X, PAD_SIZE_Y); canvas_draw_box(canvas, playersMutex->ball_X, playersMutex->ball_Y, BALL_SIZE, BALL_SIZE); canvas_set_font(canvas, FontPrimary); canvas_set_font_direction(canvas, CanvasDirectionBottomToTop); char buffer[16]; - snprintf(buffer, sizeof(buffer), "%u - %u", playersMutex->player1_score, playersMutex->player2_score); - canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2+15, SCREEN_SIZE_Y/2+2, AlignCenter, AlignTop, buffer); + snprintf( + buffer, + sizeof(buffer), + "%u - %u", + playersMutex->player1_score, + playersMutex->player2_score); + canvas_draw_str_aligned( + canvas, SCREEN_SIZE_X / 2 + 15, SCREEN_SIZE_Y / 2 + 2, AlignCenter, AlignTop, buffer); release_mutex((ValueMutex*)ctx, playersMutex); } -static void input_callback(InputEvent* input_event, void* ctx) -{ +static void input_callback(InputEvent* input_event, void* ctx) { furi_assert(ctx); FuriMessageQueue* event_queue = ctx; EventApp event = {.type = EventTypeInput, .input = *input_event}; @@ -70,48 +75,44 @@ static void clock_tick(void* ctx) { furi_message_queue_put(queue, &event, 0); } -bool insidePad(uint8_t x, uint8_t y, uint8_t playerX, uint8_t playerY) -{ - if (x >= playerX && x <= playerX+PAD_SIZE_X && y >= playerY && y <= playerY+PAD_SIZE_Y) return true; +bool insidePad(uint8_t x, uint8_t y, uint8_t playerX, uint8_t playerY) { + if(x >= playerX && x <= playerX + PAD_SIZE_X && y >= playerY && y <= playerY + PAD_SIZE_Y) + return true; return false; } -uint8_t changeSpeed() -{ +uint8_t changeSpeed() { uint8_t randomuint8[1]; - while(1) - { - furi_hal_random_fill_buf(randomuint8,1); - randomuint8[0] &= 0b00000011; - if (randomuint8[0] >= 1) break; + while(1) { + furi_hal_random_fill_buf(randomuint8, 1); + randomuint8[0] &= 0b00000011; + if(randomuint8[0] >= 1) break; } return randomuint8[0]; } -uint8_t changeDirection() -{ +uint8_t changeDirection() { uint8_t randomuint8[1]; - furi_hal_random_fill_buf(randomuint8,1); + furi_hal_random_fill_buf(randomuint8, 1); randomuint8[0] &= 0b1; - return randomuint8[0]; + return randomuint8[0]; } -int32_t flipper_pong_app() -{ +int32_t flipper_pong_app() { EventApp event; FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp)); Players players; - players.player1_X = SCREEN_SIZE_X-PAD_SIZE_X-1; - players.player1_Y = SCREEN_SIZE_Y/2 - PAD_SIZE_Y/2; + players.player1_X = SCREEN_SIZE_X - PAD_SIZE_X - 1; + players.player1_Y = SCREEN_SIZE_Y / 2 - PAD_SIZE_Y / 2; players.player1_score = 0; players.player2_X = 1; - players.player2_Y = SCREEN_SIZE_Y/2 - PAD_SIZE_Y/2; + players.player2_Y = SCREEN_SIZE_Y / 2 - PAD_SIZE_Y / 2; players.player2_score = 0; - players.ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; - players.ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; + players.ball_X = SCREEN_SIZE_X / 2 - BALL_SIZE / 2; + players.ball_Y = SCREEN_SIZE_Y / 2 - BALL_SIZE / 2; players.ball_X_speed = 1; players.ball_Y_speed = 1; players.ball_X_direction = changeDirection(); @@ -128,120 +129,120 @@ int32_t flipper_pong_app() gui_add_view_port(gui, view_port, GuiLayerFullscreen); NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); - if (players.ball_X_direction == 0) notification_message(notification, &sequence_set_only_red_255); - else notification_message(notification, &sequence_set_only_blue_255); + if(players.ball_X_direction == 0) + notification_message(notification, &sequence_set_only_red_255); + else + notification_message(notification, &sequence_set_only_blue_255); FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue); - furi_timer_start(timer, 1000/FPS); + furi_timer_start(timer, 1000 / FPS); - while(1) - { + while(1) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever); Players* playersMutex = (Players*)acquire_mutex_block(&state_mutex); - if (event_status == FuriStatusOk) - { - if(event.type == EventTypeInput) - { - if(event.input.key == InputKeyBack) - { + if(event_status == FuriStatusOk) { + if(event.type == EventTypeInput) { + if(event.input.key == InputKeyBack) { release_mutex(&state_mutex, playersMutex); notification_message(notification, &sequence_set_only_green_255); break; + } else if(event.input.key == InputKeyUp) { + if(playersMutex->player1_Y >= 1 + PLAYER1_PAD_SPEED) + playersMutex->player1_Y -= PLAYER1_PAD_SPEED; + else + playersMutex->player1_Y = 1; + } else if(event.input.key == InputKeyDown) { + if(playersMutex->player1_Y <= + SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER1_PAD_SPEED - 1) + playersMutex->player1_Y += PLAYER1_PAD_SPEED; + else + playersMutex->player1_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; } - else if(event.input.key == InputKeyUp) - { - if (playersMutex->player1_Y >= 1+PLAYER1_PAD_SPEED) playersMutex->player1_Y -= PLAYER1_PAD_SPEED; - else playersMutex->player1_Y = 1; - } - else if(event.input.key == InputKeyDown) - { - if (playersMutex->player1_Y <= SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER1_PAD_SPEED -1) playersMutex->player1_Y += PLAYER1_PAD_SPEED; - else playersMutex->player1_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; - } - } - else if (event.type == ClockEventTypeTick) - { - - if (playersMutex->ball_X + BALL_SIZE/2 <= SCREEN_SIZE_X*0.35 && playersMutex->ball_X_direction == 0) - { - if (playersMutex->ball_Y + BALL_SIZE/2 < playersMutex->player2_Y + PAD_SIZE_Y/2) - { - if (playersMutex->player2_Y >= 1+PLAYER2_PAD_SPEED) playersMutex->player2_Y -= PLAYER2_PAD_SPEED; - else playersMutex->player2_Y= 1; - } - else if (playersMutex->ball_Y + BALL_SIZE/2 > playersMutex->player2_Y + PAD_SIZE_Y/2) - { - if (playersMutex->player2_Y <= SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER2_PAD_SPEED -1) playersMutex->player2_Y += PLAYER2_PAD_SPEED; - else playersMutex->player2_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; + } else if(event.type == ClockEventTypeTick) { + if(playersMutex->ball_X + BALL_SIZE / 2 <= SCREEN_SIZE_X * 0.35 && + playersMutex->ball_X_direction == 0) { + if(playersMutex->ball_Y + BALL_SIZE / 2 < + playersMutex->player2_Y + PAD_SIZE_Y / 2) { + if(playersMutex->player2_Y >= 1 + PLAYER2_PAD_SPEED) + playersMutex->player2_Y -= PLAYER2_PAD_SPEED; + else + playersMutex->player2_Y = 1; + } else if( + playersMutex->ball_Y + BALL_SIZE / 2 > + playersMutex->player2_Y + PAD_SIZE_Y / 2) { + if(playersMutex->player2_Y <= + SCREEN_SIZE_Y - PAD_SIZE_Y - PLAYER2_PAD_SPEED - 1) + playersMutex->player2_Y += PLAYER2_PAD_SPEED; + else + playersMutex->player2_Y = SCREEN_SIZE_Y - PAD_SIZE_Y - 1; } } - uint8_t ball_corner_X[4] = {playersMutex->ball_X, playersMutex->ball_X + BALL_SIZE, playersMutex->ball_X + BALL_SIZE, playersMutex->ball_X}; - uint8_t ball_corner_Y[4] = {playersMutex->ball_Y, playersMutex->ball_Y, playersMutex->ball_Y + BALL_SIZE, playersMutex->ball_Y + BALL_SIZE}; + uint8_t ball_corner_X[4] = { + playersMutex->ball_X, + playersMutex->ball_X + BALL_SIZE, + playersMutex->ball_X + BALL_SIZE, + playersMutex->ball_X}; + uint8_t ball_corner_Y[4] = { + playersMutex->ball_Y, + playersMutex->ball_Y, + playersMutex->ball_Y + BALL_SIZE, + playersMutex->ball_Y + BALL_SIZE}; bool insidePlayer1 = false, insidePlayer2 = false; - for (int i=0;i<4;i++) - { - if (insidePad(ball_corner_X[i], ball_corner_Y[i], playersMutex->player1_X, playersMutex->player1_Y) == true) - { + for(int i = 0; i < 4; i++) { + if(insidePad( + ball_corner_X[i], + ball_corner_Y[i], + playersMutex->player1_X, + playersMutex->player1_Y) == true) { insidePlayer1 = true; break; } - if (insidePad(ball_corner_X[i], ball_corner_Y[i], playersMutex->player2_X, playersMutex->player2_Y) == true) - { + if(insidePad( + ball_corner_X[i], + ball_corner_Y[i], + playersMutex->player2_X, + playersMutex->player2_Y) == true) { insidePlayer2 = true; break; } } - if (insidePlayer1 == true) - { + if(insidePlayer1 == true) { playersMutex->ball_X_direction = 0; playersMutex->ball_X -= playersMutex->ball_X_speed; playersMutex->ball_X_speed = changeSpeed(); playersMutex->ball_Y_speed = changeSpeed(); notification_message(notification, &sequence_set_only_red_255); - } - else if (insidePlayer2 == true) - { + } else if(insidePlayer2 == true) { playersMutex->ball_X_direction = 1; playersMutex->ball_X += playersMutex->ball_X_speed; playersMutex->ball_X_speed = changeSpeed(); playersMutex->ball_Y_speed = changeSpeed(); notification_message(notification, &sequence_set_only_blue_255); - } - else - { - if (playersMutex->ball_X_direction == 1) - { - - if (playersMutex->ball_X <= SCREEN_SIZE_X - BALL_SIZE - 1 - playersMutex->ball_X_speed) - { + } else { + if(playersMutex->ball_X_direction == 1) { + if(playersMutex->ball_X <= + SCREEN_SIZE_X - BALL_SIZE - 1 - playersMutex->ball_X_speed) { playersMutex->ball_X += playersMutex->ball_X_speed; - } - else - { - playersMutex->ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; - playersMutex->ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; + } else { + playersMutex->ball_X = SCREEN_SIZE_X / 2 - BALL_SIZE / 2; + playersMutex->ball_Y = SCREEN_SIZE_Y / 2 - BALL_SIZE / 2; playersMutex->ball_X_speed = 1; playersMutex->ball_Y_speed = 1; playersMutex->ball_X_direction = 0; playersMutex->player2_score++; notification_message(notification, &sequence_set_only_red_255); } - } - else - { - if (playersMutex->ball_X >= 1 + playersMutex->ball_X_speed) - { + } else { + if(playersMutex->ball_X >= 1 + playersMutex->ball_X_speed) { playersMutex->ball_X -= playersMutex->ball_X_speed; - } - else - { - playersMutex->ball_X = SCREEN_SIZE_X/2 - BALL_SIZE/2; - playersMutex->ball_Y = SCREEN_SIZE_Y/2 - BALL_SIZE/2; + } else { + playersMutex->ball_X = SCREEN_SIZE_X / 2 - BALL_SIZE / 2; + playersMutex->ball_Y = SCREEN_SIZE_Y / 2 - BALL_SIZE / 2; playersMutex->ball_X_speed = 1; playersMutex->ball_Y_speed = 1; playersMutex->ball_X_direction = 1; @@ -251,28 +252,20 @@ int32_t flipper_pong_app() } } - if (playersMutex->ball_Y_direction == 1) - { - if (playersMutex->ball_Y <= SCREEN_SIZE_Y - BALL_SIZE - 1 - playersMutex->ball_Y_speed) - { + if(playersMutex->ball_Y_direction == 1) { + if(playersMutex->ball_Y <= + SCREEN_SIZE_Y - BALL_SIZE - 1 - playersMutex->ball_Y_speed) { playersMutex->ball_Y += playersMutex->ball_Y_speed; - } - else - { + } else { playersMutex->ball_Y = SCREEN_SIZE_Y - BALL_SIZE - 1; playersMutex->ball_X_speed = changeSpeed(); playersMutex->ball_Y_speed = changeSpeed(); playersMutex->ball_Y_direction = 0; } - } - else - { - if (playersMutex->ball_Y >= 1 + playersMutex->ball_Y_speed) - { + } else { + if(playersMutex->ball_Y >= 1 + playersMutex->ball_Y_speed) { playersMutex->ball_Y -= playersMutex->ball_Y_speed; - } - else - { + } else { playersMutex->ball_Y = 1; playersMutex->ball_X_speed = changeSpeed(); playersMutex->ball_Y_speed = changeSpeed(); From f46e72e989b55118fb700a2cbbe1ee1973931029 Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Mon, 9 Jan 2023 11:52:56 -0500 Subject: [PATCH 5/8] Fast Paddle Pong dev build --- dist/flipper_pong.fap | Bin 4116 -> 4116 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/dist/flipper_pong.fap b/dist/flipper_pong.fap index 09d48894a3fc397df370d4bcab42852670354ef7..465024e249efb2c5aca289fe992f66aabf8f7b94 100644 GIT binary patch delta 42 zcmV+_0M-ALAe11mm;wbaD+1UElbr%n1qdt4*$A`s0y6^yJ|ij(ld%LGvq%Qa2_Rk# A6951J delta 42 zcmV+_0M-ALAe11mm;wbgD+1U8lbr%n1p+I}*#fim0y6^y5l8dBld%LGvq%Qa2`*v| AcmMzZ From 3a17e72b2cd9037fa36811792595e8b55fa01b7b Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Fri, 27 Jan 2023 01:26:20 -0500 Subject: [PATCH 6/8] Reset LED on exit --- flipper_pong.c | 1 + 1 file changed, 1 insertion(+) diff --git a/flipper_pong.c b/flipper_pong.c index f1afe77..191aa97 100644 --- a/flipper_pong.c +++ b/flipper_pong.c @@ -279,6 +279,7 @@ int32_t flipper_pong_app() { view_port_update(view_port); } + notification_message(notification, &sequence_reset_rgb); furi_message_queue_free(event_queue); delete_mutex(&state_mutex); gui_remove_view_port(gui, view_port); From b54a073ddc6503ee13407e5839dca555e4d109f5 Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Fri, 27 Jan 2023 01:35:29 -0500 Subject: [PATCH 7/8] speed adjustment --- flipper_pong.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flipper_pong.c b/flipper_pong.c index 191aa97..814627b 100644 --- a/flipper_pong.c +++ b/flipper_pong.c @@ -15,7 +15,8 @@ #define PAD_SIZE_X 3 #define PAD_SIZE_Y 8 -#define PLAYER1_PAD_SPEED 8 +#define PLAYER1_PAD_SPEED 4 + #define PLAYER2_PAD_SPEED 2 #define BALL_SIZE 4 From 5000a3b1629b7ddb954fddfee7cc6b59d3c27be0 Mon Sep 17 00:00:00 2001 From: SimplyMinimal Date: Fri, 27 Jan 2023 01:44:44 -0500 Subject: [PATCH 8/8] reference repo images instead --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 9b2677c..ac99d8e 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # flipperzero-pong A Pong game for the Flipper Zero -![](https://github.com/nmrr/flipperzero-pong/blob/main/img/Flipper_Zero.jpg) - Assuming the toolchain is already installed, copy **flipper_pong** directory to **applications_user** Plug your **Flipper Zero** and build the Pong : @@ -19,5 +17,4 @@ Press **Up** or **Down** to move your paddle. Press back button to quit If you don't want to build the game, just simply copy **flipper_pong.fap** on your **Flipper Zero** ## Gallery ## - - +![](img/flipper1.png) | ![](img/flipper2.png) | ![](img/flipper3.png) \ No newline at end of file