-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipes.go
139 lines (112 loc) · 2.03 KB
/
pipes.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
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
)
type pipes struct {
mu sync.RWMutex
texture *sdl.Texture
r *sdl.Renderer
speed int32
pipes []*pipe
}
func newPipes(r *sdl.Renderer) (*pipes, error) {
var err error
var texture *sdl.Texture
sdl.Do(func() {
texture, err = img.LoadTexture(r, "resources/imgs/pipe.png")
})
if err != nil {
return nil, fmt.Errorf("could not load pipe image: %v", err)
}
ps := &pipes{
texture: texture,
speed: 2,
r: r,
}
go func() {
for {
ps.mu.Lock()
ps.pipes = append(ps.pipes, newPipe())
ps.mu.Unlock()
time.Sleep(1 * time.Second)
}
}()
return ps, nil
}
func (ps *pipes) paint() error {
ps.mu.RLock()
defer ps.mu.RUnlock()
var err error
for _, p := range ps.pipes {
if err = p.paint(ps.r, ps.texture); err != nil {
return err
}
}
return nil
}
func (ps *pipes) restart() {
ps.mu.Lock()
defer ps.mu.Unlock()
ps.pipes = nil
}
func (ps *pipes) update(sc *score) {
ps.mu.Lock()
defer ps.mu.Unlock()
var rem []*pipe
for _, p := range ps.pipes {
p.mu.Lock()
p.x -= ps.speed
p.mu.Unlock()
if p.x+p.w > 0 {
rem = append(rem, p)
} else {
sc.increase()
}
}
ps.pipes = rem
}
func (ps *pipes) destroy() {
ps.mu.Lock()
defer ps.mu.Unlock()
sdl.Do(func() {
ps.texture.Destroy()
})
}
type pipe struct {
mu sync.RWMutex
x int32
h int32
w int32
inverted bool
}
func newPipe() *pipe {
return &pipe{
x: 800,
h: 100 + int32(rand.Intn(300)),
w: 50,
inverted: rand.Float32() > 0.5,
}
}
func (p *pipe) paint(r *sdl.Renderer, texture *sdl.Texture) error {
p.mu.RLock()
defer p.mu.RUnlock()
var err error
rect := &sdl.Rect{X: p.x, Y: 600 - p.h, W: p.w, H: p.h}
flip := sdl.FLIP_NONE
if p.inverted {
rect.Y = 0
flip = sdl.FLIP_VERTICAL
}
sdl.Do(func() {
err = r.CopyEx(texture, nil, rect, 0, nil, flip)
})
if err != nil {
return fmt.Errorf("could not copy pipe: %v", err)
}
return nil
}