-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
58 lines (41 loc) · 938 Bytes
/
player.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
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
const playerSpeed = 1
type player struct {
tex *sdl.Texture
x, y float64
}
func NewPlayer(renderer *sdl.Renderer) (p player, err error) {
img, err := sdl.LoadBMP("assets/player.bmp")
if err != nil {
fmt.Println("player load", err)
return player{}, err
}
defer img.Free()
p.tex, err = renderer.CreateTextureFromSurface(img)
if err != nil {
fmt.Println("player texture", err)
return player{}, nil
}
p.x = windowWidth / 2.0
p.y = windowHeight - 20
return p, nil
}
func (p *player) draw(renderer *sdl.Renderer) {
renderer.Copy(p.tex,
&sdl.Rect{X: 0, Y: 0, W: 20, H: 20},
&sdl.Rect{X: int32(p.x), Y: int32(p.y), W: 20, H: 20})
}
func (p *player) update() {
keys := sdl.GetKeyboardState()
if keys[sdl.SCANCODE_LEFT] == 1 {
// left
p.x -= playerSpeed
} else if keys[sdl.SCANCODE_RIGHT] == 1 {
// right
p.x += playerSpeed
}
}