-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.go
317 lines (249 loc) · 5.4 KB
/
scene.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
package main
import (
"fmt"
"log"
"math/rand"
"sync"
"github.com/veandco/go-sdl2/sdl"
img "github.com/veandco/go-sdl2/sdl_image"
ttf "github.com/veandco/go-sdl2/sdl_ttf"
)
const (
numberOfPipes = 100
pipesSpeed = 10
birdInitX = 20
)
// ================== SCENE ================== //
type scene struct {
renderer *sdl.Renderer
bg *sdl.Texture
bgx int32
bird bird
pipes pipes
font *ttf.Font
}
// NewScene creates new scene
func NewScene(r *sdl.Renderer) (*scene, error) {
s := &scene{renderer: r}
var err error
s.font, err = ttf.OpenFont("resources/font.ttf", 32)
if err != nil {
return s, fmt.Errorf("Error while opening font: %v", err)
}
s.bg, err = img.LoadTexture(s.renderer, "resources/bg.png")
if err != nil {
return s, fmt.Errorf("Error while loading bg: %v", err)
}
s.bird = bird{x: birdInitX, y: windowHeight / 2, w: 50, h: 50, gravity: 5}
for i := 1; i <= 4; i++ {
t, err := img.LoadTexture(s.renderer, fmt.Sprintf("resources/bird/frame-%d.png", i))
if err != nil {
return s, fmt.Errorf("Error while loading bird texturne #%d: %v", i, err)
}
s.bird.frames = append(s.bird.frames, t)
}
s.pipes = pipes{}
s.pipes.tex, err = img.LoadTexture(s.renderer, "resources/pipe.png")
if err != nil {
return s, fmt.Errorf("Error while loading pipe texture %v", err)
}
s.resetPipes()
return s, nil
}
func (s *scene) resetPipes() {
s.pipes.pipes = s.generatePipes()
}
func (s *scene) generatePipes() []*pipe {
var pipes []*pipe
var w int32 = 52
gapW := w * 5
for i := 0; i < numberOfPipes; i++ {
limit := int32(windowHeight/2) - 50
bottomPipeHeight := rand.Int31n(limit)
topPipeHeight := rand.Int31n(limit)
pos := gapW*int32(i+2) + rand.Int31n(w*4) + w/2
if rand.Intn(10) > 4 {
pos += w
} else {
pos -= w
}
pipes = append(pipes,
&pipe{
pos: pos,
w: w,
h: bottomPipeHeight,
up: false,
},
&pipe{
pos: pos,
w: w,
h: topPipeHeight,
up: true,
})
}
return pipes
}
func (s *scene) restart() {
s.score()
s.bird.dead = false
s.bird.x = birdInitX
s.bird.y = windowHeight / 2
s.bgx = 0
s.resetPipes()
s.draw()
sdl.Delay(500)
}
func (s *scene) run(fps uint32) {
for {
s.update()
s.draw()
sdl.Delay(uint32(1000 / fps))
}
}
func (s *scene) update() {
s.bird.update()
s.pipes.update()
s.bgx = (s.bgx + 1) % 2000
if s.bgx >= windowWidth {
s.bgx = 0
}
if s.pipes.hits(&s.bird) {
s.bird.dead = true
}
if s.bird.dead {
s.restart()
}
}
func (s *scene) draw() {
s.renderer.Clear()
s.drawBg()
s.bird.draw(s.renderer)
s.pipes.draw(s.renderer)
s.drawScore()
s.renderer.Present()
}
func (s *scene) drawScore() {
score := int32(s.score() / 2)
scoreText := fmt.Sprintf("%d", score)
surf, err := s.font.RenderUTF8_Solid(scoreText, sdl.Color{R: 255, G: 255, B: 255, A: 255})
var width int32 = 30
if score >= 10 {
width += 30
}
if score >= 100 {
width += 30
}
if err != nil {
log.Fatalf("Error while rendering score: %v", err)
}
texture, err := s.renderer.CreateTextureFromSurface(surf)
defer texture.Destroy()
if err != nil {
log.Fatalf("Could not create texture from surface: %v", err)
}
s.renderer.Copy(texture, nil, &sdl.Rect{X: 15, Y: 15, W: width, H: 60})
}
func (s *scene) drawBg() {
x1 := -s.bgx
x2 := windowWidth - s.bgx
srcRect := sdl.Rect{X: 0, Y: 0, W: windowWidth, H: windowHeight}
destRect1 := sdl.Rect{X: x1, Y: 0, W: windowWidth, H: windowHeight}
destRect2 := sdl.Rect{X: x2, Y: 0, W: windowWidth, H: windowHeight}
s.renderer.Copy(s.bg, &srcRect, &destRect1)
s.renderer.Copy(s.bg, &srcRect, &destRect2)
}
func (s *scene) score() int {
return s.pipes.score()
}
// ================== BIRD ================== //
type bird struct {
x, y, w, h int32
speed, gravity int32
frames []*sdl.Texture
frame int
mu sync.Mutex
dead bool
}
func (b *bird) update() {
f := b.frame + 1
if f > 3 {
b.frame = 0
} else {
b.frame = f
}
b.mu.Lock()
defer b.mu.Unlock()
b.y += b.speed
b.speed += b.gravity
if b.y > windowHeight || b.y < 0 {
b.dead = true
}
}
func (b *bird) draw(r *sdl.Renderer) {
r.Copy(b.frames[b.frame], nil, &sdl.Rect{X: b.x, Y: b.y, W: b.w, H: b.h})
}
func (b *bird) jump() {
b.mu.Lock()
defer b.mu.Unlock()
b.speed = -20
}
// ================== PIPES ================== //
type pipes struct {
pipes []*pipe
tex *sdl.Texture
}
func (pp *pipes) update() {
for _, p := range pp.pipes {
p.update()
}
}
func (pp *pipes) draw(r *sdl.Renderer) {
for _, p := range pp.pipes {
p.draw(r, pp.tex)
}
}
func (pp *pipes) hits(b *bird) bool {
for _, p := range pp.pipes {
hits := p.hits(b)
if hits {
return true
}
}
return false
}
func (pp *pipes) score() int {
score := 0
for _, p := range pp.pipes {
if p.pos < birdInitX {
score++
}
}
return score
}
// ================== PIPE ================== //
type pipe struct {
w, h, pos int32
up bool
}
func (p *pipe) update() {
p.pos -= pipesSpeed
}
func (p *pipe) draw(r *sdl.Renderer, tex *sdl.Texture) {
dest := &sdl.Rect{X: p.pos, Y: windowHeight - p.h, W: p.w, H: p.h}
src := &sdl.Rect{X: 0, Y: 0, W: p.w, H: p.h}
flip := sdl.FLIP_NONE
if !p.up {
dest.Y = 0
flip = sdl.FLIP_VERTICAL
}
r.CopyEx(tex, src, dest, 0, nil, flip)
}
func (p *pipe) hits(b *bird) bool {
if b.x+b.w <= p.pos || b.x >= p.pos+p.w {
return false
}
if !p.up {
return b.y <= p.h
}
return b.y+b.h >= windowHeight-p.h
}