trashbin-rendering #23

Merged
s464843 merged 3 commits from trashbin-rendering into master 2022-05-10 12:08:35 +02:00
13 changed files with 68 additions and 62 deletions

View File

@ -1,29 +1,23 @@
import pygame as pg
from enum import Enum
class trashbin(pg.sprite.Sprite):
from map.tile import Tile
def __init__(self,x,y,img, type):
super().__init__()
class Waste_Type(Enum):
BIO = 0
GLASS = 1
PLASTIC = 2
PAPER = 3
MIX = 4
# trashbin type
self.type = type
def __int__(self):
return self.value
class Trashbin(Tile):
def __init__(self, img, x, y, width, height, waste_type: Waste_Type):
super().__init__(img, x, y, width, height)
# dimensions
if type == "small":
self.width = 4
self.height = 4
elif type == "medium":
self.width = 8
self.height = 8
elif type == "large":
self.width = 16
self.height = 16
# spawn coords
self.x = x
self.y = y
# load trashbin image
self.image = pg.image.load(img)
self.image = pg.transform.scale(self.image, (self.x,self.y))
self.rect = self.image.get_rect()
self.waste_type = waste_type
self.days_after_pickup = 0
self.max_capacity = 100
self.used_capacity = 0
self.access = True

View File

@ -32,7 +32,7 @@ class Game():
# initialize all variables and do all the setup for a new game
# sprite groups and map array for calculations
(self.roadTiles, self.wallTiles), self.mapArray = map.get_tiles()
(self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles()
self.agentSprites = pg.sprite.Group()
# player obj
@ -102,6 +102,7 @@ class Game():
#rerender map
map.render_tiles(self.roadTiles, self.screen, self.camera)
map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode)
map.render_tiles(self.trashbinTiles, self.screen, self.camera)
#rerender additional sprites
for sprite in self.agentSprites:

View File

@ -1,24 +0,0 @@
from settings import *
class Camera:
def __init__(self,width,height):
self.camera = pg.Rect(0,0, width, height)
self.width = width
self.height = height
def apply(self,entity):
return entity.rect.move(self.camera.topleft)
def apply_rect(self, rect):
return rect.move(self.camera.topleft)
def update(self,target):
x = -target.rect.x + int(WIDTH/2)
y = -target.rect.y + int(HEIGHT / 2)
# limit scrolling to map size
x = min(0, x) # left
y = min(0, y) # top
x = max(-(self.width - WIDTH), x) # right
y = max(-(self.height - HEIGHT), y) # bottom
self.camera = pg.Rect(x, y, self.width, self.height)

View File

@ -8,10 +8,19 @@ ROAD_DIR = TEXTURES_DIR+"\\road\\"
BUILDING_DIR = TEXTURES_DIR+"\\buliding\\"
def load_img(path):
return pg.image.load(path)
return pg.image.load(path).convert_alpha()
def get_pattern():
return {
0: load_img("resources/textures/road/GTA2_TILE_257.bmp"),
1: load_img("resources/textures/buliding/GTA2_TILE_187.bmp"),
1: load_img("resources/textures/buliding/GTA2_TILE_187.bmp")
}
def get_trashbin_pattern():
return {
0: load_img("resources/textures/misc/trash_bin_small_bio.png"),
1: load_img("resources/textures/misc/trash_bin_small_glass.png"),
2: load_img("resources/textures/misc/trash_bin_small_plastic.png"),
3: load_img("resources/textures/misc/trash_bin_small_paper.png"),
4: load_img("resources/textures/misc/trash_bin_small.png")
}

View File

@ -2,6 +2,8 @@ import random
import pygame as pg
from settings import *
from map.tile import Tile
from map import map_pattern
from game_objects.trashbin import Trashbin
# tworzenie pustego arraya o podanych wymiarach
def get_blank_map_array():
@ -11,16 +13,28 @@ def get_blank_map_array():
# generowanie obiektow na mapie
def generate_map():
map = get_blank_map_array()
# generowanie scian
for i in range(0, 20):
x = random.randint(0, MAP_WIDTH-1)
y = random.randint(0, MAP_HEIGHT-1)
map[y][x] = 1
# generowanie smietnikow
for i in range(0, 5):
x = random.randint(0, MAP_WIDTH-1)
y = random.randint(0, MAP_HEIGHT-1)
map[y][x] = 2
return map
# tworzenie grup sprite'ow
def get_sprites(map, pattern):
roadTiles = pg.sprite.Group()
wallTiles = pg.sprite.Group()
trashbinTiles = pg.sprite.Group()
trashbin_pattern = map_pattern.get_trashbin_pattern()
#objechanie tablicy i generowanie tile'a na danych kordach
for i in range(len(map)):
@ -28,13 +42,26 @@ def get_sprites(map, pattern):
for j in range(len(map[i])):
offsetX = j * TILE_SIZE_PX
tileId = map[i][j]
tile = Tile(pattern[tileId], offsetX, offsetY, TILE_SIZE_PX, TILE_SIZE_PX)
if tileId == 0:
if tileId == 0 or tileId == 1:
tile = Tile(pattern[tileId], offsetX, offsetY, TILE_SIZE_PX, TILE_SIZE_PX)
if tileId == 0:
roadTiles.add(tile)
else:
wallTiles.add(tile)
elif tileId == 2:
trashbinId = random.randint(0, 4)
tile = Tile(pattern[0], offsetX, offsetY, TILE_SIZE_PX, TILE_SIZE_PX)
trashbin = Trashbin(trashbin_pattern[trashbinId], offsetX, offsetY, 32, 30, trashbinId)
roadTiles.add(tile)
else:
wallTiles.add(tile)
trashbinTiles.add(trashbin)
return roadTiles, wallTiles
return roadTiles, wallTiles, trashbinTiles
def isRoadTile(tileId: int) -> bool:
if tileId == 0 or tileId == 2:
return True
else:
return False
class Camera:
def __init__(self,width,height):

View File

@ -10,7 +10,7 @@ class Tile(pg.sprite.Sprite):
self.width = width
self.height = height
self.image = pg.Surface([width, height])
self.image = pg.Surface([width, height], pg.SRCALPHA, 32)
self.image.blit(img, (0,0))
self.rect = pg.Rect(x, y, width, height)

View File

@ -1,9 +1,8 @@
from enum import Enum
from map import map_utils
from settings import *
ROAD_TILE = 0
class Rotation(Enum):
UP = 0
RIGHT = 1
@ -36,7 +35,7 @@ def get_neighbours(node, searched_list, array):
y = node.y + offset_y
# prevent out of map coords
if (x >= 0 and x < MAP_WIDTH and y >= 0 and y < MAP_HEIGHT):
if(array[y][x] == ROAD_TILE and (x, y) not in searched_list):
if(map_utils.isRoadTile(array[y][x]) and (x, y) not in searched_list):
neighbour = Node(x, y, Rotation.NONE)
neighbour.rotation = get_needed_rotation(node, neighbour)
neighbours.append(neighbour)

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB