1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#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;
}
|