streets_rendering #14

Merged
s473616 merged 6 commits from streets_rendering into master 2023-04-03 19:52:09 +02:00
4 changed files with 37 additions and 13 deletions
Showing only changes of commit 4fe38e3afc - Show all commits

View File

@ -1,4 +1,4 @@
from typing import Tuple
from typing import Tuple, List
import pygame
from PIL import Image
@ -10,6 +10,7 @@ class GameContext:
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)
@ -18,11 +19,4 @@ class GameContext:
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))

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()

30
startup.py Normal file
View File

@ -0,0 +1,30 @@
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 = []
example_street = Street(1, 10, 2, StreetType.HORIZONTAL)
streets.append(example_street)
return streets

View File

@ -4,7 +4,7 @@ from gameContext import GameContext
class StreetType (Enum):
VERTICAL = 0
HORIZONTAL = 0
HORIZONTAL = 1
class Street:
street_type: StreetType
@ -12,14 +12,14 @@ class Street:
end_cell: int
row_or_column: int
def __init__(self, start_cell: int, end_cell: int, row_or_column: int, street_type: StreetType = StreetType.VERTICAL) -> None:
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):
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] = (i, self.row_or_column) if self.street_type == StreetType.VERTICAL else (self.row_or_column, i)
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)