-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipe.go
52 lines (44 loc) · 1.27 KB
/
pipe.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
package main
import (
_ "image/png"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"image"
"math"
"math/rand"
)
type Pipe struct {
longitude float64
top_height float64
bot_pipe *ebiten.Image
visible bool
}
func (pipe *Pipe) reset() {
left_limit := float64(SCREEN_HEIGHT - PIPE_HEIGHT - GAP)
pipe.top_height = rand.Float64()*(PIPE_HEIGHT - left_limit) + left_limit
pipe.longitude = SCREEN_WIDTH + PIPE_WIDTH
pipe.visible = false
}
func new_pipe() *Pipe {
pipe := Pipe{}
pipe.bot_pipe, _, _ = ebitenutil.NewImageFromFile("images/pipe-red.png", ebiten.FilterDefault)
pipe.reset()
return &pipe
}
func (pipe *Pipe) draw(screen *ebiten.Image) {
// Top pipe
op := &ebiten.DrawImageOptions{}
op.GeoM.Rotate(math.Pi)
op.GeoM.Translate(pipe.longitude + PIPE_WIDTH, pipe.top_height)
screen.DrawImage(pipe.bot_pipe, op)
// Bot pipe
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(pipe.longitude, pipe.top_height + GAP)
bot_pipe_img := pipe.bot_pipe.SubImage(image.Rect(0, 0, PIPE_WIDTH, int(BG_HEIGHT - pipe.top_height - GAP + 3))).(*ebiten.Image)
screen.DrawImage(bot_pipe_img, op)
}
func (pipe *Pipe) move(){
if pipe.visible {
pipe.longitude -= VELOCITY
}
}