arkanoid_game.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <gui/view.h>
  6. #include <notification/notification.h>
  7. #include <notification/notification_messages.h>
  8. #include <dolphin/dolphin.h>
  9. #define TAG "Arkanoid"
  10. #define FLIPPER_LCD_WIDTH 128
  11. #define FLIPPER_LCD_HEIGHT 64
  12. #define MAX_SPEED 3
  13. typedef enum { EventTypeTick, EventTypeKey } EventType;
  14. typedef struct {
  15. //Brick Bounds used in collision detection
  16. int leftBrick;
  17. int rightBrick;
  18. int topBrick;
  19. int bottomBrick;
  20. bool isHit[4][13]; //Array of if bricks are hit or not
  21. } BrickState;
  22. typedef struct {
  23. int dx; //Initial movement of ball
  24. int dy; //Initial movement of ball
  25. int xb; //Balls starting possition
  26. int yb; //Balls starting possition
  27. bool released; //If the ball has been released by the player
  28. //Ball Bounds used in collision detection
  29. int leftBall;
  30. int rightBall;
  31. int topBall;
  32. int bottomBall;
  33. } BallState;
  34. typedef struct {
  35. FuriMutex* mutex;
  36. BallState ball_state;
  37. BrickState brick_state;
  38. NotificationApp* notify;
  39. unsigned int COLUMNS; //Columns of bricks
  40. unsigned int ROWS; //Rows of bricks
  41. bool initialDraw; //If the inital draw has happened
  42. int xPaddle; //X position of paddle
  43. char text[16]; //General string buffer
  44. bool bounced; //Used to fix double bounce glitch
  45. int lives; //Amount of lives
  46. int level; //Current level
  47. unsigned int score; //Score for the game
  48. unsigned int brickCount; //Amount of bricks hit
  49. int tick; //Tick counter
  50. bool gameStarted; // Did the game start?
  51. int speed; // Ball speed
  52. } ArkanoidState;
  53. typedef struct {
  54. EventType type;
  55. InputEvent input;
  56. } GameEvent;
  57. static const NotificationSequence sequence_short_sound = {
  58. &message_note_c5,
  59. &message_delay_50,
  60. &message_sound_off,
  61. NULL,
  62. };
  63. // generate number in range [min,max)
  64. int rand_range(int min, int max) {
  65. return min + rand() % (max - min);
  66. }
  67. void move_ball(Canvas* canvas, ArkanoidState* st) {
  68. st->tick++;
  69. int current_speed = abs(st->speed - 1 - MAX_SPEED);
  70. if(st->tick % current_speed != 0 && st->tick % (current_speed + 1) != 0) {
  71. return;
  72. }
  73. if(st->ball_state.released) {
  74. //Move ball
  75. if(abs(st->ball_state.dx) == 2) {
  76. st->ball_state.xb += st->ball_state.dx / 2;
  77. // 2x speed is really 1.5 speed
  78. if((st->tick / current_speed) % 2 == 0) st->ball_state.xb += st->ball_state.dx / 2;
  79. } else {
  80. st->ball_state.xb += st->ball_state.dx;
  81. }
  82. st->ball_state.yb = st->ball_state.yb + st->ball_state.dy;
  83. //Set bounds
  84. st->ball_state.leftBall = st->ball_state.xb;
  85. st->ball_state.rightBall = st->ball_state.xb + 2;
  86. st->ball_state.topBall = st->ball_state.yb;
  87. st->ball_state.bottomBall = st->ball_state.yb + 2;
  88. //Bounce off top edge
  89. if(st->ball_state.yb <= 0) {
  90. st->ball_state.yb = 2;
  91. st->ball_state.dy = -st->ball_state.dy;
  92. }
  93. //Lose a life if bottom edge hit
  94. if(st->ball_state.yb >= FLIPPER_LCD_HEIGHT) {
  95. canvas_draw_frame(canvas, st->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
  96. st->xPaddle = 54;
  97. st->ball_state.yb = 60;
  98. st->ball_state.released = false;
  99. st->lives--;
  100. st->gameStarted = false;
  101. if(rand_range(0, 2) == 0) {
  102. st->ball_state.dx = 1;
  103. } else {
  104. st->ball_state.dx = -1;
  105. }
  106. }
  107. //Bounce off left side
  108. if(st->ball_state.xb <= 0) {
  109. st->ball_state.xb = 2;
  110. st->ball_state.dx = -st->ball_state.dx;
  111. }
  112. //Bounce off right side
  113. if(st->ball_state.xb >= FLIPPER_LCD_WIDTH - 2) {
  114. st->ball_state.xb = FLIPPER_LCD_WIDTH - 4;
  115. st->ball_state.dx = -st->ball_state.dx;
  116. // arduboy.tunes.tone(523, 250);
  117. }
  118. //Bounce off paddle
  119. if(st->ball_state.xb + 1 >= st->xPaddle && st->ball_state.xb <= st->xPaddle + 12 &&
  120. st->ball_state.yb + 2 >= FLIPPER_LCD_HEIGHT - 1 &&
  121. st->ball_state.yb <= FLIPPER_LCD_HEIGHT) {
  122. st->ball_state.dy = -st->ball_state.dy;
  123. st->ball_state.dx =
  124. ((st->ball_state.xb - (st->xPaddle + 6)) / 3); //Applies spin on the ball
  125. // prevent straight bounce, but not prevent roguuemaster from stealing
  126. if(st->ball_state.dx == 0) {
  127. st->ball_state.dx = (rand_range(0, 2) == 1) ? 1 : -1;
  128. }
  129. }
  130. //Bounce off Bricks
  131. for(unsigned int row = 0; row < st->ROWS; row++) {
  132. for(unsigned int column = 0; column < st->COLUMNS; column++) {
  133. if(!st->brick_state.isHit[row][column]) {
  134. //Sets Brick bounds
  135. st->brick_state.leftBrick = 10 * column;
  136. st->brick_state.rightBrick = 10 * column + 10;
  137. st->brick_state.topBrick = 6 * row + 1;
  138. st->brick_state.bottomBrick = 6 * row + 7;
  139. //If A collison has occured
  140. if(st->ball_state.topBall <= st->brick_state.bottomBrick &&
  141. st->ball_state.bottomBall >= st->brick_state.topBrick &&
  142. st->ball_state.leftBall <= st->brick_state.rightBrick &&
  143. st->ball_state.rightBall >= st->brick_state.leftBrick) {
  144. st->score += st->level;
  145. // Blink led when we hit some brick
  146. notification_message(st->notify, &sequence_short_sound);
  147. //notification_message(st->notify, &sequence_blink_white_100);
  148. st->brickCount++;
  149. st->brick_state.isHit[row][column] = true;
  150. canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
  151. //Vertical collision
  152. if(st->ball_state.bottomBall > st->brick_state.bottomBrick ||
  153. st->ball_state.topBall < st->brick_state.topBrick) {
  154. //Only bounce once each ball move
  155. if(!st->bounced) {
  156. st->ball_state.dy = -st->ball_state.dy;
  157. st->ball_state.yb += st->ball_state.dy;
  158. st->bounced = true;
  159. }
  160. }
  161. //Hoizontal collision
  162. if(st->ball_state.leftBall < st->brick_state.leftBrick ||
  163. st->ball_state.rightBall > st->brick_state.rightBrick) {
  164. //Only bounce once brick each ball move
  165. if(!st->bounced) {
  166. st->ball_state.dx = -st->ball_state.dx;
  167. st->ball_state.xb += st->ball_state.dx;
  168. st->bounced = true;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. //Reset Bounce
  176. st->bounced = false;
  177. } else {
  178. //Ball follows paddle
  179. st->ball_state.xb = st->xPaddle + 5;
  180. }
  181. }
  182. void draw_lives(Canvas* canvas, ArkanoidState* arkanoid_state) {
  183. if(arkanoid_state->lives == 3) {
  184. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
  185. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
  186. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
  187. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
  188. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 11);
  189. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 11);
  190. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 12);
  191. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 12);
  192. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 15);
  193. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 15);
  194. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 16);
  195. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 16);
  196. } else if(arkanoid_state->lives == 2) {
  197. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
  198. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
  199. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
  200. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
  201. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 11);
  202. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 11);
  203. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 12);
  204. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 12);
  205. } else {
  206. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
  207. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
  208. canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
  209. canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
  210. }
  211. }
  212. void draw_score(Canvas* canvas, ArkanoidState* arkanoid_state) {
  213. snprintf(arkanoid_state->text, sizeof(arkanoid_state->text), "%u", arkanoid_state->score);
  214. canvas_draw_str_aligned(
  215. canvas,
  216. FLIPPER_LCD_WIDTH - 2,
  217. FLIPPER_LCD_HEIGHT - 6,
  218. AlignRight,
  219. AlignBottom,
  220. arkanoid_state->text);
  221. }
  222. void draw_ball(Canvas* canvas, ArkanoidState* ast) {
  223. canvas_draw_dot(canvas, ast->ball_state.xb, ast->ball_state.yb);
  224. canvas_draw_dot(canvas, ast->ball_state.xb + 1, ast->ball_state.yb);
  225. canvas_draw_dot(canvas, ast->ball_state.xb, ast->ball_state.yb + 1);
  226. canvas_draw_dot(canvas, ast->ball_state.xb + 1, ast->ball_state.yb + 1);
  227. move_ball(canvas, ast);
  228. }
  229. void draw_paddle(Canvas* canvas, ArkanoidState* arkanoid_state) {
  230. canvas_draw_frame(canvas, arkanoid_state->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
  231. }
  232. void reset_level(Canvas* canvas, ArkanoidState* arkanoid_state) {
  233. //Undraw paddle
  234. canvas_draw_frame(canvas, arkanoid_state->xPaddle, FLIPPER_LCD_HEIGHT - 1, 11, 1);
  235. //Undraw ball
  236. canvas_draw_dot(canvas, arkanoid_state->ball_state.xb, arkanoid_state->ball_state.yb);
  237. canvas_draw_dot(canvas, arkanoid_state->ball_state.xb + 1, arkanoid_state->ball_state.yb);
  238. canvas_draw_dot(canvas, arkanoid_state->ball_state.xb, arkanoid_state->ball_state.yb + 1);
  239. canvas_draw_dot(canvas, arkanoid_state->ball_state.xb + 1, arkanoid_state->ball_state.yb + 1);
  240. //Alter various variables to reset the game
  241. arkanoid_state->xPaddle = 54;
  242. arkanoid_state->ball_state.yb = 60;
  243. arkanoid_state->brickCount = 0;
  244. arkanoid_state->ball_state.released = false;
  245. arkanoid_state->gameStarted = false;
  246. // Reset all brick hit states
  247. for(unsigned int row = 0; row < arkanoid_state->ROWS; row++) {
  248. for(unsigned int column = 0; column < arkanoid_state->COLUMNS; column++) {
  249. arkanoid_state->brick_state.isHit[row][column] = false;
  250. }
  251. }
  252. }
  253. static void arkanoid_state_init(ArkanoidState* arkanoid_state) {
  254. // Init notification
  255. arkanoid_state->notify = furi_record_open(RECORD_NOTIFICATION);
  256. // Set the initial game state
  257. arkanoid_state->COLUMNS = 13;
  258. arkanoid_state->ROWS = 4;
  259. arkanoid_state->ball_state.dx = -1;
  260. arkanoid_state->ball_state.dy = -1;
  261. arkanoid_state->speed = 2;
  262. arkanoid_state->bounced = false;
  263. arkanoid_state->lives = 3;
  264. arkanoid_state->level = 1;
  265. arkanoid_state->score = 0;
  266. arkanoid_state->COLUMNS = 13;
  267. arkanoid_state->COLUMNS = 13;
  268. // Reset initial state
  269. arkanoid_state->initialDraw = false;
  270. arkanoid_state->gameStarted = false;
  271. }
  272. static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) {
  273. furi_assert(ctx);
  274. ArkanoidState* arkanoid_state = ctx;
  275. furi_mutex_acquire(arkanoid_state->mutex, FuriWaitForever);
  276. //Initial level draw
  277. if(!arkanoid_state->initialDraw) {
  278. arkanoid_state->initialDraw = true;
  279. // Set default font for text
  280. canvas_set_font(canvas, FontSecondary);
  281. //Draws the new level
  282. reset_level(canvas, arkanoid_state);
  283. }
  284. //Draws new bricks and resets their values
  285. for(unsigned int row = 0; row < arkanoid_state->ROWS; row++) {
  286. for(unsigned int column = 0; column < arkanoid_state->COLUMNS; column++) {
  287. if(!arkanoid_state->brick_state.isHit[row][column]) {
  288. canvas_draw_frame(canvas, 10 * column, 2 + 6 * row, 8, 4);
  289. }
  290. }
  291. }
  292. if(arkanoid_state->lives > 0) {
  293. draw_paddle(canvas, arkanoid_state);
  294. draw_ball(canvas, arkanoid_state);
  295. draw_score(canvas, arkanoid_state);
  296. draw_lives(canvas, arkanoid_state);
  297. if(arkanoid_state->brickCount == arkanoid_state->ROWS * arkanoid_state->COLUMNS) {
  298. arkanoid_state->level++;
  299. reset_level(canvas, arkanoid_state);
  300. }
  301. } else {
  302. reset_level(canvas, arkanoid_state);
  303. arkanoid_state->initialDraw = false;
  304. arkanoid_state->lives = 3;
  305. arkanoid_state->score = 0;
  306. }
  307. furi_mutex_release(arkanoid_state->mutex);
  308. }
  309. static void arkanoid_input_callback(InputEvent* input_event, void* ctx) {
  310. FuriMessageQueue* event_queue = ctx;
  311. furi_assert(event_queue);
  312. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  313. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  314. }
  315. static void arkanoid_update_timer_callback(void* ctx) {
  316. FuriMessageQueue* event_queue = ctx;
  317. furi_assert(event_queue);
  318. GameEvent event = {.type = EventTypeTick};
  319. furi_message_queue_put(event_queue, &event, 0);
  320. }
  321. int32_t arkanoid_game_app(void* p) {
  322. UNUSED(p);
  323. int32_t return_code = 0;
  324. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  325. ArkanoidState* arkanoid_state = malloc(sizeof(ArkanoidState));
  326. arkanoid_state_init(arkanoid_state);
  327. arkanoid_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  328. if(!arkanoid_state->mutex) {
  329. FURI_LOG_E(TAG, "Cannot create mutex\r\n");
  330. return_code = 255;
  331. goto free_and_exit;
  332. }
  333. // Set system callbacks
  334. ViewPort* view_port = view_port_alloc();
  335. view_port_draw_callback_set(view_port, arkanoid_draw_callback, arkanoid_state);
  336. view_port_input_callback_set(view_port, arkanoid_input_callback, event_queue);
  337. FuriTimer* timer =
  338. furi_timer_alloc(arkanoid_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  339. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
  340. // Open GUI and register view_port
  341. Gui* gui = furi_record_open(RECORD_GUI);
  342. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  343. // Call dolphin deed on game start
  344. dolphin_deed(DolphinDeedPluginGameStart);
  345. GameEvent event;
  346. for(bool processing = true; processing;) {
  347. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  348. furi_mutex_acquire(arkanoid_state->mutex, FuriWaitForever);
  349. if(event_status == FuriStatusOk) {
  350. // Key events
  351. if(event.type == EventTypeKey) {
  352. if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
  353. event.input.type == InputTypeRepeat) {
  354. switch(event.input.key) {
  355. case InputKeyBack:
  356. processing = false;
  357. break;
  358. case InputKeyRight:
  359. if(arkanoid_state->xPaddle < FLIPPER_LCD_WIDTH - 12) {
  360. arkanoid_state->xPaddle += 8;
  361. }
  362. break;
  363. case InputKeyLeft:
  364. if(arkanoid_state->xPaddle > 0) {
  365. arkanoid_state->xPaddle -= 8;
  366. }
  367. break;
  368. case InputKeyUp:
  369. if(arkanoid_state->speed < MAX_SPEED) {
  370. arkanoid_state->speed++;
  371. }
  372. break;
  373. case InputKeyDown:
  374. if(arkanoid_state->speed > 1) {
  375. arkanoid_state->speed--;
  376. }
  377. break;
  378. case InputKeyOk:
  379. if(arkanoid_state->gameStarted == false) {
  380. //Release ball if FIRE pressed
  381. arkanoid_state->ball_state.released = true;
  382. //Apply random direction to ball on release
  383. if(rand_range(0, 2) == 0) {
  384. arkanoid_state->ball_state.dx = 1;
  385. } else {
  386. arkanoid_state->ball_state.dx = -1;
  387. }
  388. //Makes sure the ball heads upwards
  389. arkanoid_state->ball_state.dy = -1;
  390. //start the game flag
  391. arkanoid_state->gameStarted = true;
  392. }
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. }
  399. }
  400. furi_mutex_release(arkanoid_state->mutex);
  401. view_port_update(view_port);
  402. }
  403. furi_timer_free(timer);
  404. view_port_enabled_set(view_port, false);
  405. gui_remove_view_port(gui, view_port);
  406. furi_record_close(RECORD_GUI);
  407. furi_record_close(RECORD_NOTIFICATION);
  408. view_port_free(view_port);
  409. furi_mutex_free(arkanoid_state->mutex);
  410. free_and_exit:
  411. free(arkanoid_state);
  412. furi_message_queue_free(event_queue);
  413. return return_code;
  414. }