This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (70 loc) · 1.78 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
package main
import (
"flag"
"fmt"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
const windowWidth = 800
const windowHeight = 600
const defaultNumBunnies = 128
const ticksPerSecond = ebiten.DefaultTPS
const delta = float64(1.0) / ticksPerSecond
var bunnyPool []*Bunny
var numBunnies = defaultNumBunnies
var spaceJustPressed = false
func init() {
flag.IntVar(&numBunnies, "num-bunnies", defaultNumBunnies, "number of bunnies on the screen")
flag.Parse()
createBunnies()
}
func main() {
ebiten.SetMaxTPS(ticksPerSecond)
ebiten.SetRunnableInBackground(true)
if err := ebiten.Run(update, windowWidth, windowHeight, 1, "Bunny Mark"); err != nil {
log.Fatal(err)
}
}
func createBunnies() {
missing := numBunnies - len(bunnyPool)
for i := 0; i < missing; i++ {
bunnyPool = append(bunnyPool, NewBunny(windowWidth*0.5, windowHeight*0.5))
}
}
func update(screen *ebiten.Image) error {
if spaceJustPressed && !ebiten.IsKeyPressed(ebiten.KeySpace) {
spaceJustPressed = false
}
if !spaceJustPressed && ebiten.IsKeyPressed(ebiten.KeySpace) {
numBunnies *= 2
createBunnies()
spaceJustPressed = true
}
// update bunny positions
for _, bunny := range bunnyPool {
bunny.Update()
}
if ebiten.IsDrawingSkipped() {
return nil
}
RenderBackground(screen)
// draw bunnies
for _, bunny := range bunnyPool {
bunny.Draw(screen)
}
printDebugLines(screen, []string {
fmt.Sprintf("FPS: %.2f", ebiten.CurrentFPS()),
fmt.Sprintf("TPS: %.2f", ebiten.CurrentTPS()),
fmt.Sprintf("Number of Bunnies: %d", numBunnies),
"Press SPACE to double the number of bunnies!",
})
return nil
}
func printDebugLines(screen *ebiten.Image, lines []string) {
y := 10
for _, line := range lines {
ebitenutil.DebugPrintAt(screen, line, 10, y)
y += 20
}
}