-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMelee.py
More file actions
326 lines (288 loc) · 12.6 KB
/
Melee.py
File metadata and controls
326 lines (288 loc) · 12.6 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import pygame
import numpy as np
import keras
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
import random
class Player1(pygame.sprite.Sprite):
def __init__(self, screen_width, screen_height, model, is_cpu, circles, all_sprites):
super(Player1, self).__init__()
self.model = model
self.is_cpu = is_cpu
self.screen_width = screen_width
self.screen_height = screen_height
self.circles = circles
self.all_sprites = all_sprites
elon = pygame.image.load("RL-Game/elon.PNG")
self.jeff_sad = pygame.image.load("RL-Game/jeff_sad.PNG")
self.surf = pygame.transform.scale(elon, (25*4, 25*4))
self.cooldown = 15
self.health = 10
self.rect = self.surf.get_rect()
def update(self, pressed_keys, player, features=None):
upperY = self.rect.top
lowerY = self.rect.bottom
upperX = self.rect.right
lowerX = self.rect.left
if self.is_cpu:
if features is not None:
features = np.expand_dims(features, axis=0)
# Predict the next move based on the features of the environment
res = self.model.predict(np.asarray(features))
res = np.argmax(res)
if res == 0 and upperY > 0:
self.rect.move_ip(0, -10*4)
if res == 1 and lowerY < self.screen_height:
self.rect.move_ip(0, 10*4)
if res == 2 and lowerX > 0:
self.rect.move_ip(-10*4, 0)
if res == 3 and upperX < self.screen_width:
self.rect.move_ip(10*4, 0)
if res == 4 and self.cooldown == 15:
# Reset attack cooldown
self.cooldown = 0
# Create attack animation
circle = Circle((0, 255, 0), self.rect.center, 40*4, 2*4, 2)
self.all_sprites.add(circle)
self.circles.add(circle)
# Determine if the enemy player is in range
player_pos = self.rect.center
other_player_pos = player.rect.center
x_diff = abs(player_pos[0] - other_player_pos[0])
y_diff = abs(player_pos[1] - other_player_pos[1])
if x_diff < 50*4 and y_diff < 50*4:
player.health -= 1
player.surf = pygame.transform.scale(self.jeff_sad, (25*4, 25*4))
else:
if pressed_keys[K_w] and upperY > 0:
self.rect.move_ip(0, -10*4)
if pressed_keys[K_s] and lowerY < self.screen_height:
self.rect.move_ip(0, 10*4)
if pressed_keys[K_a] and lowerX > 0:
self.rect.move_ip(-10*4, 0)
if pressed_keys[K_d] and upperX < self.screen_width:
self.rect.move_ip(10*4, 0)
if pressed_keys[K_SPACE] and self.cooldown == 15:
player_pos = self.rect.center
circle = Circle((0, 255, 0), self.rect.center, 40*4, 2*4, 2)
self.all_sprites.add(circle)
self.circles.add(circle)
other_player_pos = player.rect.center
x_diff = abs(player_pos[0] - other_player_pos[0])
y_diff = abs(player_pos[1] - other_player_pos[1])
if x_diff < 50*4 and y_diff < 50*4:
player.health -= 1
player.surf = pygame.transform.scale(self.jeff_sad, (25*4, 25*4))
self.cooldown = 0
if self.cooldown < 15:
self.cooldown += 1
class Player2(pygame.sprite.Sprite):
def __init__(self, screen_width, screen_height, model, is_cpu, circles, all_sprites):
super(Player2, self).__init__()
self.screen_width = screen_width
self.screen_height = screen_height
self.model = model
self.is_cpu = is_cpu
self.circles = circles
self.all_sprites = all_sprites
jeff = pygame.image.load("RL-Game/jeff.PNG")
self.elon_sad = pygame.image.load("RL-Game/elon_sad.PNG")
self.surf = pygame.transform.scale(jeff, (25*4, 25*4))
self.cooldown = 15
self.health = 10
self.rect = self.surf.get_rect()
self.rect.move_ip(screen_width-(25*4), 0)
def update(self, pressed_keys, player, features=None):
upperY = self.rect.top
lowerY = self.rect.bottom
upperX = self.rect.right
lowerX = self.rect.left
if self.is_cpu:
if features is not None:
features = np.expand_dims(features, axis=0)
res = self.model.predict(np.asarray(features))
res = np.argmax(res)
if res == 0 and upperY > 0:
self.rect.move_ip(0, -10*4)
if res == 1 and lowerY < self.screen_height:
self.rect.move_ip(0, 10*4)
if res == 2 and upperX < self.screen_width:
self.rect.move_ip(10*4, 0)
if res == 3 and lowerX > 0:
self.rect.move_ip(-10*4, 0)
if res == 4 and self.cooldown == 15:
self.cooldown = 0
circle = Circle((255, 0, 0), self.rect.center, 40*4, 2*4, 2)
self.all_sprites.add(circle)
self.circles.add(circle)
player_pos = self.rect.center
other_player_pos = player.rect.center
x_diff = abs(player_pos[0] - other_player_pos[0])
y_diff = abs(player_pos[1] - other_player_pos[1])
if x_diff < 50*4 and y_diff < 50*4:
player.health -= 1
player.surf = pygame.transform.scale(self.elon_sad, (25*4, 25*4))
else:
# action = random.choice([0, 1, 2, 3, 4, 5])
action = 4
#if pressed_keys[K_UP] and upperY > 0:
if action == 0 and upperY > 0:
self.rect.move_ip(0, -10*4)
#if pressed_keys[K_DOWN] and lowerY < self.screen_height:
elif action == 1 and lowerY < self.screen_height:
self.rect.move_ip(0, 10*4)
#if pressed_keys[K_RIGHT] and upperX < self.screen_width:
elif action == 2 and upperX < self.screen_width:
self.rect.move_ip(10*4, 0)
#if pressed_keys[K_LEFT] and lowerX > 0:
elif action == 3 and lowerX > 0:
self.rect.move_ip(-10*4, 0)
#if pressed_keys[K_l] and self.cooldown == 15:
elif action == 4 and self.cooldown == 15:
circle = Circle((255,0,0), self.rect.center, 40*4, 2*4, 2)
self.all_sprites.add(circle)
self.circles.add(circle)
player_pos = self.rect.center
other_player_pos = player.rect.center
x_diff = abs(player_pos[0] - other_player_pos[0])
y_diff = abs(player_pos[1] - other_player_pos[1])
if x_diff < 50*4 and y_diff < 50*4:
player.surf.fill((255,255,255))
player.health -= 1
self.cooldown = 0
if self.cooldown < 15:
self.cooldown += 1
class Circle(pygame.sprite.Sprite):
def __init__(self, color, center, radius, thinkness, duration):
super(Circle, self).__init__()
self.surf = pygame.Surface((radius*2, radius*2))
pygame.draw.circle(self.surf, color, (radius, radius), radius, thinkness)
rect = self.surf.get_rect()
self.rect = rect.move(center[0] - radius, center[1] - radius)
self.duration = duration
def update(self):
self.duration -= 1
if self.duration == 0:
self.kill()
class Melee():
def __init__(self, models, is_cpu, dt):
self.screen_width = 250*4
self.screen_height = 150*4
self.is_game_over = False
self.dt = dt
self.injured_time = 8
self.elon = pygame.image.load("RL-Game/elon.PNG")
self.jeff = pygame.image.load("RL-Game/jeff.PNG")
# Create the screen object
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
self.past_frames = []
# Create sprite groups
self.circles = pygame.sprite.Group()
self.all_sprites = pygame.sprite.Group()
self.players = pygame.sprite.Group()
# Create players
self.player1 = Player1(self.screen_width, self.screen_height, models[0], is_cpu[0], self.circles, self.all_sprites)
self.player2 = Player2(self.screen_width, self.screen_height, models[1], is_cpu[1], self.circles, self.all_sprites)
self.all_sprites.add(self.player1)
self.all_sprites.add(self.player2)
self.players.add(self.player1)
self.players.add(self.player2)
def is_in_range(self):
player_pos = self.player1.rect.center
other_player_pos = self.player2.rect.center
x_diff = abs(player_pos[0] - other_player_pos[0])
y_diff = abs(player_pos[1] - other_player_pos[1])
if x_diff < 50*4 and y_diff < 50*4:
return 1
return 0
def game_over(self):
return self.is_game_over
# Gives the features of the environment that the model uses to decide its next action
def get_features(self):
features = []
# The model takes input of features from the previous 8 frames
num_past_frames = 8
# Distance between players
features.append((self.player1.rect.centerx - self.player2.rect.centerx) / self.screen_width)
features.append((self.player1.rect.centery - self.player2.rect.centery) / self.screen_height)
# Absolution position of players
features.append(self.player1.rect.centerx / self.screen_width)
features.append(self.player2.rect.centerx / self.screen_width)
features.append(self.player1.rect.centery / self.screen_height)
features.append(self.player2.rect.centery / self.screen_height)
# If the opponent is in range of attack
features.append(self.is_in_range())
# Attack cooldown
features.append(self.player1.cooldown / 15)
features.append(self.player2.cooldown / 15)
if len(self.past_frames) == num_past_frames:
self.past_frames.pop(0)
self.past_frames.append(features)
# If we do not have a record of 8 frames then do nothing
if len(self.past_frames) == num_past_frames:
return self.past_frames
return None
def step(self, pressed_keys):
# Update sprites
self.player1.update(pressed_keys, self.player2, self.get_features())
self.player2.update(pressed_keys, self.player1, self.get_features())
self.circles.update()
if self.player1.health == 0:
print("Jeff wins!")
self.is_game_over = True
if self.player2.health == 0:
print("Elon wins!")
self.is_game_over = True
# Clear the screen
self.screen.fill((0, 0, 0))
# Change the sprite image if injured
if self.player1.cooldown > self.injured_time:
self.player2.surf = pygame.transform.scale(self.jeff, (25*4, 25*4))
if self.player2.cooldown > self.injured_time:
self.player1.surf = pygame.transform.scale(self.elon, (25*4, 25*4))
# Draw all sprites
for entity in self.all_sprites:
self.screen.blit(entity.surf, entity.rect)
pygame.display.flip()
# Ensure program maintains a rate of dt frames per second
clock.tick(self.dt)
if __name__ == "__main__":
# Import pygame.locals for easier access to key coordinates
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_w,
K_s,
K_d,
K_a,
K_l,
K_SPACE,
K_ESCAPE,
KEYDOWN,
QUIT,
)
# Initialize pygame
pygame.init()
clock = pygame.time.Clock()
# Load models
model1 = keras.models.load_model('melee_weights_gen_3')
model2 = keras.models.load_model('melee_weights_gen_4')
models = [model1, model2]
is_cpu = [False, True]
dt = 20
game = Melee(models, is_cpu, dt)
# Variable to keep the main loop running
is_running = True
while is_running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
is_running = False
elif event.type == QUIT:
is_running = False
pressed_keys = pygame.key.get_pressed()
game.step(pressed_keys)
if game.game_over():
is_running = False