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
113
114
|
#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;
}
|