-
Notifications
You must be signed in to change notification settings - Fork 4
/
arkanoid.go
331 lines (289 loc) · 5.54 KB
/
arkanoid.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// based on https://github.com/justinmeister/Arkanoid
package main
import (
"flag"
"os"
"runtime"
"github.com/qeedquan/go-media/math/ga"
"github.com/qeedquan/go-media/sdl"
"github.com/qeedquan/go-media/sdl/sdlgfx"
)
var (
Black = sdl.Color{0, 0, 0, 255}
White = sdl.Color{255, 255, 255, 255}
Cyan = sdl.Color{0, 255, 255, 255}
Yellow = sdl.Color{255, 255, 0, 255}
Blue = sdl.Color{0, 0, 255, 255}
Gray = sdl.Color{50, 50, 50, 255}
)
const (
DisplayWidth = 640
DisplayHeight = 480
BlockGap = 2
BlockWidth = 62
BlockHeight = 25
ArrayWidth = 10
ArrayHeight = 5
PaddleWidth = 100
PaddleHeight = 10
BallRadius = 10
BallSpeed = 4
)
var (
BgColor = Gray
PaddleColor = White
BallColor = Blue
)
func main() {
app := NewApp(NewConfig())
app.Run()
}
func ck(err error) {
if err != nil {
sdl.LogCritical(sdl.LOG_CATEGORY_APPLICATION, "%v", err)
sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", err.Error(), nil)
os.Exit(1)
}
}
func NewConfig() Config {
var conf Config
flag.BoolVar(&conf.Fullscreen, "fullscreen", false, "fullscreen mode")
flag.Parse()
return conf
}
type Config struct {
Fullscreen bool
}
type App struct {
window *sdl.Window
renderer *sdl.Renderer
blocks []Block
paddle sdl.Rect
ball Ball
run bool
fps sdlgfx.FPSManager
}
func NewApp(conf Config) *App {
runtime.LockOSThread()
err := sdl.Init(sdl.INIT_EVERYTHING &^ sdl.INIT_AUDIO)
ck(err)
sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "best")
wflag := sdl.WINDOW_RESIZABLE
if conf.Fullscreen {
wflag |= sdl.WINDOW_FULLSCREEN_DESKTOP
}
window, renderer, err := sdl.CreateWindowAndRenderer(DisplayWidth, DisplayHeight, wflag)
ck(err)
window.SetTitle("Arkanoid")
renderer.SetLogicalSize(DisplayWidth, DisplayHeight)
return &App{
window: window,
renderer: renderer,
}
}
func (c *App) xmouse() int32 {
mx, _, _ := sdl.GetMouseState()
ow, _, _ := c.renderer.OutputSize()
v := c.renderer.Viewport()
return int32(ga.LinearRemap(float64(mx), float64(v.X), float64(ow)-float64(v.X), 0, DisplayWidth))
}
func (c *App) createBall(paddle sdl.Rect) Ball {
return Ball{
X: paddle.X + paddle.W/2 - BallRadius/2,
Y: paddle.Y - BallRadius - 1,
R: BallRadius,
Moving: false,
Vel: sdl.Point{BallSpeed, -BallSpeed},
Speed: 1,
}
}
func (c *App) createPaddle() sdl.Rect {
return sdl.Rect{
c.xmouse() - PaddleWidth/2,
(DisplayHeight - PaddleHeight),
PaddleWidth,
PaddleHeight,
}
}
func (c *App) createBlocks() []Block {
var blocks []Block
for y := 0; y < ArrayHeight; y++ {
for x := 0; x < ArrayWidth; x++ {
color := Cyan
if y%2 == 0 {
color = Yellow
}
blocks = append(blocks, Block{
Rect: sdl.Rect{
int32(x * (BlockWidth + BlockGap)),
int32(y * (BlockHeight + BlockGap)),
BlockWidth,
BlockHeight,
},
Color: color,
})
}
}
return blocks
}
func (c *App) Reset() {
c.paddle = c.createPaddle()
c.blocks = c.createBlocks()
c.ball = c.createBall(c.paddle)
c.fps.Init()
c.fps.SetRate(60)
c.run = true
}
func (c *App) Run() {
c.Reset()
for c.run {
c.event()
c.update()
c.draw()
c.fps.Delay()
}
}
func (c *App) event() {
for {
ev := sdl.PollEvent()
if ev == nil {
break
}
switch ev := ev.(type) {
case sdl.QuitEvent:
c.run = false
case sdl.KeyDownEvent:
switch ev.Sym {
case sdl.K_ESCAPE:
c.run = false
case sdl.K_SPACE:
c.ball.Moving = true
case sdl.K_RETURN:
c.Reset()
}
case sdl.MouseButtonDownEvent:
switch ev.Button {
case sdl.BUTTON_LEFT:
c.ball.Moving = true
case sdl.BUTTON_RIGHT:
c.Reset()
}
case sdl.MouseWheelEvent:
switch b := &c.ball; {
case ev.Y < 0:
if b.Speed > 1 {
b.Speed--
}
case ev.Y > 0:
if b.Speed < 10 {
b.Speed++
}
}
}
}
}
func (c *App) updatePaddle() {
p := &c.paddle
p.X = c.xmouse() - p.W/2
if p.X < 0 {
p.X = 0
} else if p.X+p.W > DisplayWidth {
p.X = DisplayWidth - p.W
}
}
func (c *App) updateBall() {
b := &c.ball
p := &c.paddle
if !b.Moving {
b.X = p.X + p.W/2 - BallRadius/2
return
}
b.X += b.Vel.X * b.Speed
b.Y += b.Vel.Y * b.Speed
hit := false
for i := range c.blocks {
v := &c.blocks[i]
if !v.Dead && intersect(b, &v.Rect) {
v.Dead = true
hit = true
}
}
if intersect(b, p) {
hit = true
b.Y = p.Y - BallRadius
}
if hit {
b.Vel.Y *= -1
}
if b.X+b.R > DisplayWidth {
b.Vel.X *= -1
b.X = DisplayWidth - b.R
} else if b.X-b.R < 0 {
b.Vel.X *= -1
b.X = b.R
}
if b.Y-b.R < 0 {
b.Vel.Y *= -1
b.Y = b.R
}
if b.Y > DisplayHeight || c.won() {
c.Reset()
}
}
func (c *App) won() bool {
for _, b := range c.blocks {
if !b.Dead {
return false
}
}
return true
}
func (c *App) update() {
c.updatePaddle()
c.updateBall()
}
func (c *App) draw() {
re := c.renderer
re.SetDrawColor(Black)
re.Clear()
re.SetDrawColor(BgColor)
re.FillRect(&sdl.Rect{0, 0, DisplayWidth, DisplayHeight})
re.SetDrawColor(PaddleColor)
re.DrawRect(&c.paddle)
for _, b := range c.blocks {
if !b.Dead {
re.SetDrawColor(b.Color)
re.FillRect(&b.Rect)
}
}
b := &c.ball
sdlgfx.FilledCircle(re, int(b.X), int(b.Y), int(b.R), BallColor)
re.Present()
}
type Block struct {
Rect sdl.Rect
Color sdl.Color
Dead bool
}
type Ball struct {
X, Y, R int32
Moving bool
Vel sdl.Point
Speed int32
}
func intersect(b *Ball, r *sdl.Rect) bool {
x := clamp(b.X, r.X, r.X+r.W)
y := clamp(b.Y, r.Y, r.Y+r.H)
dx := b.X - x
dy := b.Y - y
return dx*dx+dy*dy < b.R*b.R
}
func clamp(x, s, e int32) int32 {
if x < s {
x = s
}
if x > e {
x = e
}
return x
}