-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbird.go
56 lines (44 loc) · 933 Bytes
/
bird.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
package main
import (
"fmt"
"strconv"
"sync"
"github.com/veandco/go-sdl2/sdl"
)
type bird struct {
x, y int32
w, h int32
speed float64
gravity float64
dead bool
frames []*sdl.Texture
frame int
mu sync.Mutex
score int
}
func (b *bird) update() {
b.mu.Lock()
defer b.mu.Unlock()
b.y += int32(b.speed)
b.speed += b.gravity
if b.y > windowHeight {
b.dead = true
}
b.frame = (b.frame + 1) % len(b.frames)
}
func (b *bird) draw(r *sdl.Renderer) error {
rect := &sdl.Rect{X: b.x, Y: b.y, W: b.w, H: b.h}
r.Copy(b.frames[b.frame], nil, rect)
rectScore := &sdl.Rect{X: windowWidth / 2, Y: 10 / 2, W: 50, H: 50}
color := sdl.Color{R: 255, G: 100, B: 0, A: 255}
err := drawText(r, strconv.Itoa(b.score), rectScore, color)
if err != nil {
return fmt.Errorf("could not draw score: %v", err)
}
return nil
}
func (b *bird) jump() {
b.mu.Lock()
defer b.mu.Unlock()
b.speed = -10
}