-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
134 lines (118 loc) · 3.06 KB
/
game.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
package main
import (
_ "image/png"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
)
type Game struct {
background *Background
pipes [PIPE_NUM]*Pipe
bird *Bird
score *Score
first_pipe_idx int
last_pipe_idx int
mode string
game_over_sign *ebiten.Image
}
func new_game() *Game {
game := Game{}
game.mode = "waiting"
game.score = initiate_scores()
game.background = new_background()
for a := 0; a < PIPE_NUM; a++ {
game.pipes[a] = new_pipe()
}
game.bird = new_bird()
game.game_over_sign, _, _ = ebitenutil.NewImageFromFile("images/gameover.png", ebiten.FilterDefault)
game.reset()
return &game
}
func(game *Game) start_game() {
game.mode = "on"
game.bird.active = true
game.release_new_pipe()
}
func (game *Game) Update(screen *ebiten.Image) error {
game.switch_mode()
game.bird.update()
switch game.mode {
case "waiting":
game.background.move()
case "on":
game.background.move()
for _, pipe := range game.pipes {
pipe.move()
}
if game.pipes[game.last_pipe_idx].longitude <= SCREEN_WIDTH - PIPE_WIDTH - DISTANCE {
game.release_new_pipe()
}
if game.first_pipe().longitude < -PIPE_WIDTH {
game.reset_first_pipe()
game.score.points += 1
}
if game.bird.touch_pipe(game.first_pipe()) {
game.mode = "over"
game.bird.die()
}
case "over":
game.bird.drop()
}
return nil
}
func (game *Game) switch_mode() {
if (inpututil.IsKeyJustPressed(ebiten.KeySpace)) {
switch game.mode {
case "waiting":
game.start_game()
case "on":
game.bird.jump()
case "over":
game.reset()
}
}
}
func (game *Game) Draw(screen *ebiten.Image) {
game.background.draw(screen)
for _, pipe := range game.pipes {
pipe.draw(screen)
}
game.bird.draw(screen)
switch game.mode {
case "on":
game.score.draw_score(screen, float64(SCREEN_WIDTH)/2, float64(SCREEN_HEIGHT)/3)
case "over":
draw_in_center(game.game_over_sign, screen)
}
}
func (game *Game) reset() {
for a := 0; a < PIPE_NUM; a++ {
game.pipes[a].reset()
}
game.first_pipe_idx = 0
game.last_pipe_idx = PIPE_NUM - 1
game.mode = "waiting"
game.bird.reset()
game.score.reset()
}
func (game *Game) check_input() {
}
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return SCREEN_WIDTH, SCREEN_HEIGHT
}
func (game *Game) release_new_pipe() {
game.last_pipe_idx += 1
game.last_pipe_idx %= PIPE_NUM
game.last_pipe().visible = true
}
func (game *Game) reset_first_pipe() {
game.first_pipe().reset()
game.first_pipe_idx += 1
game.first_pipe_idx %= PIPE_NUM
}
func (game *Game) first_pipe() *Pipe {
return game.pipes[game.first_pipe_idx]
}
func (game *Game) last_pipe() *Pipe {
return game.pipes[game.last_pipe_idx]
}