139 lines
3.3 KiB
C
139 lines
3.3 KiB
C
#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;
|
|
};
|
|
|
|
struct Pipe {
|
|
int positionX;
|
|
int positionY;
|
|
};
|
|
|
|
struct DoublePipe {
|
|
struct Pipe pipe1;
|
|
struct Pipe pipe2;
|
|
};
|
|
|
|
void Jump(float* vel) {
|
|
if (IsKeyPressed(KEY_UP)) {
|
|
*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);
|
|
}
|
|
|
|
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);
|
|
|
|
return Game;
|
|
}
|
|
|
|
enum GameState {
|
|
Start,
|
|
Game,
|
|
Restart
|
|
}
|
|
|
|
int main(void) {
|
|
InitWindow(800, 600, "HELLO RAYLRIN!!!");
|
|
SetTargetFPS(60);
|
|
|
|
enum GameState gs = Start;
|
|
|
|
struct Birdie bird = {0.0, 300};
|
|
struct DoublePipe double_pipe = CreateDoublePipe(pipe_gap, 200, pipe_height);
|
|
|
|
while (!WindowShouldClose()) {
|
|
BeginDrawing();
|
|
|
|
switch (GameState) {
|
|
case Game:
|
|
MainGame(&double_pipe, &bird, pipe_height, pipe_gap);
|
|
case Restart:
|
|
//func for restart
|
|
}
|
|
|
|
EndDrawing();
|
|
}
|
|
|
|
CloseWindow();
|
|
return 0;
|
|
}
|