diff --git a/overcooked_simulator/counters.py b/overcooked_simulator/counters.py index d37099f5b144afa5a78fd7d75aaf51a6df9b04a8..d48fbe810e0f63b09b6cf1d702245e4ae5842964 100644 --- a/overcooked_simulator/counters.py +++ b/overcooked_simulator/counters.py @@ -9,4 +9,4 @@ class Counter: self.occupied_by = None def __repr__(self): - return f"Counter(pos:{str(self.pos)},holds:{self.occupied_by})" \ No newline at end of file + return f"Counter(pos:{str(self.pos)},holds:{self.occupied_by})" diff --git a/overcooked_simulator/main.py b/overcooked_simulator/main.py index 9200613745e73fc2d51294a4f76f1f6c50676a82..975657704561d7ca1b5c2fd215f816571afd2b74 100644 --- a/overcooked_simulator/main.py +++ b/overcooked_simulator/main.py @@ -13,11 +13,9 @@ def main(): gui = PyGameGUI(simulator) simulator.start() - print(simulator.get_state()) gui.start_pygame() - simulator.stop() sys.exit() diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index 9ffdae06df3d140897e555c59e0e5e7d059fa8a5..4545c4f3c3157d71a2550445f9687439e70d23fe 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -23,6 +23,7 @@ class Environment: """Environment class which handles the game logic for the overcooked-inspired environment. Handles player movement, collision-detection, counters, cooking processes, recipes, incoming orders, time. + # TODO Abstract base class for different environments """ def __init__(self): self.players = {} @@ -58,8 +59,6 @@ class Environment: current_x += self.counter_side_length elif character == "E": current_x += self.counter_side_length - - current_y += self.counter_side_length return counters diff --git a/overcooked_simulator/pygame_gui.py b/overcooked_simulator/pygame_gui.py index a4410c332db0c2723999d74494134e9301143024..1ae75ed612ecf1502bc6e69c0817a8afbff73bf1 100644 --- a/overcooked_simulator/pygame_gui.py +++ b/overcooked_simulator/pygame_gui.py @@ -3,7 +3,6 @@ import numpy as np from overcooked_simulator.overcooked_environment import Action from overcooked_simulator.simulation_runner import Simulator -FPS = 60 WHITE = (255, 255, 255) GREY = (190, 190, 190) @@ -14,16 +13,14 @@ GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) - BACKGROUND_COLOR = GREY -GET_CONTINOUS_INTERACT_AND_PICKUP = False - class PlayerKeyset: - """Set of keyboard keys for controlling a player. First four keys are for movement, 5th key is for interacting - with counters. 6th key ist for picking up things or dropping them. - + """Set of keyboard keys for controlling a player. + First four keys are for movement, + 5th key is for interacting with counters. + 6th key ist for picking up things or dropping them. """ def __init__(self, keys: list[pygame.key]): self.player_keys = keys @@ -37,9 +34,12 @@ class PyGameGUI: """Visualisation of the overcooked environmnent and reading keyboard inputs using pygame. """ def __init__(self, simulator: Simulator): + self.FPS = 60 self.simulator = simulator self.counter_size = self.simulator.env.counter_side_length - self.WINDOW_WIDTH, self.WINDOW_HEIGHT = simulator.env.world_width, simulator.env.world_height + self.window_width, self.window_height = simulator.env.world_width, simulator.env.world_height + + self.GET_CONTINOUS_INTERACT_AND_PICKUP = False keys1 = [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN, pygame.K_SPACE, pygame.K_i] keys2 = [pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s, pygame.K_f, pygame.K_e] @@ -49,7 +49,7 @@ class PyGameGUI: """Sends an action to the game environment. Args: - action: The action to be sent. + action: The action to be sent. Contains the player, action type and move direction if action is a movement. """ self.simulator.enter_action(action) @@ -70,7 +70,7 @@ class PyGameGUI: action = Action(f"p{player_idx+1}", "movement", move_vec) self.send_action(action) - if GET_CONTINOUS_INTERACT_AND_PICKUP: + if self.GET_CONTINOUS_INTERACT_AND_PICKUP: if relevant_keys[-2]: action = Action(f"p{player_idx+1}", "interact", "interact") self.send_action(action) @@ -79,7 +79,8 @@ class PyGameGUI: self.send_action(action) def handle_interact_single_send(self, event): - """Handles key events. Here when a key is held down, only one action is sent. + """Handles key events. Here when a key is held down, only one action is sent. (This can be + switched by the GET_CONTINOUS_INTERACT_AND_PICKUP flag) Args: event: Pygame event for extracting the key. @@ -95,12 +96,12 @@ class PyGameGUI: def draw_background(self): """Visualizes a game background. """ - GREY = (200, 200, 200) - blockSize = 20 # Set the size of the grid block - for x in range(0, self.WINDOW_WIDTH, blockSize): - for y in range(0, self.WINDOW_HEIGHT, blockSize): - rect = pygame.Rect(x, y, blockSize, blockSize) - pygame.draw.rect(self.screen, GREY, rect, 1) + BACKGROUND_LINES = (200, 200, 200) + block_size = self.counter_size//2 # Set the size of the grid block + for x in range(0, self.window_width, block_size): + for y in range(0, self.window_height, block_size): + rect = pygame.Rect(x, y, block_size, block_size) + pygame.draw.rect(self.screen, BACKGROUND_LINES, rect, 1) def draw_players(self, state): """Visualizes the players as circles with an triangle for the facing diretion. @@ -121,7 +122,8 @@ class PyGameGUI: facing = player.facing_direction pygame.draw.polygon(self.screen, BLUE, - ((pos[0]+(facing[1]*5), pos[1]-(facing[0]*5)), (pos[0]-(facing[1]*5), pos[1]+(facing[0]*5)), player.pos + (facing * 20))) + ((pos[0]+(facing[1]*5), pos[1]-(facing[0]*5)), + (pos[0]-(facing[1]*5), pos[1]+(facing[0]*5)), player.pos + (facing * 20))) def draw_counters(self, state): """Visualizes the counters in the environment. @@ -156,7 +158,7 @@ class PyGameGUI: pygame.init() pygame.font.init() - self.screen = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT)) + self.screen = pygame.display.set_mode((self.window_width, self.window_height)) pygame.display.set_caption("Simple Overcooked Simulator") self.screen.fill(BACKGROUND_COLOR) @@ -169,12 +171,12 @@ class PyGameGUI: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False - if not GET_CONTINOUS_INTERACT_AND_PICKUP: + if not self.GET_CONTINOUS_INTERACT_AND_PICKUP: if event.type == pygame.KEYDOWN: self.handle_interact_single_send(event) self.handle_keys() - clock.tick(FPS) + clock.tick(self.FPS) state = self.simulator.get_state() self.draw(state) diff --git a/tests/test_start.py b/tests/test_start.py index b60b38a91c31467e69bf1f54a8193387a66be12b..839b4cb1ac3e2b9bc780bfc6e4346fff28056624 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -7,7 +7,6 @@ from overcooked_simulator.counters import Counter def test_player_registration(): - sim = Simulator(Environment, 200) p1 = Player("player1", np.array([0, 0])) sim.register_player(p1) @@ -21,12 +20,13 @@ def test_player_registration(): assert len(sim.env.players) == 2, "Wrong number of players" p3 = Player("player2", np.array([100, 100])) - sim.register_player(p2) # same player name + sim.register_player(p2) # same player name assert len(sim.env.players) == 2, "Wrong number of players" sim.start() sim.stop() + def test_simulator_frequency(): class TestEnv: def __init__(self): @@ -48,7 +48,7 @@ def test_simulator_frequency(): accepted_tolerance = 0.02 lower = frequency * running_time_seconds * (1-accepted_tolerance) upper = frequency * running_time_seconds * (1+accepted_tolerance) - assert sim.env.c > lower and sim.env.c < upper, "Timing error in the environment at 1000hz" + assert lower < sim.env.c < upper, "Timing error in the environment at 1000hz" def test_collision_detection(): @@ -65,9 +65,11 @@ def test_collision_detection(): assert not sim.env.detect_collision_counters(player1), "Should not collide" assert not sim.env.detect_player_collision(player1), "Should not collide yet." + assert not sim.env.detect_collision(player1), "Does not collide yet." + player1.move_abs(counter_pos) - assert sim.env.detect_collision_counters(player1), "Player and counter at same pos." + assert sim.env.detect_collision_counters(player1), "Player and counter at same pos. Not detected." player2.move_abs(counter_pos) - assert sim.env.detect_player_collision(player1), "Players at same pos." + assert sim.env.detect_player_collision(player1), "Players at same pos. Not detected." sim.stop()