Skip to content

Commit cb779e3

Browse files
Fixes on scene IA HARD and persisting training data
1 parent 222a0d3 commit cb779e3

File tree

11 files changed

+312
-204
lines changed

11 files changed

+312
-204
lines changed

components/bird.go

+47-67
Original file line numberDiff line numberDiff line change
@@ -16,113 +16,93 @@ const BirdWidth = 54
1616

1717
// BirdX is the inicial position X of the bird
1818
const BirdX = WindowWidth/2 - WindowWidth*0.2
19-
const gravity = 20.0
2019

21-
var maxSpeed = gravity / 4
20+
const ghostCountdown = 1300
21+
22+
var gravity = 20.0
23+
var maxSpeed = gravity * 30
2224

2325
// Bird component
2426
type Bird struct {
25-
x float64
26-
y float64
27-
speed float64
28-
sprite *pixel.Sprite
29-
points int64
30-
death bool
31-
enableGhost bool
32-
ghost bool
27+
X float64
28+
Y float64
29+
speed float64
30+
sprite *pixel.Sprite
31+
Points int64
32+
Dead bool
33+
Ghost bool
34+
GhostCountdown int
3335
}
3436

3537
// NewBird creates a new bird component
3638
func NewBird(x float64, sprite *pixel.Sprite) *Bird {
3739
return &Bird{
38-
x: x,
39-
y: WindowHeight / 2,
40-
speed: 2,
41-
sprite: sprite,
42-
points: 0,
43-
death: false,
44-
enableGhost: true,
45-
ghost: false,
40+
X: x,
41+
Y: WindowHeight / 2,
42+
speed: 0,
43+
sprite: sprite,
44+
Points: 0,
45+
Dead: false,
46+
Ghost: false,
47+
GhostCountdown: ghostCountdown,
4648
}
4749
}
4850

49-
func (b *Bird) GetX() float64 {
50-
return b.x
51-
}
52-
53-
func (b *Bird) GetY() float64 {
54-
return b.y
55-
}
56-
57-
func (b *Bird) GetPoints() int64 {
58-
return b.points
59-
}
60-
61-
func (b *Bird) IsDeath() bool {
62-
return b.death
63-
}
64-
65-
func (b *Bird) Death() {
66-
b.death = true
51+
// Kill set bird as dead
52+
func (b *Bird) Kill() {
53+
b.Dead = true
6754
}
6855

56+
// Update run all operations on bird
6957
func (b *Bird) Update() {
70-
if b.speed < maxSpeed {
71-
b.speed += gravity * Delta
58+
if b.GhostCountdown > 0 {
59+
b.GhostCountdown--
7260
}
7361

74-
if b.death == true {
75-
b.x -= XSpeed * Delta
76-
77-
if b.y > 80 {
78-
b.y -= b.speed
79-
}
62+
if b.speed < maxSpeed {
63+
b.speed += gravity
64+
}
8065

81-
return
66+
if b.Dead {
67+
b.X -= GameXSpeed * Delta
8268
}
8369

84-
b.y -= b.speed
70+
if b.Y > 80 || b.speed < 0 {
71+
b.Y -= b.speed * Delta
72+
}
8573
}
8674

75+
// Draw the bird on window
8776
func (b *Bird) Draw(win *pixelgl.Window) {
88-
mat := pixel.IM.Rotated(pixel.V(0, 0), Min(15*math.Pi/180, b.speed*-8*math.Pi/180))
89-
mat = mat.Moved(pixel.V(b.x, b.y))
77+
mat := pixel.IM.Rotated(pixel.V(0, 0), Min(15*math.Pi/180, -b.speed/10*math.Pi/180))
78+
mat = mat.Moved(pixel.V(b.X, b.Y))
9079

9180
sprite := b.sprite
92-
if b.ghost {
81+
if b.Ghost {
9382
sprite = Sprites["bird16"]
9483
}
9584

9685
sprite.Draw(win, mat)
9786
}
9887

88+
// Jump change speed of bird to jump
9989
func (b *Bird) Jump() {
100-
b.speed = -gravity * Delta * 23
90+
b.speed = -gravity * 20
10191
}
10292

93+
// IncreasePoint run all inscreases
10394
func (b *Bird) IncreasePoint() {
104-
b.points++
105-
}
106-
107-
func (b *Bird) Ghost() bool {
108-
return b.ghost
109-
}
110-
111-
func (b *Bird) IsEnableGhost() bool {
112-
return b.enableGhost
95+
b.Points++
11396
}
11497

98+
// UseGhost enable ghost power on bird
11599
func (b *Bird) UseGhost() {
116-
if b.enableGhost {
117-
b.ghost = true
118-
b.enableGhost = false
100+
if b.GhostCountdown == 0 {
101+
b.GhostCountdown = ghostCountdown
102+
b.Ghost = true
119103

120104
time.AfterFunc(2*time.Second, func() {
121-
b.ghost = false
122-
})
123-
124-
time.AfterFunc(7*time.Second, func() {
125-
b.enableGhost = true
105+
b.Ghost = false
126106
})
127107
}
128108
}

components/pipe.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ type Obstacle interface {
2222
Update()
2323
IsDefeated() bool
2424
Defeat()
25-
CheckCrash(b Bird) bool
25+
CheckCrash(x, y float64) bool
2626
GetType() float64
27+
GetWidth() float64
2728
}
2829

2930
// Pipe type of obstacle
@@ -50,6 +51,11 @@ func NewPipe(x float64, y float64) *Pipe {
5051
}
5152
}
5253

54+
// GetWidth return the pipe width
55+
func (p *Pipe) GetWidth() float64 {
56+
return PipeWidth
57+
}
58+
5359
// GetType return the type of obstacle
5460
func (p *Pipe) GetType() float64 {
5561
return 1
@@ -79,38 +85,38 @@ func (p *Pipe) Defeat() {
7985
func (p *Pipe) Draw(win *pixelgl.Window) {
8086
// Draw down part of pipe
8187
p.spriteUp.Draw(win, pixel.IM.Moved(pixel.V(p.x, p.y-100)))
82-
for i := p.y - 100 - PipeHeight; i > 0; i -= PipeHeight {
88+
for i := p.y - 100 - PipeHeight; i > 0; i -= PipeHeight - 1 {
8389
p.spriteDown.Draw(win, pixel.IM.Moved(pixel.V(p.x, i)))
8490
}
8591

8692
// Draw up part of pipe
8793
p.spriteUp.Draw(win, pixel.IM.Rotated(pixel.V(0, 0), math.Pi).Moved(pixel.V(p.x, p.y+100)))
88-
for i := p.y + 100 + PipeHeight; i < WindowHeight+PipeHeight; i += PipeHeight {
94+
for i := p.y + 100 + PipeHeight; i < WindowHeight+PipeHeight; i += PipeHeight - 1 {
8995
p.spriteDown.Draw(win, pixel.IM.Rotated(pixel.V(0, 0), math.Pi).Moved(pixel.V(p.x, i)))
9096
}
9197

92-
p.x -= XSpeed * Delta
98+
p.x -= GameXSpeed * Delta
9399
}
94100

95101
// Update the pipe
96102
func (p *Pipe) Update() {
97103
if p.direction == 1 {
98-
p.y++
104+
p.y += 0.5
99105
} else {
100-
p.y--
106+
p.y -= 0.5
101107
}
102108

103-
if p.y > WindowHeight-150 {
109+
if p.y > WindowHeight-200 {
104110
p.direction = 0
105111
} else if p.y < 200 {
106112
p.direction = 1
107113
}
108114
}
109115

110-
// CheckCrash check if a bird crash on the pipe
111-
func (p *Pipe) CheckCrash(b Bird) bool {
112-
if b.GetX() >= p.GetX()-50 && b.GetX() <= p.GetX()+50 {
113-
if b.GetY() <= (p.GetY()-55) || b.GetY() >= (p.GetY()+55) {
116+
// CheckCrash check if a position crash on pipe
117+
func (p *Pipe) CheckCrash(x, y float64) bool {
118+
if x >= p.GetX()-PipeWidth/2 && x <= p.GetX()+PipeWidth/2 {
119+
if y <= (p.GetY()-50) || y >= (p.GetY()+50) {
114120
return true
115121
}
116122
}

components/population.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@ func (p *Population) AddIndividual(i *Individual) {
3535

3636
// RemoveIndividual remove an individual from the population
3737
func (p *Population) RemoveIndividual(i int) {
38-
p.individuals[i] = p.individuals[len(p.individuals)-1]
39-
p.individuals = p.individuals[:len(p.individuals)-1]
38+
p.individuals = append(p.individuals[:i], p.individuals[i+1:]...)
4039
}

components/wall.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func NewWall(x float64) *Wall {
2323
}
2424
}
2525

26+
// GetWidth return the wall width
27+
func (w *Wall) GetWidth() float64 {
28+
return WallWidth
29+
}
30+
2631
// GetType return the type of obstacle
2732
func (w *Wall) GetType() float64 {
2833
return 2
@@ -56,17 +61,17 @@ func (w *Wall) Draw(win *pixelgl.Window) {
5661
Sprites["wall"].Draw(win, pixel.IM.Moved(pixel.V(w.X, float64(i)*160)))
5762
}
5863

59-
w.X -= XSpeed * Delta
64+
w.X -= GameXSpeed * Delta
6065
}
6166

6267
// Update do nothing
6368
func (w *Wall) Update() {
6469

6570
}
6671

67-
// CheckCrash check if a bird crash on the wall
68-
func (w *Wall) CheckCrash(b Bird) bool {
69-
if b.GetX() >= w.X-WallWidth/2 && b.GetX() <= w.X+WallWidth/2 {
72+
// CheckCrash check if a position crash on the wall
73+
func (w *Wall) CheckCrash(x, y float64) bool {
74+
if x >= w.X-WallWidth/2 && x <= w.X+WallWidth/2 {
7075
return true
7176
}
7277

components/window.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const WindowHeight = 720
1414
// Delta is to control FPS
1515
var Delta float64
1616

17-
// XSpeed is the horizontal speed
18-
var XSpeed = 180.0
17+
// GameXSpeed is the horizontal speed
18+
var GameXSpeed = 180.0
1919

2020
// CreateWindow creates a window
2121
func CreateWindow(t string) (*pixelgl.Window, error) {

neuralnetwork/layer.go

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Layer struct {
1010
neurons []*Neuron
1111
}
1212

13+
// NewLayer create a new layer
1314
func NewLayer(numberNeurons, numberInputs int64) *Layer {
1415
neurons := make([]*Neuron, numberNeurons)
1516
for i := range neurons {
@@ -21,10 +22,12 @@ func NewLayer(numberNeurons, numberInputs int64) *Layer {
2122
}
2223
}
2324

25+
// Neurons return all neurons of the layer
2426
func (l *Layer) Neurons() []*Neuron {
2527
return l.neurons
2628
}
2729

30+
// Run predicts all neurons
2831
func (l *Layer) Run(inputs []float64) []float64 {
2932
r := make([]float64, len(l.neurons))
3033
for i, n := range l.neurons {

0 commit comments

Comments
 (0)