From c5e6eadcaed03b559871439af926089102ade8af Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Sat, 22 Apr 2023 17:46:08 +0200 Subject: [PATCH] add function for moving dust car --- bfs.py | 7 +------ main.py | 10 ++++++++++ movement.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ turnCar.py | 19 +++++++++++++++++++ 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 movement.py create mode 100644 turnCar.py diff --git a/bfs.py b/bfs.py index eaa99fa..6c1afb7 100644 --- a/bfs.py +++ b/bfs.py @@ -4,6 +4,7 @@ from gridCellType import GridCellType from agentActionType import AgentActionType from agentOrientation import AgentOrientation from queue import Queue +from turnCar import turn_left_orientation, turn_right_orientation class Succ: state: AgentState @@ -56,12 +57,6 @@ def succ(state: AgentState) -> list[Succ]: result.append(move_forward_succ(state)) return result -def turn_left_orientation(orientation: AgentOrientation) -> AgentOrientation: - return (orientation - 1) % 4 - -def turn_right_orientation(orientation: AgentOrientation) -> AgentOrientation: - return (orientation + 1) % 4 - def move_forward_succ(state: AgentState) -> Succ: position = get_next_cell(state) if position == None: diff --git a/main.py b/main.py index 467a645..88a0c48 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ from gameEventHandler import handle_game_event from gameContext import GameContext from startup import startup from PIL import Image +from agentActionType import AgentActionType +from movement import move_dust_car pygame.init() @@ -17,6 +19,14 @@ game_context.dust_car_pygame = pygame.image.frombuffer(dust_car_pil.tobytes(), d game_context.canvas = canvas startup(game_context) +# test = [AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, +# AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, +# AgentActionType.MOVE_FORWARD, AgentActionType.TURN_RIGHT, AgentActionType.MOVE_FORWARD, +# AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, +# AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, +# AgentActionType.TURN_LEFT, AgentActionType.MOVE_FORWARD] + +# move_dust_car(test, game_context) exit = False diff --git a/movement.py b/movement.py new file mode 100644 index 0000000..39f0650 --- /dev/null +++ b/movement.py @@ -0,0 +1,48 @@ +from agentActionType import AgentActionType +import time +from turnCar import turn_left_orientation, turn_right_orientation +from garbageTruck import GarbageTruck +from typing import Tuple, Dict +from gridCellType import GridCellType +from gameContext import GameContext +from agentOrientation import AgentOrientation +import pygame + +def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None: + for action in actions: + street_position = game_context.dust_car.position + has_to_render_street = False + if action == AgentActionType.TURN_LEFT: + game_context.dust_car.orientation = turn_left_orientation(game_context.dust_car.orientation) + elif action == AgentActionType.TURN_RIGHT: + game_context.dust_car.orientation = turn_right_orientation(game_context.dust_car.orientation) + elif action == AgentActionType.MOVE_FORWARD: + game_context.dust_car.position = calculate_next_position(game_context.dust_car) + has_to_render_street = True + game_context.dust_car.render(game_context) + if has_to_render_street: + if game_context.grid[street_position] == GridCellType.STREET_HORIZONTAL: + game_context.render_in_cell(street_position, "imgs/street_horizontal.png") + elif game_context.grid[street_position] == GridCellType.STREET_VERTICAL: + game_context.render_in_cell(street_position, "imgs/street_vertical.png") + pygame.display.update() + time.sleep(0.5) + + + +def calculate_next_position(car: GarbageTruck) -> Tuple[int, int]: + if car.orientation == AgentOrientation.UP: + if car.position[1] - 1 < 1: + return None + return (car.position[0], car.position[1] - 1) + if car.orientation == AgentOrientation.DOWN: + if car.position[1] + 1 > 27: + return None + return (car.position[0], car.position[1] + 1) + if car.orientation == AgentOrientation.LEFT: + if car.position[0] - 1 < 1: + return None + return (car.position[0] - 1, car.position[1]) + if car.position[0] + 1 > 27: + return None + return (car.position[0] + 1, car.position[1]) \ No newline at end of file diff --git a/turnCar.py b/turnCar.py new file mode 100644 index 0000000..f0a0c34 --- /dev/null +++ b/turnCar.py @@ -0,0 +1,19 @@ +from agentOrientation import AgentOrientation + +def turn_left_orientation(orientation: AgentOrientation) -> AgentOrientation: + if orientation == AgentOrientation.DOWN: + return AgentOrientation.RIGHT + if orientation == AgentOrientation.LEFT: + return AgentOrientation.DOWN + if orientation == AgentOrientation.UP: + return AgentOrientation.LEFT + return AgentOrientation.UP + +def turn_right_orientation(orientation: AgentOrientation) -> AgentOrientation: + if orientation == AgentOrientation.DOWN: + return AgentOrientation.LEFT + if orientation == AgentOrientation.LEFT: + return AgentOrientation.UP + if orientation == AgentOrientation.UP: + return AgentOrientation.RIGHT + return AgentOrientation.DOWN \ No newline at end of file