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
|
package main
import (
"image/color"
_ "image/png"
"log"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)
const screen_w, screen_h = 640, 480
type Penguin struct {
img *ebiten.Image
x, y float64
xv, yv float64
}
func newPenguin() Penguin {
penguin := Penguin{
x: screen_w / 2,
y: 0,
xv: 0,
yv: 0,
}
var err error
penguin.img, _, err = ebitenutil.NewImageFromFile("assets/penguin.png")
if err != nil {
log.Fatal(err)
}
return penguin;
}
func (penguin *Penguin) draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(penguin.x, penguin.y)
screen.DrawImage(penguin.img, op)
}
func (penguin *Penguin) drawBounds(screen *ebiten.Image) {
vector.StrokeRect(screen,
float32(penguin.x), float32(penguin.y),
float32(penguin.img.Bounds().Dx()), float32(penguin.img.Bounds().Dy()),
4, color.RGBA{0xcc, 0x66, 0x66, 0xff}, true)
}
func (penguin *Penguin) update(g *Game) {
if penguin.collideWithCurve(g.ground, penguin.xv, 0){
penguin.xv = 0
penguin.yv -= 4
} else {
penguin.x += penguin.xv
penguin.xv *= 0.9
}
if penguin.collideWithCurve(g.ground, 0, penguin.yv){
penguin.yv = 0
} else {
penguin.y += penguin.yv
penguin.yv += 0.9
}
}
func (p *Penguin) collideWithCurve(curve Curve, xv, yv float64) bool {
cy1 := curve(p.x + xv)
y1 := p.y + yv
y2 := p.y + yv + float64(p.img.Bounds().Dy())
if y1 < cy1 && cy1 < y2 {
return true
}
cy2 := curve(p.x + xv + float64(p.img.Bounds().Dx()))
if y1 < cy2 && cy2 < y2 {
return true
}
return false
}
func (g *Game) init() {
g.penguin = newPenguin()
}
type Curve func(x float64)(y float64)
func (curve Curve) draw(dst *ebiten.Image, clr color.Color, width float32, resolution int, antialias bool) {
for x1 := 0; x1 < screen_w; x1 += resolution {
x2 := x1 + resolution
y1 := curve(float64(x1))
y2 := curve(float64(x2))
vector.StrokeLine(dst, float32(x1), float32(y1), float32(x2), float32(y2), width, color.RGBA{0x19, 0x19, 0x19, 0xff}, true)
}
}
type Game struct{
penguin Penguin
ground Curve
}
func (g *Game) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
g.penguin.xv = 4
}
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
g.penguin.xv = -4
}
if ebiten.IsKeyPressed(ebiten.KeyArrowUp) {
g.penguin.yv = -4
}
if ebiten.IsKeyPressed(ebiten.KeyArrowDown) {
g.penguin.yv = 4
}
g.penguin.update(g)
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{0xfe, 0xfe, 0xfe, 0xff})
g.ground.draw(screen, color.RGBA{0x19, 0x19, 0x19, 0xff}, 4, 16, true);
g.penguin.draw(screen)
g.penguin.drawBounds(screen)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 640, 480
}
func main() {
ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("penguin")
game := Game{}
game.ground = func(x float64)(y float64) {
return 200-math.Pow((x - 320)/320, 3)+math.Pow((x-320), 2)/300
}
game.init()
if err := ebiten.RunGame(&game); err != nil {
log.Fatal(err)
}
}
|