Merge pull request 'streets_rendering' (#14) from streets_rendering into master

Reviewed-on: #14
This commit is contained in:
Paweł Felcyn 2023-04-03 19:52:09 +02:00
commit 9841406878
7 changed files with 87 additions and 27 deletions

32
city.py
View File

@ -1,17 +1,19 @@
from typing import List, Tuple
from typing import List
from garbageCan import GarbageCan
from street import Street
from gameContext import GameContext
class Node:
garbageCan: GarbageCan
id: int
def __init__(self, id: int, can: GarbageCan) -> None:
self.id = id
self.id
self.can = can
class City:
nodes: List[Node]
streets: List[Tuple[int, int]]
streets: List[Street]
def __init__(self) -> None:
self.nodes = []
@ -20,20 +22,12 @@ class City:
def add_node(self, node: Node) -> None:
self.nodes.append(node)
def add_street(self, street: Tuple[int, int]) -> None:
firstFound: bool = False
secondFound: bool = False
for node in self.nodes:
if firstFound and secondFound:
break
if node.id == street.__getitem__(0):
firstFound = True
continue
if node.id == street.__getitem__(1):
secondFound = True
if not firstFound or not secondFound:
return
def add_street(self, street: Street) -> None:
self.streets.append(street)
def render_city(self, game_context: GameContext) -> None:
self._render_streets(game_context)
def _render_streets(self, game_context: GameContext) -> None:
for street in self.streets:
street.render(game_context)

View File

@ -1,3 +1,4 @@
from typing import Tuple, List
import pygame
from PIL import Image
@ -8,12 +9,14 @@ class GameContext:
dust_car_pygame = None
dust_car_pil = None
canvas = None
_cell_size: int = 30
city = None
def render_in_cell(self, cell: Tuple[int, int], img_path: str):
img = Image.open(img_path)
pygame_img = pygame.image.frombuffer(img.tobytes(), (self._cell_size,self._cell_size), 'RGB')
start_x = (cell[0] - 1) * self._cell_size
start_y = (cell[1] - 1) * self._cell_size
self.canvas.blit(pygame_img, (start_x, start_y))
def startup(game_context: GameContext):
render_background(game_context)
game_context.canvas.blit(game_context.dust_car_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))
def render_background(game_context: GameContext):
bg_img = Image.open("imgs/background.jpg")
pygame_bg_image = pygame.image.frombuffer(bg_img.tobytes(), bg_img.size, 'RGB')
game_context.canvas.blit(pygame_bg_image, (0, 0))

BIN
imgs/street_horizontal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

BIN
imgs/street_vertical.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 B

View File

@ -1,7 +1,7 @@
import pygame
from gameEventHandler import handle_game_event
from gameContext import GameContext
from gameContext import startup
from startup import startup
from PIL import Image
pygame.init()

38
startup.py Normal file
View File

@ -0,0 +1,38 @@
from gameContext import GameContext
from city import City
from PIL import Image
import pygame
from typing import Tuple, List
from street import Street, StreetType
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))
def render_background(game_context: GameContext):
bg_img = Image.open("imgs/background.jpg")
pygame_bg_image = pygame.image.frombuffer(bg_img.tobytes(), bg_img.size, 'RGB')
game_context.canvas.blit(pygame_bg_image, (0, 0))
def create_city() -> City:
city: City = City()
streets = create_streets()
for s in streets:
city.add_street(s)
return city
def create_streets() -> List[Street]:
streets = []
streets.append(Street(3, 30, 3, StreetType.HORIZONTAL))
streets.append(Street(4, 15, 10, StreetType.VERTICAL))
streets.append(Street(4, 10, 25, StreetType.VERTICAL))
streets.append(Street(11, 24, 10, StreetType.HORIZONTAL))
streets.append(Street(4, 9, 18, StreetType.VERTICAL))
streets.append(Street(1, 30, 16, StreetType.HORIZONTAL))
streets.append(Street(17, 30, 2, StreetType.VERTICAL))
streets.append(Street(3, 25, 23, StreetType.HORIZONTAL))
streets.append(Street(17, 30, 13, StreetType.VERTICAL))
streets.append(Street(17, 23, 25, StreetType.VERTICAL))
return streets

25
street.py Normal file
View File

@ -0,0 +1,25 @@
from enum import Enum
from typing import Tuple
from gameContext import GameContext
class StreetType (Enum):
VERTICAL = 0
HORIZONTAL = 1
class Street:
street_type: StreetType
start_cell: int
end_cell: int
row_or_column: int
def __init__(self, start_cell: int, end_cell: int, row_or_column: int, street_type: StreetType) -> None:
self.start_cell = start_cell
self.end_cell = end_cell
self.street_type = street_type
self.row_or_column = row_or_column
def render(self, game_context: GameContext) -> None:
for i in range(self.start_cell, self.end_cell + 1):
img_str: str = 'imgs/street_vertical.png' if self.street_type == StreetType.VERTICAL else 'imgs/street_horizontal.png'
cell: Tuple[int, int] = (self.row_or_column, i) if self.street_type == StreetType.VERTICAL else (i, self.row_or_column)
game_context.render_in_cell(cell, img_str)