115 lines
2.4 KiB
C
115 lines
2.4 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 {
|
|
float x;
|
|
float y;
|
|
int width;
|
|
int height;
|
|
int velocity;
|
|
int controls[3];
|
|
} Player;
|
|
int init_vel = 300;
|
|
|
|
void move_player(Player* p){
|
|
if (IsKeyDown(p->controls[MOVE_DOWN])){
|
|
p->y += p->velocity * GetFrameTime();
|
|
if (p->y > screenHeight){
|
|
p->y = -p->height;
|
|
}
|
|
} else if (IsKeyDown(p->controls[MOVE_UP])){
|
|
p->y -= p->velocity * GetFrameTime();
|
|
if (p->y < -p->height){
|
|
p->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){
|
|
DrawRectangle(p->x, p->y, p->width, p->height, BLACK);
|
|
}
|
|
|
|
|
|
typedef struct ball {
|
|
float x;
|
|
float y;
|
|
float r;
|
|
int vel_x;
|
|
int vel_y;
|
|
Player* target;
|
|
} Ball;
|
|
|
|
void move_ball(Ball *b){
|
|
b->y += b->vel_y * GetFrameTime();
|
|
if (b->y < b->r || b->y > screenHeight - b->r) {
|
|
b->vel_y *= -1;
|
|
b->y += b->vel_y * GetFrameTime();
|
|
}
|
|
b->x += b->vel_x * GetFrameTime();
|
|
if (b->x < b->r || b->x > screenWidth - b->r) {
|
|
b->vel_x *= -1;
|
|
b->x += b->vel_x * GetFrameTime();
|
|
}
|
|
/* if (p->x > p->target->){ */
|
|
|
|
/* } */
|
|
}
|
|
|
|
void draw_ball(Ball* b){
|
|
DrawCircle(b->x, b->y, b->r, BLACK);
|
|
}
|
|
|
|
int main(void){
|
|
InitWindow(screenWidth, screenHeight, "Pong");
|
|
Player p[2] =
|
|
{{ .x = 30, .y = 30, .width = 10, .height = 60, .velocity = init_vel,
|
|
.controls = {KEY_W, KEY_S, KEY_LEFT_SHIFT}},
|
|
{ .x = screenWidth - 30, .width = 10, .height = 60, .velocity = init_vel,
|
|
.controls = {KEY_UP, KEY_DOWN, KEY_RIGHT_SHIFT}}};
|
|
Ball ball = { .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);
|
|
|
|
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;
|
|
}
|