196 lines
5.1 KiB
C
196 lines
5.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "raylib.h"
|
|
|
|
const float gravity = 0.25;
|
|
const int bird_pos_x = 400;
|
|
const float bird_radius = 10.0;
|
|
const int pipe_vel = 3;
|
|
const int pipe_width = 50;
|
|
const int pipe_gap = 50;
|
|
const int pipe_height = 900;
|
|
|
|
struct Birdie {
|
|
float velocityY;
|
|
int positionY;
|
|
bool is_colliding;
|
|
int score;
|
|
};
|
|
|
|
struct Pipe {
|
|
int positionX;
|
|
int positionY;
|
|
};
|
|
|
|
struct DoublePipe {
|
|
struct Pipe pipe1;
|
|
struct Pipe pipe2;
|
|
};
|
|
|
|
void Jump(float* vel) {
|
|
if (IsKeyPressed(KEY_UP) || GetTouchPointCount() != 0) {
|
|
*vel = -7;
|
|
}
|
|
}
|
|
|
|
void VelocityControl(float* vel) {
|
|
if (*vel < -12) {
|
|
*vel = -12;
|
|
}
|
|
}
|
|
|
|
void FlappyBird(struct Birdie* bird) {
|
|
Jump(&bird->velocityY);
|
|
VelocityControl(&bird->velocityY);
|
|
|
|
bird->velocityY += gravity;
|
|
bird->positionY += (int)bird->velocityY;
|
|
|
|
if (bird->positionY > GetScreenHeight()) {
|
|
bird->positionY = GetScreenHeight();
|
|
bird->velocityY = 0;
|
|
}
|
|
|
|
else if (bird->positionY < 0) {
|
|
bird->positionY = 0;
|
|
bird->velocityY = 0;
|
|
}
|
|
|
|
DrawCircle(bird_pos_x, bird->positionY, bird_radius, YELLOW);
|
|
}
|
|
|
|
struct DoublePipe CreateDoublePipe(int gap, int posY, int height) {
|
|
struct DoublePipe double_pipe = {{GetScreenWidth(), posY - height - gap}, {GetScreenWidth(), posY + gap}};
|
|
|
|
return double_pipe;
|
|
}
|
|
|
|
void DrawDoublePipe(struct DoublePipe* double_pipe) {
|
|
DrawRectangle(double_pipe->pipe1.positionX, double_pipe->pipe1.positionY, pipe_width, pipe_height, GREEN);
|
|
DrawRectangle(double_pipe->pipe2.positionX, double_pipe->pipe2.positionY, pipe_width, pipe_height, GREEN);
|
|
}
|
|
|
|
void MoveDoublePipe(struct DoublePipe* double_pipe, int height, int gap) {
|
|
if (double_pipe->pipe1.positionX + pipe_width < 0 && double_pipe->pipe2.positionX < 0) {
|
|
double_pipe->pipe1.positionX = GetScreenWidth();
|
|
double_pipe->pipe2.positionX = GetScreenWidth();
|
|
|
|
int randint = rand() % (GetScreenHeight() + 1);
|
|
|
|
double_pipe->pipe1.positionY = randint - height - gap;
|
|
double_pipe->pipe2.positionY = randint + gap;
|
|
|
|
}
|
|
|
|
double_pipe->pipe1.positionX -= 6;
|
|
double_pipe->pipe2.positionX -= 6;
|
|
}
|
|
|
|
bool IsPlayerColliding(struct DoublePipe* double_pipe, struct Birdie* bird) {
|
|
Rectangle rec1 = {double_pipe->pipe1.positionX, double_pipe->pipe1.positionY, pipe_width, pipe_height};
|
|
Rectangle rec2 = {double_pipe->pipe2.positionX, double_pipe->pipe2.positionY, pipe_width, pipe_height};
|
|
Vector2 center = {bird_pos_x, bird->positionY};
|
|
|
|
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,
|
|
Restart
|
|
};
|
|
|
|
enum GameState MainGame(struct DoublePipe* double_pipe, struct Birdie* bird, int pipe_height, int pipe_gap) {
|
|
if (!IsPlayerColliding(double_pipe, bird)) {
|
|
ClearBackground(BLACK);
|
|
} else {
|
|
return Restart;
|
|
}
|
|
|
|
FlappyBird(bird);
|
|
|
|
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) {
|
|
InitWindow(800, 600, "HELLO RAYLRIN!!!");
|
|
SetTargetFPS(60);
|
|
|
|
enum GameState gs = Start;
|
|
|
|
struct Birdie bird = {0.0, 300, false, 0};
|
|
struct DoublePipe double_pipe = CreateDoublePipe(pipe_gap, 200, pipe_height);
|
|
|
|
while (!WindowShouldClose()) {
|
|
BeginDrawing();
|
|
|
|
switch (gs) {
|
|
case Game:
|
|
gs = MainGame(&double_pipe, &bird, pipe_height, pipe_gap);
|
|
break;
|
|
case Restart:
|
|
gs = RestartGame(&double_pipe, &bird);
|
|
break;
|
|
case Start:
|
|
gs = StartGame();
|
|
break;
|
|
}
|
|
|
|
EndDrawing();
|
|
}
|
|
|
|
CloseWindow();
|
|
return 0;
|
|
}
|