coin.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <jetpack_joyride_icons.h>
  4. #include <gui/gui.h>
  5. #include "coin.h"
  6. #include "barry.h"
  7. void coin_tick(COIN* const coins, BARRY* const barry, int* const poins) {
  8. // Move coins towards the player
  9. for(int i = 0; i < COINS_MAX; i++) {
  10. if(coin_colides(&coins[i], barry)) {
  11. coins[i].point.x = 0; // Remove the coin
  12. (*poins)++;
  13. }
  14. if(coins[i].point.x > 0) {
  15. coins[i].point.x -= 1; // move left by 1 unit
  16. if(coins[i].point.x < -16) { // if the coin is out of screen
  17. coins[i].point.x = 0; // set coin x coordinate to 0 to mark it as "inactive"
  18. }
  19. }
  20. }
  21. }
  22. bool coin_colides(COIN* const coin, BARRY* const barry) {
  23. return !(
  24. barry->point.x > coin->point.x + 7 || // Barry is to the right of the coin
  25. barry->point.x + 11 < coin->point.x || // Barry is to the left of the coin
  26. barry->point.y > coin->point.y + 7 || // Barry is below the coin
  27. barry->point.y + 15 < coin->point.y); // Barry is above the coin
  28. }
  29. void spawn_random_coin(COIN* const coins) {
  30. // Check for an available slot for a new coin
  31. for(int i = 0; i < COINS_MAX; ++i) {
  32. if(coins[i].point.x <= 0) {
  33. coins[i].point.x = 127;
  34. coins[i].point.y = rand() % 64;
  35. break;
  36. }
  37. }
  38. }
  39. void draw_coins(const COIN* coins, Canvas* const canvas) {
  40. canvas_set_color(canvas, ColorBlack);
  41. for(int i = 0; i < COINS_MAX; ++i) {
  42. if(coins[i].point.x > 0) {
  43. canvas_draw_icon(canvas, coins[i].point.x, coins[i].point.y, &I_coin);
  44. }
  45. }
  46. }