-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.go
More file actions
43 lines (36 loc) · 870 Bytes
/
player.go
File metadata and controls
43 lines (36 loc) · 870 Bytes
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
package goebitencamerademo
import (
"github.com/co0p/ebiten-camera-demo/assets"
"github.com/hajimehoshi/ebiten/v2"
)
const velocity float64 = 4
type Player struct {
Position Position
sprite *ebiten.Image
}
func NewPlayer(x, y float64) Player {
return Player{
Position: Position{X: x, Y: y},
sprite: assets.LoadPlayerSprite(),
}
}
func (p *Player) Draw(screen *ebiten.Image) {
ops := ebiten.DrawImageOptions{}
ops.GeoM.Translate(p.Position.X, p.Position.Y)
screen.DrawImage(p.sprite, &ops)
}
func (p *Player) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
p.Position.X -= velocity
}
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
p.Position.X += velocity
}
if ebiten.IsKeyPressed(ebiten.KeyArrowUp) {
p.Position.Y -= velocity
}
if ebiten.IsKeyPressed(ebiten.KeyArrowDown) {
p.Position.Y += velocity
}
return nil
}