-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
164 lines (124 loc) · 5.13 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
import pygame
import numpy as np
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
self.pos[1] += self.v * (1 / 40)
# 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 (even you can add more hidden layer)
layer_sizes = None
if mode == 'gravity':
layer_sizes = CONFIG["gravity_mode_network"]
elif mode == 'helicopter':
layer_sizes = CONFIG["helicopter_mode_network"]
elif mode == 'thrust':
layer_sizes = CONFIG["thrust_mode_network"]
return layer_sizes
def think(self, mode, box_lists, agent_position, velocity):
# mode example: 'helicopter'
# box_lists: an array of `BoxList` objects
# agent_position example: [600, 250]
# velocity example: 7
input_data = np.zeros((5, 1))
# creating nomalized input vector
if(len(box_lists) > 0):
input_data[0] = (box_lists[0].x - agent_position[0]) / CONFIG["WIDTH"]
input_data[1] = (box_lists[0].gap_mid - agent_position[1]) / CONFIG["HEIGHT"]
if(len(box_lists) > 1):
input_data[2] = (box_lists[1].x - agent_position[0]) / CONFIG["WIDTH"]
input_data[3] = (box_lists[1].gap_mid - agent_position[1]) / CONFIG["HEIGHT"]
input_data[4] = velocity / CONFIG["MAXIMUM_VELOCITY"]
ann_output = self.nn.forward(input_data)
# (additional) all modes
player_direction = 0
maximum_output_index = ann_output.argmax()
if mode == "thrust":
# thrust mode wiht 3 output
if maximum_output_index == 0:
player_direction = 0
elif maximum_output_index == 1:
player_direction = -1
else:
player_direction = 1
else:
# gravity and helicopter mode wiht 2 output
if maximum_output_index == 0:
player_direction = 1
else:
player_direction = -1
return player_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