Compare commits

..

7 Commits

Author SHA1 Message Date
9841406878 Merge pull request 'streets_rendering' (#14) from streets_rendering into master
Reviewed-on: #14
2023-04-03 19:52:09 +02:00
Pawel Felcyn
693121ac00 city streets rendering 2023-04-03 19:51:06 +02:00
Pawel Felcyn
4fe38e3afc render exapmple street 2023-04-03 19:26:56 +02:00
Pawel Felcyn
b8280a3854 add render method in city class 2023-04-03 15:49:28 +02:00
Pawel Felcyn
ba49049cc3 add street class 2023-04-03 15:43:03 +02:00
Pawel Felcyn
51fdebbc00 add imgs of street 2023-04-03 15:29:36 +02:00
Pawel Felcyn
9f0cdbc5ff add method for rendering img in cell 2023-04-03 15:29:22 +02:00
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 garbageCan import GarbageCan
from street import Street
from gameContext import GameContext
class Node: class Node:
garbageCan: GarbageCan garbageCan: GarbageCan
id: int id: int
def __init__(self, id: int, can: GarbageCan) -> None: def __init__(self, id: int, can: GarbageCan) -> None:
self.id = id self.id
self.can = can self.can = can
class City: class City:
nodes: List[Node] nodes: List[Node]
streets: List[Tuple[int, int]] streets: List[Street]
def __init__(self) -> None: def __init__(self) -> None:
self.nodes = [] self.nodes = []
@ -20,20 +22,12 @@ class City:
def add_node(self, node: Node) -> None: def add_node(self, node: Node) -> None:
self.nodes.append(node) self.nodes.append(node)
def add_street(self, street: Tuple[int, int]) -> None: def add_street(self, street: Street) -> 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
self.streets.append(street) 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 import pygame
from PIL import Image from PIL import Image
@ -8,12 +9,14 @@ class GameContext:
dust_car_pygame = None dust_car_pygame = None
dust_car_pil = None dust_car_pil = None
canvas = 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 import pygame
from gameEventHandler import handle_game_event from gameEventHandler import handle_game_event
from gameContext import GameContext from gameContext import GameContext
from gameContext import startup from startup import startup
from PIL import Image from PIL import Image
pygame.init() 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)