2022-04-08 02:17:02 +02:00
|
|
|
import random
|
2022-04-08 01:34:11 +02:00
|
|
|
import pygame as pg
|
2022-04-08 02:17:02 +02:00
|
|
|
from settings import *
|
2022-04-23 19:14:23 +02:00
|
|
|
from map.tile import Tile
|
2022-05-10 01:05:34 +02:00
|
|
|
from map import map_pattern
|
|
|
|
from game_objects.trashbin import Trashbin
|
2022-04-08 01:34:11 +02:00
|
|
|
|
|
|
|
# tworzenie pustego arraya o podanych wymiarach
|
2022-04-23 19:14:23 +02:00
|
|
|
def get_blank_map_array():
|
2022-04-08 01:34:11 +02:00
|
|
|
map = [[0 for x in range(0,MAP_WIDTH)] for y in range (0,MAP_HEIGHT)]
|
|
|
|
return map
|
|
|
|
|
2022-04-08 02:17:02 +02:00
|
|
|
# generowanie obiektow na mapie
|
2022-04-23 19:14:23 +02:00
|
|
|
def generate_map():
|
|
|
|
map = get_blank_map_array()
|
2022-05-10 01:05:34 +02:00
|
|
|
|
|
|
|
# generowanie scian
|
2022-04-08 02:17:02 +02:00
|
|
|
for i in range(0, 20):
|
|
|
|
x = random.randint(0, MAP_WIDTH-1)
|
|
|
|
y = random.randint(0, MAP_HEIGHT-1)
|
|
|
|
map[y][x] = 1
|
2022-05-10 01:05:34 +02:00
|
|
|
|
|
|
|
# generowanie smietnikow
|
2022-05-13 08:31:40 +02:00
|
|
|
for i in range(0, 10):
|
2022-05-10 01:05:34 +02:00
|
|
|
x = random.randint(0, MAP_WIDTH-1)
|
|
|
|
y = random.randint(0, MAP_HEIGHT-1)
|
|
|
|
map[y][x] = 2
|
|
|
|
|
2022-04-08 02:17:02 +02:00
|
|
|
return map
|
2022-04-08 01:34:11 +02:00
|
|
|
|
2022-05-13 08:31:40 +02:00
|
|
|
trashbins =[]
|
|
|
|
|
2022-04-08 01:34:11 +02:00
|
|
|
# tworzenie grup sprite'ow
|
2022-04-23 19:14:23 +02:00
|
|
|
def get_sprites(map, pattern):
|
2022-04-08 01:34:11 +02:00
|
|
|
roadTiles = pg.sprite.Group()
|
|
|
|
wallTiles = pg.sprite.Group()
|
2022-05-10 01:05:34 +02:00
|
|
|
trashbinTiles = pg.sprite.Group()
|
|
|
|
|
|
|
|
trashbin_pattern = map_pattern.get_trashbin_pattern()
|
2022-04-08 01:34:11 +02:00
|
|
|
|
2022-04-08 02:17:02 +02:00
|
|
|
#objechanie tablicy i generowanie tile'a na danych kordach
|
2022-04-08 01:34:11 +02:00
|
|
|
for i in range(len(map)):
|
|
|
|
offsetY = i * TILE_SIZE_PX
|
|
|
|
for j in range(len(map[i])):
|
|
|
|
offsetX = j * TILE_SIZE_PX
|
|
|
|
tileId = map[i][j]
|
2022-05-10 01:05:34 +02:00
|
|
|
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)
|
2022-04-08 01:34:11 +02:00
|
|
|
roadTiles.add(tile)
|
2022-05-10 01:05:34 +02:00
|
|
|
trashbinTiles.add(trashbin)
|
2022-05-13 08:31:40 +02:00
|
|
|
trashbins.append(trashbin)
|
2022-05-10 01:05:34 +02:00
|
|
|
|
|
|
|
return roadTiles, wallTiles, trashbinTiles
|
2022-04-08 02:17:02 +02:00
|
|
|
|
2022-05-10 01:05:34 +02:00
|
|
|
def isRoadTile(tileId: int) -> bool:
|
|
|
|
if tileId == 0 or tileId == 2:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2022-04-08 01:34:11 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
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)
|
2022-04-28 21:28:03 +02:00
|
|
|
|
|
|
|
def offset(self):
|
|
|
|
x, y = self.camera.topleft
|
|
|
|
return x//TILE_SIZE_PX, y//TILE_SIZE_PX
|
2022-04-23 19:14:23 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-04-08 01:34:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-08 02:17:02 +02:00
|
|
|
|