forked from HosseinZaredar/EvolutionaryGames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
168 lines (132 loc) · 5.47 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
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
layer_sizes = None
if mode == 'gravity':
layer_sizes = [5, 20, 1]
elif mode == 'helicopter':
layer_sizes = [5, 20, 1]
elif mode == 'thrust':
layer_sizes = [2, 20, 1]
return layer_sizes
def think(self, mode, box_lists, agent_position, velocity):
direction = -1
if mode == 'helicopter':
inputLayer = np.zeros((5, 1))
inputLayer[0, 0] = velocity / 10
inputLayer[1, 0] = agent_position[1] / CONFIG["HEIGHT"]
if len(box_lists) >= 2:
inputLayer[2, 0] = (agent_position[1] - box_lists[0].gap_mid) / CONFIG["HEIGHT"]
inputLayer[3, 0] = (agent_position[1] - box_lists[1].gap_mid) / CONFIG["HEIGHT"]
inputLayer[4, 0] = (box_lists[0].x - agent_position[0]) / CONFIG["WIDTH"]
result = self.nn.forward(inputLayer)[0]
if result > 0.5:
direction = 1
else:
direction = -1
elif mode == 'gravity':
inputLayer = np.zeros((5, 1))
inputLayer[0, 0] = velocity / 10
inputLayer[1, 0] = agent_position[1] / CONFIG["HEIGHT"]
if len(box_lists) >= 2:
inputLayer[2, 0] = (agent_position[1] - box_lists[0].gap_mid) / CONFIG["HEIGHT"]
inputLayer[3, 0] = (agent_position[1] - box_lists[1].gap_mid) / CONFIG["HEIGHT"]
inputLayer[4, 0] = (box_lists[0].x - agent_position[0]) / CONFIG["WIDTH"]
result = self.nn.forward(inputLayer)[0]
if result > 0.5:
direction = 1
else:
direction = -1
elif mode == 'thrust':
inputLayer = np.zeros((2, 1))
inputLayer[0, 0] = velocity
if len(box_lists) >= 1:
inputLayer[1, 0] = (agent_position[1] - box_lists[0].gap_mid) / CONFIG["HEIGHT"]
result = self.nn.forward(inputLayer)[0]
if result > 0.6:
direction = 1
elif result < 0.3:
direction = -1
else:
direction = 0
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