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
|
blob Paddle {
x: float
y: float
}
blob Ball {
x: float
y: float
vx: float
vy: float
}
blob Player {
paddle: Paddle
}
blob State {
p1: Player
p2: Player
ball: Ball
}
ball_speed := 2.0
SPEED := 10.0
abs := fn a: float -> float {
if a < 0.0 {
ret -a
}
ret a
}
and := fn a: bool, b: bool -> bool {
if a {
ret b
}
ret false
}
or := fn a: bool, b: bool -> bool {
if a {
ret true
}
ret b
}
rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: float, bw: float, bh: float -> bool {
dx := abs(ax - bx) + (-aw - bw) / 2.
dy := abs(ay - by) + (-ah - bh) / 2.
ret and(dx < 0., dy < 0.)
}
ball_and_paddle_check := fn pad: Paddle, ball: Ball {
if rect_overlap(ball.x, ball.y, 0.2, 0.2, pad.x, pad.y, 0.2, 1.) {
ball.vx = -ball.vx
}
}
update := fn state: State {
delta := get_delta()
speed := delta * SPEED
if key_down("w") {
state.p1.paddle.y = state.p1.paddle.y - speed
}
if key_down("s") {
state.p1.paddle.y = state.p1.paddle.y + speed
}
if key_down("i") {
state.p2.paddle.y = state.p2.paddle.y - speed
}
if key_down("k") {
state.p2.paddle.y = state.p2.paddle.y + speed
}
state.ball.x = state.ball.x + delta * state.ball.vx
state.ball.y = state.ball.y + delta * state.ball.vy
ball_and_paddle_check(state.p1.paddle, state.ball)
ball_and_paddle_check(state.p2.paddle, state.ball)
}
draw := fn state: State {
clear()
draw_rectangle(state.p1.paddle.x, state.p1.paddle.y, 0.2, 1.)
draw_rectangle(state.p2.paddle.x, state.p2.paddle.y, 0.2, 1.)
draw_rectangle(state.ball.x, state.ball.y, 0.2, 0.2)
}
init := fn {
running := true
state := State()
state.ball = Ball()
state.ball.x = 10.0
state.ball.y = 10.0
state.ball.vx = 1.0
state.ball.vy = 0.0
state.p1 = Player()
state.p1.paddle = Paddle()
state.p1.paddle.x = 1.
state.p1.paddle.y = 10.
state.p2 = Player()
state.p2.paddle = Paddle()
state.p2.paddle.x = 19.
state.p2.paddle.y = 10.
for i := 0, i == i, i = i + 1 {
update(state)
draw(state)
yield
}
}
init()
|