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
city = None
grid: Dict[Tuple[int, int], GridCellType] = {}
dust_car = None
def __init__(self) -> None:
self._init_grid()

View File

@ -2,23 +2,4 @@ import pygame
from gameContext import GameContext
def handle_game_event(event, game_context: GameContext):
dust_car_movement(event, game_context)
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))
pass

View File

@ -1,6 +1,7 @@
from typing import List, Tuple
from agentOrientation import AgentOrientation
from garbage import RecognizedGarbage
from gameContext import GameContext
class GarbageTruck:
position: Tuple[int, int]
@ -9,8 +10,9 @@ class GarbageTruck:
glass: List[RecognizedGarbage]
bio: List[RecognizedGarbage]
mixed: List[RecognizedGarbage]
truckSpritePath = 'imgs/dust_car.png'
def __init__(self, position: List[int, int]) -> None:
orientation: AgentOrientation = AgentOrientation.RIGHT
def __init__(self, position: Tuple[int, int]) -> None:
self.position = position
self.paper = []
self.plastic_and_metal = []
@ -34,3 +36,15 @@ class GarbageTruck:
elif RecognizedGarbage.garbage_type == 5:
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
from typing import Tuple, List
from street import Street, StreetType
from garbageTruck import GarbageTruck
def startup(game_context: GameContext):
render_background(game_context)
game_context.city = create_city()
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):
bg_img = Image.open("imgs/background.jpg")