rayflap

A Flappy Bird clone written in C and compiled to WebAssembly.
git clone https://unitehenry.com/git/rayflap.git
Log | Files | Refs | README

game.c (10690B)


      1 #include "raylib.h"
      2 #include <emscripten/emscripten.h>
      3 #include <math.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include <time.h>
      7 
      8 #define MainLoop emscripten_set_main_loop
      9 #define SeedRandom srand(time(NULL))
     10 #define InitAudio init_audio()
     11 
     12 /* Properties */
     13 static const char *title = "rayflap";
     14 static const int screenWidth = 288;
     15 static const int screenHeight = 512;
     16 static const int maxGravity = 3;
     17 static const int maxBirdRotate = 20.0f;
     18 
     19 /* Textures */
     20 Texture2D backgroundTexture;
     21 Texture2D messageTexture;
     22 Texture2D birdTexture;
     23 Texture2D baseTexture;
     24 Texture2D pipeTexture;
     25 Texture2D scoreTexture[10];
     26 
     27 /* Sounds */
     28 Sound pointSound;
     29 Sound hitSound;
     30 Sound wingSound;
     31 
     32 /* Game State */
     33 typedef enum { TITLE, PLAY } Screen;
     34 Screen screen = TITLE;
     35 bool isPaused = false;
     36 float backgroundScroll = 0.0f;
     37 float baseScroll = 0.0f;
     38 char *birdColor = NULL;
     39 float birdRotate = 0.0f;
     40 float thrust = 0.0f;
     41 float gravity = 0.0f;
     42 int lastCountedPipe = -1;
     43 int score = 0;
     44 
     45 /* Entities */
     46 Rectangle birdRect = {0, 0, 0.0f, 0.0f};
     47 Rectangle baseRect1 = {0, 0, 0.0f, 0.0f};
     48 Rectangle baseRect2 = {0, 0, 0.0f, 0.0f};
     49 Rectangle pipeRects[4] = {
     50     {0, 0, 0.0f, 0.0f},
     51     {0, 0, 0.0f, 0.0f},
     52     {0, 0, 0.0f, 0.0f},
     53     {0, 0, 0.0f, 0.0f},
     54 };
     55 
     56 void init_audio() {
     57   InitAudioDevice();
     58 
     59   pointSound = LoadSound("assets/audio/point.ogg");
     60   hitSound = LoadSound("assets/audio/hit.ogg");
     61   wingSound = LoadSound("assets/audio/wing.ogg");
     62 }
     63 
     64 void play_sound(Sound sound) {
     65   if (!IsSoundValid(sound))
     66     return;
     67 
     68   if (isPaused)
     69     return;
     70 
     71   PlaySound(sound);
     72 }
     73 
     74 void draw_message() {
     75   if (screen != TITLE)
     76     return;
     77 
     78   if (!IsTextureValid(messageTexture)) {
     79     messageTexture = LoadTexture("assets/sprites/message.png");
     80   }
     81 
     82   float centerX = (screenWidth - messageTexture.width) / 2.0f;
     83 
     84   float centerY = (screenHeight - messageTexture.height) / 2.0f;
     85 
     86   DrawTexture(messageTexture, centerX, centerY, WHITE);
     87 }
     88 
     89 void draw_background() {
     90   if (!IsTextureValid(backgroundTexture)) {
     91     backgroundTexture = LoadTexture("assets/sprites/background-day.png");
     92   }
     93 
     94   static const float scrollSpeed = 0.5f;
     95 
     96   backgroundScroll -= scrollSpeed;
     97 
     98   DrawTexture(backgroundTexture,
     99               (int)fmodf(backgroundScroll, (float)screenWidth), 0, WHITE);
    100 
    101   DrawTexture(backgroundTexture,
    102               (int)fmodf(backgroundScroll, (float)screenWidth) + screenWidth, 0,
    103               WHITE);
    104 }
    105 
    106 void draw_pipes() {
    107   if (!IsTextureValid(pipeTexture)) {
    108     pipeTexture = LoadTexture("assets/sprites/pipe-green.png");
    109   }
    110 
    111   if (screen == TITLE)
    112     return;
    113 
    114   for (int i = 0; i < sizeof(pipeRects) / sizeof(pipeRects[0]); i++) {
    115     bool topPipe = i % 2 != 0;
    116 
    117     Rectangle source = {0, 0, (float)pipeTexture.width,
    118                         topPipe ? -(float)pipeTexture.height
    119                                 : (float)pipeTexture.height};
    120 
    121     Vector2 origin = {0, 0};
    122 
    123     float rotate = 0.0f;
    124 
    125     DrawTexturePro(pipeTexture, source, pipeRects[i], origin, rotate, WHITE);
    126   }
    127 }
    128 
    129 void draw_bird() {
    130   if (birdColor == NULL) {
    131     char *birdColors[3] = {"red", "blue", "yellow"};
    132 
    133     birdColor =
    134         birdColors[rand() % (sizeof(birdColors) / sizeof(birdColors[0]))];
    135   }
    136 
    137   static Texture2D downFlapTexture;
    138 
    139   static Texture2D upFlapTexture;
    140 
    141   if (!IsTextureValid(birdTexture)) {
    142     char *texturePath;
    143 
    144     asprintf(&texturePath, "assets/sprites/%sbird-downflap.png", birdColor);
    145 
    146     downFlapTexture = LoadTexture(texturePath);
    147 
    148     SetTextureFilter(downFlapTexture, TEXTURE_FILTER_BILINEAR);
    149 
    150     asprintf(&texturePath, "assets/sprites/%sbird-upflap.png", birdColor);
    151 
    152     upFlapTexture = LoadTexture(texturePath);
    153 
    154     SetTextureFilter(upFlapTexture, TEXTURE_FILTER_BILINEAR);
    155   }
    156 
    157   birdTexture = thrust > 0 ? downFlapTexture : upFlapTexture;
    158 
    159   if (screen == TITLE) {
    160     birdTexture = downFlapTexture;
    161   }
    162 
    163   Rectangle source = {0, 0, (float)birdTexture.width,
    164                       (float)birdTexture.height};
    165 
    166   Vector2 origin = {0, 0};
    167 
    168   float rotate = 0.0f;
    169 
    170   if (screen == PLAY) {
    171     if (thrust > 0) {
    172       birdRotate += -4.0f;
    173       birdRotate = fmax(birdRotate, maxBirdRotate * -1);
    174     } else {
    175       birdRotate += 4.0f;
    176       birdRotate = fmin(birdRotate, maxBirdRotate);
    177     }
    178 
    179     rotate = birdRotate;
    180   }
    181 
    182   DrawTexturePro(birdTexture, source, birdRect, origin, rotate, WHITE);
    183 }
    184 
    185 void draw_base() {
    186   if (!IsTextureValid(baseTexture)) {
    187     baseTexture = LoadTexture("assets/sprites/base.png");
    188   }
    189 
    190   float bottomY =
    191       screenHeight - baseTexture.height + (baseTexture.height / 2.0f);
    192 
    193   static const float scrollSpeed = 1.2f;
    194 
    195   baseScroll -= scrollSpeed;
    196 
    197   Rectangle source = {0, 0, (float)baseTexture.width,
    198                       (float)baseTexture.height};
    199 
    200   Vector2 origin = {0, 0};
    201 
    202   baseRect1.x = (int)fmodf(baseScroll, (float)screenWidth);
    203   baseRect1.y = bottomY;
    204   baseRect1.width = (float)baseTexture.width;
    205   baseRect1.height = (float)baseTexture.height;
    206 
    207   baseRect2.x = (int)fmodf(baseScroll, (float)screenWidth) + screenWidth;
    208   baseRect2.y = bottomY;
    209   baseRect2.width = (float)baseTexture.width;
    210   baseRect2.height = (float)baseTexture.height;
    211 
    212   DrawTexturePro(baseTexture, source, baseRect1, origin, 0.0f, WHITE);
    213 
    214   DrawTexturePro(baseTexture, source, baseRect2, origin, 0.0f, WHITE);
    215 }
    216 
    217 void draw_score() {
    218   if (!IsTextureValid(scoreTexture[0])) {
    219     for (int i = 0; i < sizeof(scoreTexture) / sizeof(scoreTexture[0]); i++) {
    220       char *texturePath;
    221 
    222       asprintf(&texturePath, "assets/sprites/%i.png", i);
    223 
    224       scoreTexture[i] = LoadTexture(texturePath);
    225     }
    226   }
    227 
    228   if (screen == TITLE) {
    229     return;
    230   }
    231 
    232   char *renderScore;
    233 
    234   asprintf(&renderScore, "%i", score);
    235 
    236   float scoreY = 0.0f + (scoreTexture[0].height * 1.5f);
    237 
    238   int numDigits = strlen(renderScore);
    239 
    240   float digitWidth = scoreTexture[0].width;
    241 
    242   float totalWidth = numDigits * digitWidth;
    243 
    244   float startX = (screenWidth - totalWidth) / 2.0f;
    245 
    246   for (int i = 0; i < numDigits; i++) {
    247     float scoreX = startX + (digitWidth * i);
    248 
    249     DrawTexture(scoreTexture[renderScore[i] - '0'], scoreX, scoreY, WHITE);
    250   }
    251 }
    252 
    253 void draw() {
    254   BeginDrawing();
    255 
    256   if (isPaused) {
    257     EndDrawing();
    258     return;
    259   }
    260 
    261   draw_background();
    262 
    263   draw_pipes();
    264 
    265   draw_base();
    266 
    267   draw_message();
    268 
    269   draw_bird();
    270 
    271   draw_score();
    272 
    273   EndDrawing();
    274 }
    275 
    276 Vector2 random_pipe_y() {
    277   if (!IsTextureValid(pipeTexture)) {
    278     Vector2 origin = {0.0f, 0.0f};
    279     return origin;
    280   }
    281 
    282   float unit = screenHeight / 20.0f;
    283 
    284   float lowerBound = pipeTexture.height - (unit * 5);
    285 
    286   float upperBound = screenHeight - pipeTexture.height + (unit * 8);
    287 
    288   float pipeY = lowerBound + fmodf(rand(), (upperBound - lowerBound + 1));
    289 
    290   float gap = (unit * 4);
    291 
    292   float bottomPipeY = pipeY;
    293 
    294   float topPipeY = pipeY - gap - pipeTexture.height;
    295 
    296   Vector2 pipeVector = {bottomPipeY, topPipeY};
    297 
    298   return pipeVector;
    299 }
    300 
    301 void reset_pipes() {
    302   if (!IsTextureValid(pipeTexture))
    303     return;
    304 
    305   for (int i = 0; i < sizeof(pipeRects) / sizeof(pipeRects[0]); i += 2) {
    306     float startX = i > 0 ? pipeRects[i - 1].x + screenWidth - pipeTexture.width
    307                          : (screenWidth + pipeTexture.width);
    308 
    309     Vector2 pipeY = random_pipe_y();
    310 
    311     Rectangle bottomPipeRect = pipeRects[i];
    312 
    313     bottomPipeRect.x = startX;
    314     bottomPipeRect.y = pipeY.x;
    315     bottomPipeRect.width = (float)pipeTexture.width;
    316     bottomPipeRect.height = (float)pipeTexture.height;
    317 
    318     pipeRects[i] = bottomPipeRect;
    319 
    320     Rectangle topPipeRect = pipeRects[i + 1];
    321 
    322     topPipeRect.x = startX;
    323     topPipeRect.y = pipeY.y;
    324     topPipeRect.width = (float)pipeTexture.width;
    325     topPipeRect.height = (float)pipeTexture.height;
    326 
    327     pipeRects[i + 1] = topPipeRect;
    328   }
    329 }
    330 
    331 void reset_bird() {
    332   if (!IsTextureValid(birdTexture))
    333     return;
    334 
    335   float centerX = (screenWidth - birdTexture.width) / 2.0f;
    336 
    337   float centerY = (screenHeight - birdTexture.height) / 2.0f;
    338 
    339   float messageOffsetY = 0;
    340 
    341   if (IsTextureValid(messageTexture)) {
    342     messageOffsetY = (messageTexture.height - birdTexture.height) / 5.2f;
    343   }
    344 
    345   birdRect.x = centerX;
    346   birdRect.y = centerY + messageOffsetY;
    347   birdRect.width = (float)birdTexture.width;
    348   birdRect.height = (float)birdTexture.height;
    349 }
    350 
    351 void collide() {
    352   Rectangle baseRects[2] = {baseRect1, baseRect2};
    353 
    354   for (int i = 0; i < (sizeof(baseRects) / sizeof(baseRects[0])); i++) {
    355     Rectangle baseRect = baseRects[i];
    356 
    357     if (CheckCollisionRecs(birdRect, baseRect)) {
    358       play_sound(hitSound);
    359       isPaused = true;
    360     }
    361   }
    362 
    363   for (int i = 0; i < sizeof(pipeRects) / sizeof(pipeRects[0]); i++) {
    364     Rectangle pipeRect = pipeRects[i];
    365 
    366     if (CheckCollisionRecs(birdRect, pipeRect)) {
    367       play_sound(hitSound);
    368       isPaused = true;
    369     }
    370   }
    371 }
    372 
    373 void reset() {
    374   reset_pipes();
    375   reset_bird();
    376   isPaused = false;
    377   screen = TITLE;
    378   birdColor = NULL;
    379   lastCountedPipe = -1;
    380   score = 0;
    381 }
    382 
    383 void input() {
    384   if (!IsMouseButtonReleased(0))
    385     return;
    386 
    387   if (isPaused) {
    388     reset();
    389     return;
    390   }
    391 
    392   if (screen == TITLE) {
    393     screen = PLAY;
    394   }
    395 
    396   play_sound(wingSound);
    397   thrust = 3.75f;
    398 }
    399 
    400 void update() {
    401   input();
    402 
    403   if (screen == TITLE) {
    404     reset_pipes();
    405     reset_bird();
    406     return;
    407   }
    408 
    409   if (thrust > 0) {
    410     thrust -= .1;
    411     birdRect.y -= thrust;
    412     gravity = 1.0f;
    413   } else {
    414     if (gravity < maxGravity) {
    415       gravity += .1;
    416     }
    417     birdRect.y += gravity;
    418   }
    419 
    420   static const float scrollSpeed = 1.2f;
    421 
    422   float offscreenX = 0.0f - pipeTexture.width;
    423 
    424   int lastIdx = (sizeof(pipeRects) / sizeof(pipeRects[0])) - 1;
    425 
    426   for (int i = 0; i < sizeof(pipeRects) / sizeof(pipeRects[0]); i += 2) {
    427     float startX = (i > 0 ? pipeRects[i - 1].x : pipeRects[lastIdx].x) +
    428                    screenWidth - pipeTexture.width;
    429 
    430     Rectangle bottomPipeRect = pipeRects[i];
    431 
    432     bottomPipeRect.x -= scrollSpeed;
    433     bottomPipeRect.width = (float)pipeTexture.width;
    434     bottomPipeRect.height = (float)pipeTexture.height;
    435 
    436     Rectangle topPipeRect = pipeRects[i + 1];
    437 
    438     topPipeRect.x -= scrollSpeed;
    439     topPipeRect.width = (float)pipeTexture.width;
    440     topPipeRect.height = (float)pipeTexture.height;
    441 
    442     if (bottomPipeRect.x < (screenWidth / 2.0f)) {
    443       if (lastCountedPipe != i) {
    444         play_sound(pointSound);
    445 
    446         score++;
    447         lastCountedPipe = i;
    448       }
    449     }
    450 
    451     if (topPipeRect.x < offscreenX) {
    452       Vector2 pipeY = random_pipe_y();
    453 
    454       bottomPipeRect.x = startX;
    455       bottomPipeRect.y = pipeY.x;
    456 
    457       topPipeRect.x = startX;
    458       topPipeRect.y = pipeY.y;
    459     }
    460 
    461     pipeRects[i] = bottomPipeRect;
    462     pipeRects[i + 1] = topPipeRect;
    463   }
    464 
    465   collide();
    466 }
    467 
    468 void game_loop(void) {
    469   update();
    470 
    471   draw();
    472 }
    473 
    474 int main() {
    475   SeedRandom;
    476 
    477   InitWindow(screenWidth, screenHeight, title);
    478 
    479   InitAudio;
    480 
    481   MainLoop(game_loop, 60, 1);
    482 
    483   return 0;
    484 }