dust car instance

This commit is contained in:
Pawel Felcyn 2023-04-22 16:36:13 +02:00
parent 7c44d65347
commit d0d9fb0309
4 changed files with 27 additions and 25 deletions

View File

@ -13,6 +13,7 @@ class GameContext:
_cell_size: int = 30 _cell_size: int = 30
city = None city = None
grid: Dict[Tuple[int, int], GridCellType] = {} grid: Dict[Tuple[int, int], GridCellType] = {}
dust_car = None
def __init__(self) -> None: def __init__(self) -> None:
self._init_grid() self._init_grid()

View File

@ -2,23 +2,4 @@ import pygame
from gameContext import GameContext from gameContext import GameContext
def handle_game_event(event, game_context: GameContext): def handle_game_event(event, game_context: GameContext):
dust_car_movement(event, game_context) pass
return
def dust_car_movement(event, game_context:GameContext):
if event.type != pygame.KEYDOWN:
return
(width, height) = game_context.dust_car_pil.size
if event.key == pygame.K_LEFT:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_x -= game_context.dust_car_speed
elif event.key == pygame.K_RIGHT:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_x += game_context.dust_car_speed
elif event.key == pygame.K_UP:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_y -= game_context.dust_car_speed
elif event.key == pygame.K_DOWN:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_y += game_context.dust_car_speed
game_context.canvas.blit(game_context.dust_car_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))

View File

@ -1,6 +1,7 @@
from typing import List, Tuple from typing import List, Tuple
from agentOrientation import AgentOrientation
from garbage import RecognizedGarbage from garbage import RecognizedGarbage
from gameContext import GameContext
class GarbageTruck: class GarbageTruck:
position: Tuple[int, int] position: Tuple[int, int]
@ -9,8 +10,9 @@ class GarbageTruck:
glass: List[RecognizedGarbage] glass: List[RecognizedGarbage]
bio: List[RecognizedGarbage] bio: List[RecognizedGarbage]
mixed: List[RecognizedGarbage] mixed: List[RecognizedGarbage]
truckSpritePath = 'imgs/dust_car.png' orientation: AgentOrientation = AgentOrientation.RIGHT
def __init__(self, position: List[int, int]) -> None:
def __init__(self, position: Tuple[int, int]) -> None:
self.position = position self.position = position
self.paper = [] self.paper = []
self.plastic_and_metal = [] self.plastic_and_metal = []
@ -33,4 +35,16 @@ class GarbageTruck:
self.bio.append(RecognizedGarbage) self.bio.append(RecognizedGarbage)
elif RecognizedGarbage.garbage_type == 5: elif RecognizedGarbage.garbage_type == 5:
self.mixed.append(RecognizedGarbage) self.mixed.append(RecognizedGarbage)
def render(self, game_context: GameContext) -> None:
path = None
if self.orientation == AgentOrientation.LEFT:
path = 'imgs/dust_car_left.png'
elif self.orientation == AgentOrientation.RIGHT:
path = 'imgs/dust_car_right.png'
elif self.orientation == AgentOrientation.UP:
path = 'imgs/dust_car_up.png'
elif self.orientation == AgentOrientation.DOWN:
path = 'imgs/dust_car_down.png'
game_context.render_in_cell(self.position, path)

View File

@ -4,12 +4,18 @@ from PIL import Image
import pygame import pygame
from typing import Tuple, List from typing import Tuple, List
from street import Street, StreetType from street import Street, StreetType
from garbageTruck import GarbageTruck
def startup(game_context: GameContext): def startup(game_context: GameContext):
render_background(game_context) render_background(game_context)
game_context.city = create_city() game_context.city = create_city()
game_context.city.render_city(game_context) game_context.city.render_city(game_context)
game_context.canvas.blit(game_context.dust_car_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y)) car = create_dust_car(game_context)
car.render(game_context)
game_context.dust_car = car
def create_dust_car(game_context: GameContext) -> GarbageTruck:
return GarbageTruck((3, 3))
def render_background(game_context: GameContext): def render_background(game_context: GameContext):
bg_img = Image.open("imgs/background.jpg") bg_img = Image.open("imgs/background.jpg")