-
Notifications
You must be signed in to change notification settings - Fork 16
/
game.py
163 lines (126 loc) · 4.3 KB
/
game.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
import random
from copy import copy
GRAVITY = 2
FRICTION = 0.9
class Game():
def __init__(self, width, height):
self.width = width
self.height = height
self.bird = Bird(width / 2, height / 2)
self.wall = self.create_wall()
self.lost = False
self.score = 0;
self.record = False
self.states = []
def update(self):
self.bird.update()
self.wall.update()
# 10 score for passing a wall
if (self.wall.x + self.wall.width) < self.width / 2:
self.wall = self.create_wall()
self.score += 10
if not self.record:
print("\033[32m+\033[0m", end='')
if self.intercept(self.bird, self.wall):
self.lost = True
if self.bird.y < 0 or self.bird.y > self.height:
self.lost = True
# a constant score for each movement, this way
# our birds will try to stay alive longer and
# our evolution strategy won't start with zero reward
self.score += 0.1
if self.record:
rec = { 'width': self.width,
'height': self.height,
'lost': self.lost,
'score': round(self.score, 2),
'bird': {
'x': self.bird.x,
'y': self.bird.y,
},
'wall': {
'x': self.wall.x,
'gate': {
'y': self.wall.gate.y,
'height': self.wall.gate.height
}
}
}
self.states.append(rec)
# create a wall, the wall is between the 15%-65% of the screen
def create_wall(self):
return Wall(self.width - WALL_WIDTH, self.height * (0.15 + np.random.random() * 0.5) )
def intercept(self, bird, wall):
return ((bird.x + bird.width) > wall.x and
((bird.y + bird.height) > (wall.gate.y + wall.gate.height) or
(bird.y) < wall.gate.y))
JUMP_STEPS = 2
JUMP_SPEED = 7
class Bird():
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 24
self.height = 18
self.velocity = np.array([0, 0], dtype=np.float64)
self.acceleration = np.array([0, 0], dtype=np.float64)
def jump(self):
self.velocity[1] = -JUMP_SPEED
self.acceleration[1] = 0
def update(self):
self.x += self.velocity[0]
self.y += self.velocity[1]
self.velocity += self.acceleration
self.velocity *= FRICTION
self.acceleration[1] = GRAVITY
WALL_WIDTH = 30
GATE_HEIGHT = 60
class Wall():
def __init__(self, x, y):
self.x = x
self.gate = dotdict({
'y': y,
'height': GATE_HEIGHT
})
self.width = WALL_WIDTH
def update(self):
self.x -= 2
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
# limit the game to 1000 frames while training, sometimes a game might take
# too long to finish after a while of training
MAX_FRAMES = 10000
def play(fn, step=None, record=False):
game = Game(250, 200)
frame = 0
# while showing to user, we want to update the GTK frontend
# the `step` function is responsible for doing just that, see index.py
if step:
return step(game, lambda: show_update(fn, game))
if record:
game.record = True
while not game.lost and frame < MAX_FRAMES:
frame += 1
# input of the model: bird x, bird y, distance to next wall, height of wall's entrance
data = np.array([[game.bird.x, game.bird.y, game.bird.x - game.wall.x, game.wall.gate.y]])
jump = fn(data)[0][0]
if jump > 0.5:
game.bird.jump()
game.update()
if record:
return game.states
return game.score
def show_update(fn, game):
if not game.lost:
data = np.array([[game.bird.x, game.bird.y, game.bird.x - game.wall.x, game.wall.gate.y]])
jump = fn(data)[0][0]
if jump > 0.5:
game.bird.jump()
game.update()
return True
else:
return False