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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
package main
import (
_ "embed"
"github.com/gen2brain/raylib-go/raylib"
"math"
"strconv"
)
const (
screenWidth = 1920
screenHeight = 1080
ballSpeed = 5
paddleSpeed = 5
)
func printCenter(text string, size int32, color rl.Color) {
rl.DrawText(text, screenWidth/2-rl.MeasureText(text, size), screenHeight/2, size, color)
}
type rect rl.Rectangle
func (r rect) bottom() float32 {
return r.Y + r.Height
}
func (r rect) right() float32 {
return r.X + r.Width
}
func (r rect) collides(s rect) bool {
return rl.CheckCollisionRecs(rl.Rectangle(r), rl.Rectangle(s))
}
func (r rect) draw(color rl.Color) {
rl.DrawRectangleRec(rl.Rectangle(r), color)
}
type ball struct {
pos rl.Vector2
velocity rl.Vector2
}
func (b *ball) bounds() rect {
const ballSize = 30
return rect{
X: b.pos.X,
Y: b.pos.Y,
Width: ballSize,
Height: ballSize,
}
}
func (b *ball) predictIntersection(targetX float32) (float32, bool) {
if b.velocity.X <= 0 {
return 0, false
}
x := b.pos.X
y := b.pos.Y
velocity := b.velocity
dist := targetX - x
prediction := y + velocity.Y*dist/velocity.X
if prediction < 0 {
bounces := int(-prediction / screenHeight)
if bounces%2 == 0 {
return -prediction - float32(screenHeight*bounces), true
}
return prediction + float32(screenHeight*(bounces+1)), true
}
if prediction > screenHeight {
bounces := int(prediction / screenHeight)
if bounces%2 == 0 {
return prediction - float32(screenHeight*bounces), true
}
return -prediction + float32(screenHeight*(bounces+1)), true
}
return prediction, true
}
type side int
const (
left side = iota
right
)
func (b *ball) reset(side side) {
b.pos = rl.Vector2{X: screenWidth / 2, Y: screenHeight / 2}
b.velocity = rl.Vector2{X: ballSpeed}
if side == left {
b.velocity.X = -b.velocity.X
}
}
type paddle struct {
pos float32
side side
}
func (p *paddle) bounds() rect {
const (
paddleHeight = 200
thickness = 10
padding = 100
)
r := rect{
X: padding - thickness/2,
Y: p.pos - paddleHeight/2,
Width: thickness,
Height: paddleHeight,
}
if p.side == right {
r.X = screenWidth - padding - thickness/2
}
return r
}
//go:embed ping.mp3
var pingMP3 []byte
func main() {
rl.InitWindow(screenWidth, screenHeight, "pongo")
defer rl.CloseWindow()
rl.SetTargetFPS(120)
rl.InitAudioDevice()
defer rl.CloseAudioDevice()
pingWave := rl.LoadWaveFromMemory(".mp3", pingMP3, int32(len(pingMP3)))
defer rl.UnloadWave(pingWave)
ping := rl.LoadSoundFromWave(pingWave)
defer rl.UnloadSound(ping)
playerScore := 0
aiScore := 0
var ball ball
ball.reset(left)
paddle1 := paddle{pos: screenHeight / 2, side: left}
paddle2 := paddle{pos: screenHeight / 2, side: right}
for !rl.WindowShouldClose() {
switch {
case rl.IsKeyDown(rl.KeyUp) && paddle1.bounds().Y >= 0:
paddle1.pos -= paddleSpeed
case rl.IsKeyDown(rl.KeyDown) && paddle1.bounds().bottom() <= screenHeight:
paddle1.pos += paddleSpeed
}
if prediction, ok := ball.predictIntersection(paddle2.bounds().X); ok {
switch {
case paddle2.pos < prediction && paddle2.bounds().bottom() <= screenHeight:
paddle2.pos += paddleSpeed
case paddle2.pos > prediction && paddle2.bounds().Y >= 0:
paddle2.pos -= paddleSpeed
}
}
ball.pos = rl.Vector2Add(ball.pos, ball.velocity)
switch {
case ball.bounds().collides(paddle1.bounds()):
rl.PlaySound(ping)
d := ball.pos.Y - paddle1.pos
if d < 0 {
d = -d
}
angle := d / (paddle1.bounds().Height/2 + ball.bounds().Height) * (75 * 2 * math.Pi / 360)
if ball.pos.Y < paddle1.pos {
angle = -angle
}
speed := ballSpeed * (1 + 4*d/(paddle1.bounds().Height/2+ball.bounds().Height))
ball.velocity = rl.Vector2{X: speed * float32(math.Cos(float64(angle))), Y: speed * float32(math.Sin(float64(angle)))}
case ball.bounds().collides(paddle2.bounds()):
rl.PlaySound(ping)
d := ball.pos.Y - paddle2.pos
if d < 0 {
d = -d
}
angle := d / (paddle2.bounds().Height/2 + ball.bounds().Height) * (75 * 2 * math.Pi / 360)
if ball.pos.Y < paddle2.pos {
angle = -angle
}
angle += math.Pi
speed := ballSpeed * (1 + 4*d/(paddle2.bounds().Height/2+ball.bounds().Height))
ball.velocity = rl.Vector2{X: speed * float32(math.Cos(float64(angle))), Y: speed * float32(math.Sin(float64(angle)))}
case ball.bounds().right() <= paddle1.bounds().X:
aiScore++
ball.reset(right)
case ball.bounds().X >= paddle2.bounds().X:
playerScore++
ball.reset(left)
case ball.bounds().Y <= 0 || ball.bounds().bottom() >= screenHeight:
ball.velocity.Y = -ball.velocity.Y
rl.PlaySound(ping)
}
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
rl.DrawLine(screenWidth/2, 0, screenWidth/2, screenHeight, rl.Gray)
paddle1.bounds().draw(rl.RayWhite)
paddle2.bounds().draw(rl.RayWhite)
ball.bounds().draw(rl.RayWhite)
const fontSize = 120
rl.DrawText(strconv.Itoa(playerScore), int32(paddle1.bounds().X)+20, 30, fontSize, rl.RayWhite)
rl.DrawText(strconv.Itoa(aiScore), int32(paddle2.bounds().X)-rl.MeasureText("9999", fontSize), 30, fontSize, rl.RayWhite)
rl.EndDrawing()
}
}
|