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-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-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
|
|
|
|
return map
|
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-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]
|
|
|
|
tile = Tile(pattern[tileId], offsetX, offsetY, TILE_SIZE_PX, TILE_SIZE_PX)
|
|
|
|
if tileId == 0:
|
|
|
|
roadTiles.add(tile)
|
|
|
|
else:
|
|
|
|
wallTiles.add(tile)
|
2022-04-08 02:17:02 +02:00
|
|
|
|
2022-04-08 01:34:11 +02:00
|
|
|
return roadTiles, wallTiles
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|