-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame3.py
More file actions
101 lines (85 loc) · 2.97 KB
/
Copy pathgame3.py
File metadata and controls
101 lines (85 loc) · 2.97 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
import pygame
from helpers2 import get_model, get_action
import gym
FRAMES_PER_SECOND = 23
HEIGHT = 640
WIDTH = 480
WINDOW = (WIDTH, HEIGHT)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
def draw_start_screen(screen):
screen.fill(BLACK)
font = pygame.font.Font(None, 36)
text = font.render('Press any key to start', True, WHITE)
text_rect = text.get_rect(center=(WIDTH//2, HEIGHT//2))
screen.blit(text, text_rect)
def draw_death_screen(screen):
screen.fill(BLACK)
font = pygame.font.Font(None, 36)
text = font.render('Game Over! Press any key to restart', True, WHITE)
text_rect = text.get_rect(center=(WIDTH//2, HEIGHT//2))
screen.blit(text, text_rect)
def game_init():
pygame.init()
screen = pygame.display.set_mode(WINDOW)
clock = pygame.time.Clock()
model_path = "models/winnerpac.pth"
# model_path = "models/psychopac.pth"
# model_path = "models/suicidepac.pth"
# model_path = "models/paranoidpac.pth"
model, env = get_model(model_path)
obs, info = env.reset()
return screen, clock, model, env, obs
def main():
screen, clock, model, env, obs = game_init()
quit = False
draw_start_screen(screen)
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit = True
if event.type == pygame.KEYDOWN:
waiting = False
while not quit:
action = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] or keys[pygame.K_w]:
action = 1
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
action = 2
elif keys[pygame.K_LEFT] or keys[pygame.K_a]:
action = 3
elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
action = 4
elif keys[pygame.K_SPACE]:
action = get_action(model, obs)
action = [action]
obs, reward, done, _, info = env.step(action)
frame = env.render()
frame = pygame.surfarray.make_surface(frame)
frame = pygame.transform.rotate(frame, -90)
frame = pygame.transform.flip(frame, True, False)
screen.blit(pygame.transform.scale(frame, WINDOW), (0, 0))
pygame.display.flip()
if done:
if info[0]['lives'] == 0:
draw_death_screen(screen)
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit = True
if event.type == pygame.KEYDOWN:
waiting = False
obs, info = env.reset()
clock.tick(FRAMES_PER_SECOND)
env.close()
pygame.quit()
if __name__ == "__main__":
main()