-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (132 loc) · 2.59 KB
/
main.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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"image"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
type Sprite struct {
Img *ebiten.Image
X, Y float64
}
type Player struct {
*Sprite
Health uint
}
type Enemy struct {
*Sprite
FollowsPlayer bool
}
type Game struct {
player *Player
enemies []*Enemy
}
func (g *Game) Update() error {
// reaction to key presses
if ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyD) {
g.player.X += 2
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA) {
g.player.X -= 2
}
if ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyW) {
g.player.Y -= 2
}
if ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyS) {
g.player.Y += 2
}
for _, sprite := range g.enemies {
if !sprite.FollowsPlayer {
continue
}
if sprite.X < g.player.X {
sprite.X += 1
} else if sprite.X > g.player.X {
sprite.X -= 1
}
if sprite.Y > g.player.Y {
sprite.Y -= 1
} else if sprite.Y < g.player.Y {
sprite.Y += 1
}
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{100, 100, 100, 255})
opts := ebiten.DrawImageOptions{}
opts.GeoM.Translate(g.player.X, g.player.Y)
//ebitenutil.DebugPrint(screen, "Hello, World!")
screen.DrawImage(
g.player.Img.SubImage(
image.Rect(0, 0, 16, 16),
).(*ebiten.Image),
&opts,
)
opts.GeoM.Reset()
for _, enemy := range g.enemies {
opts.GeoM.Translate(enemy.X, enemy.Y)
screen.DrawImage(
enemy.Img.SubImage(
image.Rect(0, 0, 16, 16),
).(*ebiten.Image),
&opts,
)
opts.GeoM.Reset()
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 320, 240
}
func main() {
ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("Hello, World!")
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
playerImg, _, err := ebitenutil.NewImageFromFile("assets/images/ninja.png")
if err != nil {
log.Fatal(err)
}
skeletonImg, _, err := ebitenutil.NewImageFromFile("assets/images/skeleton.png")
if err != nil {
log.Fatal(err)
}
if err := ebiten.RunGame(&Game{
player: &Player{
&Sprite{
Img: playerImg,
X: 50,
Y: 50,
},
50,
},
enemies: []*Enemy{
{
&Sprite{
Img: skeletonImg,
X: 100,
Y: 100,
},
true,
},
{
&Sprite{
Img: skeletonImg,
X: 150,
Y: 100,
},
false,
},
{
&Sprite{
Img: skeletonImg,
X: 100,
Y: 150,
},
false,
},
},
}); err != nil {
log.Fatal(err)
}
}