arkanoid_game.c 17 KB

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