add house class and simple test with draw it on field #8

Merged
s462072 merged 1 commits from house_class into master 2022-03-25 11:48:51 +01:00
2 changed files with 43 additions and 7 deletions

29
house.py Normal file
View File

@ -0,0 +1,29 @@
import pygame.image
from typing import Optional
import os
from random import randint
class House(pygame.sprite.Sprite):
def __init__(self, x:int, y:int,
number_of_bins:int = 0, if_exist: bool = False):
super().__init__()
self.number_of_bins = number_of_bins
self.x = x
self.y = y
self.if_exist = if_exist
self.bin_id: Optional[int]
self.base_path = "./resources/textures/buliding/"
# Statyczny sprite domku, obecnie randomny
# self.image = pygame.image.load("./resources/textures/buliding/GTA2_TILE_112.bmp")
self.image = pygame.image.load(f"{self.base_path}{self.get_random_house_texture()}")
def get_random_house_texture(self):
files = os.listdir(self.base_path)
value = randint(0, len(files)-1)
return files[value]

19
main.py
View File

@ -1,7 +1,7 @@
import pygame import pygame
from map import preparedMap from map import preparedMap
from agent import trashmaster from agent import trashmaster
from house import House
class WalleGame(): class WalleGame():
@ -23,9 +23,10 @@ class WalleGame():
def update_window(self): def update_window(self):
pygame.display.update() pygame.display.update()
def draw_trashmaster(self, smieciara: trashmaster, pos): def draw_object(self, drawable_object, pos):
# pos => (x, y) # pos => (x, y)
self.screen.blit(smieciara.image, pos ) # drawable object must have .image field inside class
self.screen.blit(drawable_object.image, pos )
def reloadMap(self): def reloadMap(self):
self.screen.fill(pygame.Color(self.BACKGROUND_COLOR)) self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
@ -36,7 +37,11 @@ def main():
game.update_window() game.update_window()
smieciara_object = trashmaster(16,16,"./resources/textures/garbagetruck/trashmaster_blu.png") smieciara_object = trashmaster(16,16,"./resources/textures/garbagetruck/trashmaster_blu.png")
game.draw_trashmaster(smieciara_object, (0, 0)) game.draw_object(smieciara_object, (0, 0))
house_object = House(20, 20)
# Test draw house object
game.draw_object(house_object, (20,20))
game.update_window() game.update_window()
@ -48,9 +53,11 @@ def main():
running = False running = False
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
game.reloadMap() game.reloadMap()
game.draw_trashmaster(smieciara_object, game.draw_object(smieciara_object,
smieciara_object.movement(event.key, 16)) smieciara_object.movement(event.key, 16))
game.update_window() game.update_window()
pygame.quit() pygame.quit()