-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.h
133 lines (108 loc) · 2.72 KB
/
pong.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma once
typedef struct {
int x, y, height;
Line line;
} Bat;
typedef struct {
int size, x, y, is_moving, speed_x, speed_y;
Box box;
} Ball;
typedef struct {
int score_left, score_right;
} Scoreboard;
Scoreboard scoreboard;
Scoreboard createScoreboard() {
Scoreboard scoreboard;
scoreboard.score_left = 0;
scoreboard.score_right = 0;
return scoreboard;
}
Ball createBall() {
Ball ball;
ball.size = 2;
ball.x = SCREEN_WIDTH / 2 - ball.size / 2;
ball.y = SCREEN_HEIGHT / 2 - ball.size / 2;
ball.box = createBox(createColor(0, 255, 0), ball.x, ball.y, ball.x + ball.size, ball.y + ball.size);
ball.is_moving = 0;
ball.speed_x = 0;
ball.speed_y = 0;
return ball;
}
Ball accelerateBall(Ball ball) {
if(ball.speed_x < 0) ball.speed_x -= rand() % 2;
else ball.speed_x += rand() % 2;
if(ball.speed_y < 0) ball.speed_y -= rand() % 2;
else ball.speed_y += rand() % 2;
return ball;
}
Ball resetBall(Ball ball) {
ball = createBall();
return ball;
}
Ball moveBall(Ball ball, Bat batLeft, Bat batRight) {
if(!ball.is_moving) return ball;
ball.x += ball.speed_x;
ball.y += ball.speed_y;
ball.box = moveBox(ball.box, ball.x, ball.y);
if(ball.x < batLeft.x && ball.x > 10 && ball.y > batLeft.y && ball.y < batLeft.y + batLeft.height) {
ball.speed_x = ball.speed_x *= -1;
ball.x = batLeft.x;
ball = accelerateBall(ball);
}
if(ball.x > batRight.x && ball.x < SCREEN_WIDTH - 10 && ball.y > batRight.y && ball.y < batRight.y + batRight.height) {
ball.speed_x = ball.speed_x *= -1;
ball.x = batRight.x;
ball = accelerateBall(ball);
}
if(ball.y > SCREEN_HEIGHT - 20) {
ball.speed_y = ball.speed_y *= -1;
ball.y = SCREEN_HEIGHT - 20;
}
if(ball.y < 20) {
ball.speed_y = ball.speed_y *= -1;
ball.y = 20;
}
if(ball.x < -5) {
ball = resetBall(ball);
scoreboard.score_right ++;
}
if(ball.x > SCREEN_WIDTH + 5) {
ball = resetBall(ball);
scoreboard.score_left ++;
}
return ball;
}
Ball kickBall(Ball ball) {
if(ball.is_moving) return ball;
ball.speed_x = 1 + rand() % 2 - rand() % 4;
ball.speed_y = 1 + rand() % 2 - rand() % 2;
ball.is_moving = 1;
return ball;
}
void drawBall(Ball ball) {
drawBox(ball.box);
}
Bat createBat(int x) {
Bat bat;
bat.x = x;
bat.y = SCREEN_HEIGHT / 2 - 30;
bat.height = 60;
bat.line = createLine(createColor(255, 255, 255), bat.x, bat.y, bat.x, bat.y + bat.height);
return bat;
}
Bat moveBat(Bat bat, int delta_y) {
if(delta_y < 0 && bat.y + delta_y <= 22) {
bat.y = 20;
return bat;
}
if(delta_y > 0 && bat.y + bat.height + delta_y > SCREEN_HEIGHT - 22) {
bat.y = SCREEN_HEIGHT - 20 - bat.height;
return bat;
}
bat.y += delta_y;
bat.line = moveLine(bat.line, bat.x, bat.y, bat.x, bat.y + bat.height);
return bat;
}
void drawBat(Bat bat) {
drawLine(bat.line);
}