-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
114 lines (95 loc) · 3.28 KB
/
main.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
from time import time, sleep
import contextlib
from time import time
with contextlib.redirect_stdout(None):
import pygame
from pygame.locals import *
import numpy as np
import multiprocessing as mp
import threading
from environment import BombeRLeWorld, ReplayWorld
from settings import s
# Function to run the game logic in a separate thread
def game_logic(world, user_inputs):
last_update = time()
while True:
# Game logic
if (s.turn_based and len(user_inputs) == 0):
sleep(0.1)
elif (s.gui and (time()-last_update < s.update_interval)):
sleep(s.update_interval - (time() - last_update))
else:
last_update = time()
if world.running:
try:
world.do_step(user_inputs.pop(0) if len(user_inputs) else 'WAIT')
except Exception as e:
world.end_round()
raise
def main():
pygame.init()
# Emulate Windows process spawning behaviour under Unix (for testing)
# mp.set_start_method('spawn')
# Initialize environment and agents
world = BombeRLeWorld([
('bombi_agent', False),
('simple_agent', False),
('simple_agent', False),
('simple_agent', False)
])
# world = ReplayWorld('Replay 2019-01-30 16:57:42')
user_inputs = []
# Start game logic thread
t = threading.Thread(target=game_logic, args=(world, user_inputs))
t.daemon = True
t.start()
# Run one or more games
for i in range(s.n_rounds):
if not world.running:
world.ready_for_restart_flag.wait()
world.ready_for_restart_flag.clear()
world.new_round()
# First render
if s.gui:
world.render()
pygame.display.flip()
round_finished = False
last_update = time()
last_frame = time()
user_inputs.clear()
# Main game loop
while not round_finished:
# Grab events
key_pressed = None
for event in pygame.event.get():
if event.type == QUIT:
world.end_round()
world.end()
return
elif event.type == KEYDOWN:
key_pressed = event.key
if key_pressed in (K_q, K_ESCAPE):
world.end_round()
if not world.running:
round_finished = True
# Convert keyboard input into actions
if s.input_map.get(key_pressed):
if s.turn_based:
user_inputs.clear()
user_inputs.append(s.input_map.get(key_pressed))
if not world.running and not s.gui:
round_finished = True
# Rendering
if s.gui and (time()-last_frame >= 1/s.fps):
world.render()
pygame.display.flip()
last_frame = time()
else:
sleep_time = 1/s.fps - (time() - last_frame)
if sleep_time > 0:
sleep(sleep_time)
if not s.gui:
last_frame = time()
world.end()
if __name__ == '__main__':
main()