-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
73 lines (61 loc) · 1.2 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
package main
import (
"image"
"os"
"time"
_ "image/png"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
type Game struct {
Title string
Win *pixelgl.Window
Ticker *time.Ticker
Pictures []pixel.Picture
Over bool
}
func NewGame(title string, bounds pixel.Rect, resizable, smooth bool, length time.Duration) *Game {
cfg := pixelgl.WindowConfig{
Title: title,
Bounds: bounds,
Resizable: resizable,
VSync: smooth,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
win.SetSmooth(smooth)
g := &Game{
Title: title,
Win: win,
Ticker: time.NewTicker(time.Second / 80),
Over: false,
}
go g.Wait(length)
return g
}
func (g *Game) LoadPicture(path string) (int, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return 0, err
}
g.Pictures = append(g.Pictures, pixel.PictureDataFromImage(img))
return len(g.Pictures) - 1, nil
}
func (g *Game) Open() bool {
return !g.Win.Closed() && !g.Over
}
func (g *Game) Loop() {
g.Win.Update()
<-g.Ticker.C
}
func (g *Game) Wait(length time.Duration) {
time.Sleep(length)
g.Over = true
}