forked from deadprogram/flappy-gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (131 loc) · 3.23 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
package main
import (
rand "math/rand/v2"
"strconv"
"github.com/firefly-zero/firefly-go/firefly"
)
var (
frames = 0 // 経過フレーム数
moveInterval = 5 // 壁の追加間隔
newWallsInterval = 200 // 新しい壁を追加する間隔
wallMovementInterval = 10 // 壁の移動量
wallStartX = 240 // 壁の初期X座標
walls = []*wallData{} // 壁のX座標とY座標
scene = "title"
score = 0
titleFont firefly.Font
gopher *gopherData
wallImage firefly.Image
buttons firefly.Buttons
)
func init() {
firefly.Boot = boot
firefly.Update = update
firefly.Render = render
}
func boot() {
titleFont = firefly.LoadFile("font").Font()
gopher = newGopher()
wallImage = firefly.LoadFile("wall").Image()
}
func update() {
switch scene {
case "title":
buttons = firefly.ReadButtons(firefly.GetMe())
if buttons.N || buttons.S || buttons.E || buttons.W {
scene = "game"
}
case "game":
updateGame()
case "gameover":
buttons = firefly.ReadButtons(firefly.GetMe())
if buttons.N || buttons.S || buttons.E || buttons.W {
scene = "title"
score = 0
frames = 0
gopher.reset()
walls = []*wallData{}
}
}
}
func render() {
switch scene {
case "title":
renderTitle()
case "game":
renderGame()
case "gameover":
renderGameover()
}
}
func renderTitle() {
firefly.ClearScreen(firefly.ColorBlue)
firefly.DrawText("Press any button to Start", titleFont, firefly.Point{X: 100, Y: 100}, firefly.ColorWhite)
}
func renderGame() {
firefly.ClearScreen(firefly.ColorWhite)
firefly.DrawText("Score: "+strconv.Itoa(score), titleFont, firefly.Point{X: 10, Y: 10}, firefly.ColorDarkBlue)
gopher.draw()
for _, wall := range walls {
wall.draw()
}
}
func renderGameover() {
firefly.ClearScreen(firefly.ColorBlue)
gopher.draw()
for _, wall := range walls {
wall.draw()
}
firefly.DrawText("GAME OVER", titleFont, firefly.Point{X: 100, Y: 100}, firefly.ColorWhite)
firefly.DrawText("Score: "+strconv.Itoa(score), titleFont, firefly.Point{X: 100, Y: 120}, firefly.ColorWhite)
}
func updateGame() {
buttons = firefly.ReadButtons(firefly.GetMe())
if buttons.N || buttons.S || buttons.E || buttons.W {
gopher.jump()
}
frames += 1
// add walls
if frames%newWallsInterval == 0 {
wall := &wallData{wallStartX, rand.N(holeYMax)}
walls = append(walls, wall)
}
// current score
l, _, _, _ := gopher.position()
for i, wall := range walls {
if wall.wallX < int(l) {
score = i + 1
}
}
if frames%wallMovementInterval == 0 {
for _, wall := range walls {
wall.move()
}
}
if frames%moveInterval == 0 {
gopher.move()
for _, wall := range walls {
// 上の壁を表す四角形を作る
bLeft, bTop, bRight, bBottom := wall.top()
// 上の壁との当たり判定
if gopher.isHit(bLeft, bTop, bRight, bBottom) {
scene = "gameover"
}
// 下の壁を表す四角形を作る
bLeft, bTop, bRight, bBottom = wall.bottom()
// 下の壁との当たり判定
if gopher.isHit(bLeft, bTop, bRight, bBottom) {
scene = "gameover"
}
_, t, _, b := gopher.position()
if t < 0 {
scene = "gameover"
}
if b > 160 {
scene = "gameover"
}
}
}
}
func main() {
}