forked from 529124368/golang_ebiten_game_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
200 lines (186 loc) · 4.54 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
_ "image/png"
"log"
"strconv"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
IDLE int = 0
RUN int = 1
ATTACK int = 2
screenWidth int = 320
screenHeight int = 240
offsetX int = 150
offsetY int = 80
)
//Game
type Game struct {
count int
player *Player
}
//Player
type Player struct {
x float64
y float64
state int
direction int
}
var counts int = 0
var frameNums int = 4
var imgs_idel0, imgs_idel1, imgs_idel2, imgs_idel3, imgs_run0, imgs_run1, imgs_run2, imgs_run3, imgNow map[int]*ebiten.Image
var op, opBg *ebiten.DrawImageOptions
//BG
var bgImage *ebiten.Image
//factory
func NewGame() *Game {
gameStart := &Game{
count: 0,
player: &Player{
x: 0,
y: 0,
state: IDLE,
direction: 0,
},
}
return gameStart
}
func init() {
//get images
imgNow = make(map[int]*ebiten.Image)
//idle
imgs_idel0 = make(map[int]*ebiten.Image)
imgs_idel1 = make(map[int]*ebiten.Image)
imgs_idel2 = make(map[int]*ebiten.Image)
imgs_idel3 = make(map[int]*ebiten.Image)
//run
imgs_run0 = make(map[int]*ebiten.Image)
imgs_run1 = make(map[int]*ebiten.Image)
imgs_run2 = make(map[int]*ebiten.Image)
imgs_run3 = make(map[int]*ebiten.Image)
for i := 0; i < 4; i++ {
mg, _, err := ebitenutil.NewImageFromFile("resource/idle0/" + strconv.Itoa(i) + ".png")
mg1, _, err := ebitenutil.NewImageFromFile("resource/idle1/" + strconv.Itoa(i) + ".png")
mg2, _, err := ebitenutil.NewImageFromFile("resource/idle2/" + strconv.Itoa(i) + ".png")
mg3, _, err := ebitenutil.NewImageFromFile("resource/idle3/" + strconv.Itoa(i) + ".png")
if err != nil {
log.Fatal(err)
}
imgs_idel0[i] = mg
imgs_idel1[i] = mg1
imgs_idel2[i] = mg2
imgs_idel3[i] = mg3
}
for i := 0; i < 6; i++ {
img, _, err := ebitenutil.NewImageFromFile("resource/run0/" + strconv.Itoa(i) + ".png")
img1, _, err := ebitenutil.NewImageFromFile("resource/run1/" + strconv.Itoa(i) + ".png")
img2, _, err := ebitenutil.NewImageFromFile("resource/run2/" + strconv.Itoa(i) + ".png")
img3, _, err := ebitenutil.NewImageFromFile("resource/run3/" + strconv.Itoa(i) + ".png")
if err != nil {
log.Fatal(err)
}
imgs_run0[i] = img
imgs_run1[i] = img1
imgs_run2[i] = img2
imgs_run3[i] = img3
}
//BG
img, _, err := ebitenutil.NewImageFromFile("resource/bg/bg1.png")
if err != nil {
log.Fatal(err)
}
bgImage = img
opBg = &ebiten.DrawImageOptions{}
opBg.Filter = ebiten.FilterLinear
opBg.GeoM.Translate(-700, -550)
opBg.GeoM.Scale(0.5, 0.5)
//image option
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(screenWidth/2+offsetX), float64(screenHeight/2+offsetY))
op.GeoM.Scale(0.4, 0.4)
op.Filter = ebiten.FilterLinear
//copy
MapCopy(imgs_run0, imgNow)
}
func MapCopy(a, b map[int]*ebiten.Image) {
for k, v := range a {
b[k] = v
}
}
func (g *Game) Update() error {
g.count++
g.player.state = IDLE
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
g.player.state = RUN
g.player.direction = 3
opBg.GeoM.Translate(g.player.x+1, 0)
MapCopy(imgs_run3, imgNow)
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
g.player.state = RUN
g.player.direction = 1
opBg.GeoM.Translate(g.player.x-1, 0)
MapCopy(imgs_run1, imgNow)
}
if ebiten.IsKeyPressed(ebiten.KeyDown) {
g.player.state = RUN
g.player.direction = 2
opBg.GeoM.Translate(0, g.player.y-1)
MapCopy(imgs_run2, imgNow)
}
if ebiten.IsKeyPressed(ebiten.KeyUp) {
g.player.state = RUN
g.player.direction = 0
opBg.GeoM.Translate(0, g.player.y+1)
MapCopy(imgs_run0, imgNow)
}
if g.player.state == IDLE {
frameNums = 4
if g.player.direction == 0 {
MapCopy(imgs_idel0, imgNow)
}
if g.player.direction == 1 {
MapCopy(imgs_idel1, imgNow)
}
if g.player.direction == 2 {
MapCopy(imgs_idel2, imgNow)
}
if g.player.direction == 3 {
MapCopy(imgs_idel3, imgNow)
}
} else {
frameNums = 6
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
defer func() {
if r := recover(); r != nil {
fmt.Println("#################", r)
}
}()
//draw bg
screen.DrawImage(bgImage, opBg)
//draw images
screen.DrawImage(imgNow[counts], op)
if g.count > frameNums {
counts++
g.count = 0
if counts >= frameNums {
counts = 0
}
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 320, 240
}
func main() {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
//ebiten.SetFullscreen(true)
ebiten.SetWindowTitle("golang game test")
if err := ebiten.RunGame(NewGame()); err != nil {
log.Fatal(err)
}
}