Restructure map class
21
game.py
|
@ -28,8 +28,6 @@ from map import Map
|
|||
map = Map(METER)
|
||||
map.load("map.json")
|
||||
|
||||
tilemap = map.map["map"]
|
||||
|
||||
import controller
|
||||
from player import Player
|
||||
player = Player()
|
||||
|
@ -58,14 +56,17 @@ while 1:
|
|||
psize = player.sprite.rect.size
|
||||
camera = [CENTER[0] - (psize[0] / 2 * SCALE), CENTER[1] - (psize[1] / 2 * SCALE)]
|
||||
|
||||
for row in range(len(tilemap)):
|
||||
for column in range(len(tilemap[row])):
|
||||
texture = assets.get(map.map["tiles"][tilemap[row][column]]["texture"])
|
||||
tilesize = texture.get_rect().size[0]
|
||||
screen.blit(pygame.transform.scale(texture, [SCALE * tilesize, SCALE * tilesize]), [
|
||||
column * SCALE * tilesize - (player.pos.x * SCALE * METER) + camera[0],
|
||||
row * SCALE * tilesize - (player.pos.y * SCALE * METER) + camera[1]
|
||||
])
|
||||
for layer in map.map["renderLayers"]:
|
||||
for row in range(len(layer)):
|
||||
for column in range(len(layer[row])):
|
||||
materialIndex = layer[row][column]
|
||||
if materialIndex != 0:
|
||||
texture = assets.get(map.map["tiles"][materialIndex - 1])
|
||||
tilesize = texture.get_rect().size[0]
|
||||
screen.blit(pygame.transform.scale(texture, [SCALE * tilesize, SCALE * tilesize]), [
|
||||
column * SCALE * tilesize - (player.pos.x * SCALE * METER) + camera[0],
|
||||
row * SCALE * tilesize - (player.pos.y * SCALE * METER) + camera[1]
|
||||
])
|
||||
|
||||
# Draw player based on camera position
|
||||
screen.blit(pygame.transform.scale(player.sprite.texture.frame, [SCALE * player.sprite.texture.width, SCALE * player.sprite.texture.height]), camera)
|
||||
|
|
85
map.json
|
@ -1,50 +1,45 @@
|
|||
{
|
||||
"tiles": [
|
||||
{
|
||||
"solid": false,
|
||||
"texture": "floor_cobble.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_down.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_right.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_up.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_left.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_corner_nw.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_corner_ne.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_corner_se.png"
|
||||
},
|
||||
{
|
||||
"solid": true,
|
||||
"texture": "wall_cobble_corner_sw.png"
|
||||
}
|
||||
"floor_cobble.png",
|
||||
"wall_cobble_down.png",
|
||||
"wall_cobble_right.png",
|
||||
"wall_cobble_up.png",
|
||||
"wall_cobble_left.png",
|
||||
"wall_cobble_corner_nw_inner.png",
|
||||
"wall_cobble_corner_ne_inner.png",
|
||||
"wall_cobble_corner_se_inner.png",
|
||||
"wall_cobble_corner_sw_inner.png"
|
||||
],
|
||||
"map": [
|
||||
[5, 1, 1, 1, 1, 1, 1, 6],
|
||||
[2, 0, 0, 0, 0, 0, 0, 4],
|
||||
[2, 0, 0, 0, 0, 0, 0, 4],
|
||||
[2, 0, 0, 0, 0, 0, 0, 4],
|
||||
[2, 0, 0, 0, 0, 0, 0, 4],
|
||||
[2, 0, 0, 0, 0, 0, 0, 4],
|
||||
[2, 0, 0, 0, 0, 0, 0, 4],
|
||||
[8, 3, 3, 3, 3, 3, 3, 7]
|
||||
"renderLayers": [
|
||||
[
|
||||
[0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0]
|
||||
],
|
||||
[
|
||||
[6, 2, 2, 2, 2, 2, 2, 7],
|
||||
[3, 0, 0, 0, 0, 0, 0, 5],
|
||||
[3, 0, 0, 0, 0, 0, 0, 5],
|
||||
[3, 0, 0, 0, 0, 0, 0, 5],
|
||||
[3, 0, 0, 0, 0, 0, 0, 5],
|
||||
[3, 0, 0, 0, 0, 0, 0, 5],
|
||||
[3, 0, 0, 0, 0, 0, 0, 5],
|
||||
[9, 4, 4, 4, 4, 4, 4, 8]
|
||||
]
|
||||
],
|
||||
"boundaries": [
|
||||
[1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1]
|
||||
]
|
||||
}
|
||||
|
|
14
map.py
|
@ -12,21 +12,15 @@ class Map:
|
|||
|
||||
def load(self, filename):
|
||||
with open(filename) as file:
|
||||
data = json.load(file)
|
||||
for tile in data["tiles"]:
|
||||
assets.load(tile["texture"])
|
||||
|
||||
self.map = data
|
||||
self.map = json.load(file)
|
||||
|
||||
def collides(self, pos, rect):
|
||||
for y in range(int(floor(pos.y)) - 1, int(floor(pos.y)) + 2):
|
||||
for x in range(int(floor(pos.x)) - 1, int(floor(pos.x)) + 2):
|
||||
if not (x == pos.x and y == pos.y):
|
||||
tile = self.map["tiles"][self.map["map"][y][x]]
|
||||
if tile["solid"]:
|
||||
rect2 = assets.get(tile["texture"]).get_rect()
|
||||
if pos.x + (rect.width / self.METER) >= x and pos.x <= (x + rect2.width / self.METER) and \
|
||||
pos.y + (rect.height / self.METER) >= y and pos.y <= (y + rect2.height / self.METER):
|
||||
if self.map["boundaries"][y][x] == 1:
|
||||
if pos.x + (rect.width / self.METER) >= x and pos.x <= (x + 1) and \
|
||||
pos.y + (rect.height / self.METER) >= y and pos.y <= (y + 1):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |