-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
193 lines (150 loc) · 5.99 KB
/
player.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import pygame
import numpy as np
import sys
from nn import NeuralNetwork
from config import CONFIG
class Player():
def __init__(self, mode, control=False):
self.control = control # if True, playing mode is activated. else, AI mode.
self.pos = [100, 275] # position of the agent
self.direction = -1 # if 1, goes upwards. else, goes downwards.
self.v = 0 # vertical velocity
self.g = 9.8 # gravity constant
self.mode = mode # game mode
# neural network architecture (AI mode)
layer_sizes = self.init_network(mode)
self.nn = NeuralNetwork(layer_sizes)
self.fitness = 0 # fitness of agent
def move(self, box_lists, camera, events=None):
if len(box_lists) != 0:
if box_lists[0].x - camera + 60 < self.pos[0]:
box_lists.pop(0)
mode = self.mode
# manual control
if self.control:
self.get_keyboard_input(mode, events)
# AI control
else:
agent_position = [camera + self.pos[0], self.pos[1]]
self.direction = self.think(mode, box_lists, agent_position, self.v)
# game physics
if mode == 'gravity' or mode == 'helicopter':
self.v -= self.g * self.direction * (1 / 60)
self.pos[1] += self.v
elif mode == 'thrust':
self.v -= 6 * self.direction * (1 / 40)
self.pos[1] += self.v
# collision detection
is_collided = self.collision_detection(mode, box_lists, camera)
return is_collided
# reset agent parameters
def reset_values(self):
self.pos = [100, 275]
self.direction = -1
self.v = 0
def get_keyboard_input(self, mode, events=None):
if events is None:
events = pygame.event.get()
if mode == 'helicopter':
self.direction = -1
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.direction = 1
elif mode == 'thrust':
self.direction = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.direction = 1
elif keys[pygame.K_DOWN]:
self.direction = -1
for event in events:
if event.type == pygame.KEYDOWN:
if mode == 'gravity' and event.key == pygame.K_SPACE:
self.direction *= -1
def init_network(self, mode):
# you can change the parameters below
layer_sizes = None
if mode == 'gravity':
layer_sizes = [5, 20, 1]
elif mode == 'helicopter':
layer_sizes = [5, 20, 1]
elif mode == 'thrust':
layer_sizes = [5, 20, 1]
return layer_sizes
def think(self, mode, box_lists, agent_position, velocity):
# TODO
# mode example: 'helicopter'
# box_lists: an array of `BoxList` objects
# agent_position example: [600, 250]
# velocity example: 7
input_layer = []
if mode == 'helicopter' or mode == 'gravity':
if len(box_lists) > 0:
input_layer.append((box_lists[0].x - agent_position[0])/CONFIG['WIDTH'])
input_layer.append((box_lists[0].gap_mid - agent_position[1])/CONFIG['HEIGHT'])
else:
input_layer.append(1)
input_layer.append(0)
if len(box_lists) > 1:
input_layer.append((box_lists[1].x - agent_position[0])/CONFIG['WIDTH'])
input_layer.append((box_lists[1].gap_mid - agent_position[1])/CONFIG['HEIGHT'])
else:
input_layer.append(1)
input_layer.append(0)
input_layer.append(velocity/10)
out = self.nn.forward(np.array(input_layer))
# print(velocity)
if out >= 0.5:
direction = 1
else:
direction = -1
return direction
elif mode == 'thrust':
if len(box_lists) > 0:
input_layer.append((box_lists[0].x - agent_position[0])/CONFIG['WIDTH'])
input_layer.append((box_lists[0].gap_mid - agent_position[1])/CONFIG['HEIGHT'])
else:
input_layer.append(1)
input_layer.append(0)
if len(box_lists) > 1:
input_layer.append((box_lists[1].x - agent_position[0])/CONFIG['WIDTH'])
input_layer.append((box_lists[1].gap_mid - agent_position[1])/CONFIG['HEIGHT'])
else:
input_layer.append(1)
input_layer.append(0)
input_layer.append(velocity /10)
out = self.nn.forward(np.array(input_layer))
# action = out.index(max(out))
# print(out)
# if action == 0:
# direction = 1
# elif action == 1:
# direction = 0
# else:
# direction = -1
if out >= 0.5:
direction = 1
# elif out <= 0.40:
# direction = -1
else:
direction = -1
return direction
def collision_detection(self, mode, box_lists, camera):
if mode == 'helicopter':
rect = pygame.Rect(self.pos[0], self.pos[1], 100, 50)
elif mode == 'gravity':
rect = pygame.Rect(self.pos[0], self.pos[1], 70, 70)
elif mode == 'thrust':
rect = pygame.Rect(self.pos[0], self.pos[1], 110, 70)
else:
rect = pygame.Rect(self.pos[0], self.pos[1], 50, 50)
is_collided = False
if self.pos[1] < -60 or self.pos[1] > CONFIG['HEIGHT']:
is_collided = True
if len(box_lists) != 0:
box_list = box_lists[0]
for box in box_list.boxes:
box_rect = pygame.Rect(box[0] - camera, box[1], 60, 60)
if box_rect.colliderect(rect):
is_collided = True
return is_collided