yuppiie
This commit is contained in:
parent
5be8f91587
commit
a116731452
2 changed files with 53 additions and 4 deletions
BIN
MyGame
BIN
MyGame
Binary file not shown.
57
main.c
57
main.c
|
|
@ -1,3 +1,4 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue