-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplants.go
66 lines (55 loc) · 1.12 KB
/
plants.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
package main
import (
"math/rand"
"time"
"github.com/gotk3/gotk3/cairo"
"github.com/gotk3/gotk3/gtk"
)
type Actor interface {
act(*Grid) bool
getX() int
setX(int)
getY() int
setY(int)
draw(float64, *cairo.Context)
setEnergy(int)
getEnergy() int
}
func main() {
gtk.Init(nil)
// Handle constructing the plants and grid
rand.Seed(time.Now().Unix())
var pGrid Grid
for i := 0; i < 10; i++ {
var plant Plant
plant.create()
pGrid.append(&plant, plant.x, plant.y)
}
// gui boilerplate
win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
da, _ := gtk.DrawingAreaNew()
win.Add(da)
win.SetTitle("Random Squares")
win.Connect("destroy", gtk.MainQuit)
win.SetPosition(gtk.WIN_POS_CENTER)
win.SetDefaultSize(640, 480)
win.ShowAll()
// Data
unitSize := 10.0
// Event handlers
da.Connect("draw", func(da *gtk.DrawingArea, cr *cairo.Context) {
// Clear the background to one color
cr.SetSourceRGB(.1, .1, .1)
cr.Paint()
// Plant Loop
actors := pGrid.actors()
for i := range actors {
actors[i].act(&pGrid)
}
for _, v := range actors {
v.draw(unitSize, cr)
}
win.QueueDraw()
})
gtk.Main()
}