113 lines
2.6 KiB
C
113 lines
2.6 KiB
C
#include <raylib.h>
|
|
|
|
const int screenWidth = 800;
|
|
const int screenHeight = 450;
|
|
|
|
enum control_keys {
|
|
MOVE_UP,
|
|
MOVE_DOWN,
|
|
BOOST
|
|
} control_key;
|
|
|
|
typedef struct player {
|
|
Rectangle rec;
|
|
int velocity;
|
|
int controls[3];
|
|
} Player;
|
|
int init_vel = 300;
|
|
|
|
void move_player(Player* p){
|
|
if (IsKeyDown(p->controls[MOVE_DOWN])){
|
|
p->rec.y += p->velocity * GetFrameTime();
|
|
if (p->rec.y > screenHeight){
|
|
p->rec.y = -p->rec.height;
|
|
}
|
|
} else if (IsKeyDown(p->controls[MOVE_UP])){
|
|
p->rec.y -= p->velocity * GetFrameTime();
|
|
if (p->rec.y < -p->rec.height){
|
|
p->rec.y = screenHeight;
|
|
}
|
|
}
|
|
}
|
|
|
|
void apply_boost(Player* p){
|
|
if (IsKeyPressed(p->controls[BOOST])){
|
|
p->velocity *= 2;
|
|
}
|
|
else if (IsKeyUp(p->controls[BOOST])){
|
|
p->velocity = init_vel;
|
|
}
|
|
|
|
}
|
|
|
|
void draw_player(Player* p){
|
|
DrawRectangleRec(p->rec, BLACK);
|
|
}
|
|
|
|
|
|
typedef struct ball {
|
|
Vector2 pos;
|
|
float r;
|
|
int vel_x;
|
|
int vel_y;
|
|
Player* target;
|
|
} Ball;
|
|
|
|
void move_ball(Ball *b, Player p[2]){
|
|
b->pos.y += b->vel_y * GetFrameTime();
|
|
if (b->pos.y < b->r || b->pos.y > screenHeight - b->r) {
|
|
b->vel_y *= -1;
|
|
b->pos.y += b->vel_y * GetFrameTime();
|
|
}
|
|
b->pos.x += b->vel_x * GetFrameTime();
|
|
if (b->pos.x < b->r || b->pos.x > screenWidth - b->r ||
|
|
CheckCollisionCircleRec(b->pos, b->r, p[0].rec) ||
|
|
CheckCollisionCircleRec(b->pos, b->r, p[1].rec)) {
|
|
b->vel_x *= -1;
|
|
b->pos.x += b->vel_x * GetFrameTime();
|
|
}
|
|
/* if (p->x > p->target->){ */
|
|
|
|
/* } */
|
|
}
|
|
|
|
void draw_ball(Ball* b){
|
|
DrawCircleV(b->pos, b->r, BLACK);
|
|
}
|
|
|
|
int main(void){
|
|
InitWindow(screenWidth, screenHeight, "Pong");
|
|
Player p[2] =
|
|
{{ .rec = {.x = 30, .y = 30, .width = 10, .height = 60}, .velocity = init_vel,
|
|
.controls = {KEY_W, KEY_S, KEY_LEFT_SHIFT}},
|
|
{ .rec = {.x = screenWidth - 30, .width = 10, .height = 60}, .velocity = init_vel,
|
|
.controls = {KEY_UP, KEY_DOWN, KEY_RIGHT_SHIFT}}};
|
|
Ball ball = { .pos = {.x = screenWidth / 2, .y = screenHeight / 2}, .r = 10,
|
|
.vel_x = 300, .vel_y = 300};
|
|
|
|
while (!WindowShouldClose()){
|
|
|
|
apply_boost(&p[0]);
|
|
apply_boost(&p[1]);
|
|
|
|
move_player(&p[0]);
|
|
move_player(&p[1]);
|
|
|
|
move_ball(&ball, p);
|
|
|
|
BeginDrawing();
|
|
|
|
ClearBackground(WHITE);
|
|
DrawText("Hello world!", screenWidth/2, screenHeight/2, 20, BLACK);
|
|
|
|
draw_player(&p[0]);
|
|
draw_player(&p[1]);
|
|
draw_ball(&ball);
|
|
|
|
EndDrawing();
|
|
}
|
|
|
|
CloseWindow();
|
|
return 0;
|
|
}
|