-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
213 lines (190 loc) · 5.21 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
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"log"
"time"
_ "image/png"
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
type Sprite struct {
Img *ebiten.Image
W int
H int
X float64
Y float64
}
type Asteroid struct {
Sprite
Rot float64
RotSpeed float64
Speed float64
}
type Ship struct {
Sprite
Speed float64
}
type Game struct {
BgImg *ebiten.Image
W int
H int
AsteroidInitialSpeed float64
Asteroid *Asteroid
Ship *Ship
Level int64
FlyBys int64
Score int64
}
func (g *Game) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
g.Ship.X -= g.Ship.Speed
if g.Ship.X < 0 {
g.Ship.X = 0
}
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
g.Ship.X += g.Ship.Speed
if g.Ship.X > float64(g.W-g.Ship.W) {
g.Ship.X = float64(g.W - g.Ship.W)
}
}
if ebiten.IsKeyPressed(ebiten.KeyUp) {
g.Ship.Y -= g.Ship.Speed
if g.Ship.Y < 0 {
g.Ship.Y = 0
}
}
if ebiten.IsKeyPressed(ebiten.KeyDown) {
g.Ship.Y += g.Ship.Speed
if g.Ship.Y > float64(g.H-g.Ship.H) {
g.Ship.Y = float64(g.H - g.Ship.H)
}
}
if g.Asteroid.Y > float64(g.H) {
g.Asteroid.X = float64(rand.Intn(g.W - g.Asteroid.W))
g.Asteroid.Y = float64(-1 * g.Asteroid.H)
g.FlyBys++
g.Score += g.Level * 10
if g.FlyBys >= 10 {
g.Level++
g.Asteroid.Speed = float64(g.Level+1) * .5 * g.AsteroidInitialSpeed
g.FlyBys = 0
}
} else {
g.Asteroid.Y += g.Asteroid.Speed
// log.Printf("X: %f Y: %f\n", g.Asteroid.X, g.Asteroid.Y)
g.Asteroid.Rot += g.Asteroid.RotSpeed
}
if g.GameOver() {
return fmt.Errorf("Game Over! Your score is %d", g.Score)
}
return nil
}
func (g *Game) GameOver() bool {
shipX2 := g.Ship.X + float64(g.Ship.W)
shipY2 := g.Ship.Y + float64(g.Ship.H)
asteroidX2 := g.Asteroid.X + float64(g.Asteroid.W)
asteroidY2 := g.Asteroid.Y + float64(g.Asteroid.H)
overX1 := g.Ship.X >= g.Asteroid.X && g.Ship.X <= asteroidX2
overX2 := shipX2 >= g.Asteroid.X && shipX2 <= asteroidX2
overY1 := g.Ship.Y >= g.Asteroid.Y && g.Ship.Y <= asteroidY2
overY2 := shipY2 >= g.Asteroid.Y && shipY2 <= asteroidY2
log.Printf("%f %f %f %f", g.Ship.X, shipX2, g.Asteroid.X, asteroidX2)
return (overX1 || overX2) && (overY1 || overY2)
}
func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(
float64(g.W)/float64(g.BgImg.Bounds().Dx()),
float64(g.H)/float64(g.BgImg.Bounds().Dy()),
)
screen.DrawImage(g.BgImg, op)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(g.Ship.X, g.Ship.Y)
screen.DrawImage(g.Ship.Img, op)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(-g.Asteroid.W)/2, float64(-g.Asteroid.H)/2)
op.GeoM.Rotate(g.Asteroid.Rot)
op.GeoM.Translate(g.Asteroid.X, g.Asteroid.Y)
screen.DrawImage(g.Asteroid.Img, op)
// Debug infos
debugMsg := fmt.Sprintf("Your score is %d at level %d", g.Score, g.Level)
ebitenutil.DebugPrint(screen, debugMsg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
// log.Printf("Layout %d %d", outsideWidth, outsideHeight)
g.W = outsideWidth
g.H = outsideHeight
return g.W, g.H
}
func NewGame(screenWidth int, screenHeight int) *Game {
rand.Seed(time.Now().UnixNano())
bgImg, _, err := ebitenutil.NewImageFromFile("res/background.png")
if err != nil {
log.Fatalf("could not load background image: %v", err)
}
shipImg, _, err := ebitenutil.NewImageFromFile("res/millennium.png")
if err != nil {
log.Fatalf("could not load millennium image: %v", err)
}
shipW := shipImg.Bounds().Dx()
shipH := shipImg.Bounds().Dy()
ship := &Ship{
Sprite: Sprite{
Img: shipImg,
W: shipW,
H: shipH,
X: float64(screenWidth-shipW) / 2,
Y: float64(screenHeight-shipH) / 2,
},
Speed: 6,
}
origAsteroidImg, _, err := ebitenutil.NewImageFromFile("res/asteroid.png")
if err != nil {
log.Fatalf("could not load asteroid image: %v", err)
}
opAsteroid := &ebiten.DrawImageOptions{}
var asteroidScale float64 = 0.5
opAsteroid.GeoM.Scale(asteroidScale, asteroidScale)
asteroidW := int(float64(origAsteroidImg.Bounds().Dx()) * asteroidScale)
asteroidH := int(float64(origAsteroidImg.Bounds().Dy()) * asteroidScale)
asteroidImg := ebiten.NewImage(asteroidW, asteroidH)
asteroidImg.DrawImage(origAsteroidImg, opAsteroid)
asteroid := &Asteroid{
Sprite: Sprite{
Img: asteroidImg,
W: asteroidW,
H: asteroidH,
X: float64(rand.Intn(screenWidth - asteroidW)),
Y: -1 * float64(asteroidH),
},
Rot: 0,
RotSpeed: .1,
Speed: 1,
}
return &Game{
BgImg: bgImg,
Asteroid: asteroid,
AsteroidInitialSpeed: 6,
Ship: ship,
FlyBys: 0,
Level: 1,
Score: 0,
H: screenHeight,
W: screenWidth,
}
}
func main() {
title := "This my game and it's called Asteroid Field Ship!"
screenWidth := 1000
screenHeight := 800
// ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle(title)
ebiten.SetFullscreen(false)
ebiten.SetWindowResizable(true)
ebiten.SetWindowDecorated(true)
if err := ebiten.RunGame(NewGame(screenWidth, screenHeight)); err != nil {
log.Fatal(err)
}
}