-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
119 lines (101 loc) · 2.3 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
package main
import (
"fmt"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
screenWidth = 480
screenHeight = 640
)
type Game struct {
touchIDs []ebiten.TouchID
}
var (
fruits = []*Fruit{}
world = World{X: 90, Y: 100, Width: 300, Height: 540}
dropper = NewDropper(world)
calc = &Calc{World: world}
draw = &Draw{}
isKeyPressed = false
)
func (g *Game) leftTouched() bool {
for _, id := range g.touchIDs {
x, y := ebiten.TouchPosition(id)
if y >= screenHeight/2 {
return false
}
if x < screenWidth/2 {
return true
}
}
return false
}
func (g *Game) rightTouched() bool {
for _, id := range g.touchIDs {
x, y := ebiten.TouchPosition(id)
if y >= screenHeight/2 {
return false
}
if x >= screenWidth/2 {
return true
}
}
return false
}
func (g *Game) bottomTouched() bool {
for _, id := range g.touchIDs {
_, y := ebiten.TouchPosition(id)
if y >= screenHeight/2 {
return true
}
}
return false
}
func (g *Game) Update() error {
fruits = calc.Fruits(fruits)
g.touchIDs = ebiten.AppendTouchIDs(g.touchIDs[:0])
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) || g.leftTouched() {
dropper.MoveLeft()
}
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) || g.rightTouched() {
dropper.MoveRight()
}
if ebiten.IsKeyPressed(ebiten.KeySpace) || g.bottomTouched() {
isKeyPressed = true
} else if isKeyPressed {
isKeyPressed = false
if next := dropper.Drop(); next != nil {
fruits = append(fruits, next)
}
}
dropper.Tick()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
draw.World(screen, world)
if next := dropper.Next(); next != nil {
draw.Fruit(screen, world, next)
}
draw.Fruits(screen, world, fruits)
msg := fmt.Sprintf(
"PC:\n <- key: move left\n -> key: move right\n spacebar: drop fruit\n"+
"Touch Devices:\n left: move left\n right: move right\n bottom: drop fruit\n"+
"HI-SCORE: %d\nSCORE: %d\nFPS: %0.2f",
calc.HiScore,
calc.Score,
ebiten.ActualFPS(),
)
ebitenutil.DebugPrint(screen, msg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("suika-game-go")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}