-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (68 loc) · 1.44 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
package main
import (
"github.com/gen2brain/raylib-go/raylib"
"math/rand"
"strconv"
"time"
)
const (
screenWidth = 600
screenHeight = 800
populationSize = 400
brainSize = 400
)
type Game struct {
WindowsShouldClose bool
population Population
goal Goal
walls []Wall
}
func NewGame() (g Game) {
g.Init()
return
}
func (g *Game) Init() {
g.WindowsShouldClose = false
g.walls = make([]Wall, 2)
g.walls[0] = newWall(rl.Vector2{0, 300}, 400, 10)
g.walls[1] = newWall(rl.Vector2{200, 550}, 400, 10)
g.population = newPopulation()
g.goal = newGoal()
}
func (g *Game) Update() {
if rl.WindowShouldClose() {
g.WindowsShouldClose = true
}
if g.population.gen == 0 {
g.population.mutateBabies()
g.population.gen++
} else if g.population.allDotsDead() {
g.population.calculateFitnesses(g.goal)
g.population.naturalSelection(g.goal)
g.population.mutateBabies()
}
g.population.update(g.goal, g.walls)
}
func (g Game) Draw() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
fps := strconv.Itoa(int(rl.GetFPS()))
rl.DrawText("FPS:"+fps, 10, 10, 20, rl.Black)
for i := range g.walls {
g.walls[i].show()
}
g.goal.show()
g.population.show()
rl.EndDrawing()
}
func main() {
rand.Seed(time.Now().UnixNano())
game := NewGame()
rl.InitWindow(screenWidth, screenHeight, "Smart Dot")
rl.SetTargetFPS(60)
for !game.WindowsShouldClose {
game.Update()
game.Draw()
}
rl.CloseWindow()
}