Skip to content
Snippets Groups Projects
Commit d2e7af03 authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

Added game time limit, property game_ended of overcooked_environment returns...

Added game time limit, property game_ended of overcooked_environment returns true when time limit is reached. GUI switches to game end screen when it is finished.
parent 276cc639
No related branches found
No related tags found
1 merge request!28Resolve "Game time limit"
Pipeline #43799 passed
plates: plates:
clean_plates: 3 clean_plates: 3
dirty_plates: 2 dirty_plates: 2
plate_delay: [ 5, 10 ] plate_delay: [ 5, 10 ]
\ No newline at end of file
game:
time_limit_seconds: 10.0
...@@ -163,7 +163,7 @@ class PyGameGUI: ...@@ -163,7 +163,7 @@ class PyGameGUI:
game_height + (2 * self.screen_margin), game_height + (2 * self.screen_margin),
) )
return window_width, window_height, game_width, game_height, grid_size return int(window_width), int(window_height), game_width, game_height, grid_size
def create_player_colors(self) -> list[Color]: def create_player_colors(self) -> list[Color]:
number_player = len(self.simulator.env.players) number_player = len(self.simulator.env.players)
...@@ -735,6 +735,9 @@ class PyGameGUI: ...@@ -735,6 +735,9 @@ class PyGameGUI:
self.handle_keys() self.handle_keys()
state = self.simulator.get_state() state = self.simulator.get_state()
if state["ended"]:
self.finished_button_press()
self.manage_button_visibility()
self.draw(state) self.draw(state)
game_screen_rect = self.game_screen.get_rect() game_screen_rect = self.game_screen.get_rect()
......
# colors: https://www.webucator.com/article/python-color-constants-module/ # colors: https://www.webucator.com/article/python-color-constants-module/
GameWindow: GameWindow:
WhatIsFixed: window_height # entweder grid oder window_width WhatIsFixed: grid # grid or window_width or window_height
size: 500 size: 40
screen_margin: 100 screen_margin: 100
start_width: 600 start_width: 600
start_height: 600 start_height: 600
......
...@@ -134,6 +134,14 @@ class Environment: ...@@ -134,6 +134,14 @@ class Environment:
self.score: int = 0 self.score: int = 0
self.env_time = create_init_env_time() self.env_time = create_init_env_time()
self.env_time_end = self.env_time + timedelta(
seconds=environment_config["game"]["time_limit_seconds"]
)
log.debug(f"End time: {self.env_time_end}")
@property
def game_ended(self) -> bool:
return self.env_time >= self.env_time_end
def load_item_info(self) -> dict[str, ItemInfo]: def load_item_info(self) -> dict[str, ItemInfo]:
with open(self.item_info_path, "r") as file: with open(self.item_info_path, "r") as file:
...@@ -437,6 +445,7 @@ class Environment: ...@@ -437,6 +445,7 @@ class Environment:
and time limits. and time limits.
""" """
self.env_time += passed_time self.env_time += passed_time
with self.lock: with self.lock:
for counter in self.counters: for counter in self.counters:
if isinstance(counter, (CuttingBoard, Stove, Sink, PlateDispenser)): if isinstance(counter, (CuttingBoard, Stove, Sink, PlateDispenser)):
...@@ -448,7 +457,12 @@ class Environment: ...@@ -448,7 +457,12 @@ class Environment:
Returns: Dict of lists of the current relevant game objects. Returns: Dict of lists of the current relevant game objects.
""" """
return {"players": self.players, "counters": self.counters, "score": self.score} return {
"players": self.players,
"counters": self.counters,
"score": self.score,
"ended": self.game_ended,
}
def get_state_json(self): def get_state_json(self):
"""Get the current state of the game environment as a json-like nested dictionary. """Get the current state of the game environment as a json-like nested dictionary.
......
...@@ -98,9 +98,11 @@ class Simulator(Thread): ...@@ -98,9 +98,11 @@ class Simulator(Thread):
overslept_in_ns = 0 overslept_in_ns = 0
self.env.reset_env_time() self.env.reset_env_time()
last_step_start = time.time_ns()
while not self.finished: while not self.finished:
step_start = time.time_ns() step_start = time.time_ns()
self.step(timedelta(seconds=overslept_in_ns / 1_000_000_000)) self.step(timedelta(seconds=(step_start - last_step_start) / 1_000_000_000))
last_step_start = step_start
step_duration = time.time_ns() - step_start step_duration = time.time_ns() - step_start
time_to_sleep_ns = self.preferred_sleep_time_ns - ( time_to_sleep_ns = self.preferred_sleep_time_ns - (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment