add function for moving dust car
This commit is contained in:
parent
d0d9fb0309
commit
c5e6eadcae
7
bfs.py
7
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:
|
||||
|
10
main.py
10
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
|
||||
|
||||
|
48
movement.py
Normal file
48
movement.py
Normal file
@ -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])
|
19
turnCar.py
Normal file
19
turnCar.py
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user