diff --git a/MyGame b/MyGame index d0f1a44..240de3f 100755 Binary files a/MyGame and b/MyGame differ diff --git a/main.c b/main.c index bad0371..50311e7 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,4 @@ +#include #include #include "raylib.h" @@ -12,6 +13,8 @@ const int pipe_height = 900; struct Birdie { float velocityY; int positionY; + bool is_colliding; + int score; }; struct Pipe { @@ -25,7 +28,7 @@ struct DoublePipe { }; void Jump(float* vel) { - if (IsKeyPressed(KEY_UP)) { + if (IsKeyPressed(KEY_UP) || GetTouchPointCount() != 0) { *vel = -7; } } @@ -91,6 +94,21 @@ bool IsPlayerColliding(struct DoublePipe* double_pipe, struct Birdie* bird) { return CheckCollisionCircleRec(center, bird_radius, rec1) || CheckCollisionCircleRec(center, bird_radius, rec2); } +void Score(struct DoublePipe* double_pipe, struct Birdie* bird) { + Rectangle rec1 = {double_pipe->pipe1.positionX, double_pipe->pipe1.positionY, pipe_width, pipe_height*25}; + Vector2 center = {bird_pos_x, bird->positionY}; + + if (bird->is_colliding && !CheckCollisionCircleRec(center, bird_radius, rec1)) { + bird->score += 1; + } + + bird->is_colliding = CheckCollisionCircleRec(center, bird_radius, rec1); + + char array[20]; + sprintf(array, "%d", bird->score); + DrawText(array, 10, 10, 12, WHITE); +} + enum GameState { Start, Game, @@ -109,11 +127,41 @@ enum GameState MainGame(struct DoublePipe* double_pipe, struct Birdie* bird, int DrawDoublePipe(double_pipe); MoveDoublePipe(double_pipe, pipe_height, pipe_gap); + Score(double_pipe, bird); + return Game; } enum GameState StartGame() { + ClearBackground(BLACK); + DrawText("Flappy bird", (GetScreenWidth()/2-(MeasureText("Flappy bird", 26)/2)), (GetScreenHeight()/2-13), 26, WHITE); + DrawText("Press up to start", (GetScreenWidth()/2-(MeasureText("Press up to start", 12)/2)), (GetScreenHeight()/2+38), 12, WHITE); + if (IsKeyPressed(KEY_UP) || GetTouchPointCount() != 0) { + return Game; + } + + return Start; +} + +enum GameState RestartGame(struct DoublePipe* double_pipe, struct Birdie* bird) { + ClearBackground(BLACK); + + char array[200]; + sprintf(array, "Your final score was: %d", bird->score); + DrawText(array, (GetScreenWidth()/2-(MeasureText(array, 26)/2)), (GetScreenHeight()/2-13), 26, WHITE); + DrawText("Press 'r' to restart", (GetScreenWidth()/2-(MeasureText("Press 'r' to restart", 12)/2)), (GetScreenHeight()/2+38), 12, WHITE); + + if (IsKeyPressed(KEY_R) || GetTouchPointCount() != 0) { + struct Birdie new_bird = {0.0, 300, false, 0}; + + *bird = new_bird; + *double_pipe = CreateDoublePipe(pipe_gap, 200, pipe_height); + + return Game; + } + + return Restart; } int main(void) { @@ -122,7 +170,7 @@ int main(void) { enum GameState gs = Start; - struct Birdie bird = {0.0, 300}; + struct Birdie bird = {0.0, 300, false, 0}; struct DoublePipe double_pipe = CreateDoublePipe(pipe_gap, 200, pipe_height); while (!WindowShouldClose()) { @@ -130,12 +178,13 @@ int main(void) { switch (gs) { case Game: - MainGame(&double_pipe, &bird, pipe_height, pipe_gap); + gs = MainGame(&double_pipe, &bird, pipe_height, pipe_gap); break; case Restart: + gs = RestartGame(&double_pipe, &bird); break; case Start: - MainGame(&double_pipe, &bird, pipe_height, pipe_gap); + gs = StartGame(); break; }