feature: Add Types in tilemap

This commit is contained in:
ulazho 2023-05-22 10:18:32 +02:00
parent fb6511b05b
commit e39efc72bd
3 changed files with 56 additions and 35 deletions

View File

@ -5,46 +5,59 @@ class Field:
FieldSizeY = 0 FieldSizeY = 0
#Get value method
def __getitem__(self, pos): def __getitem__(self, pos):
X, Y = pos X, Y = pos
return self.Nodes[X][Y].Type return self.Nodes[X][Y].Value
def __setitem__(self, pos, value): def __setitem__(self, pos, value, Type = "Value"):
X, Y = pos X, Y = pos
self.Nodes[X][Y].Type = value if(Type == "Value"):
self.Nodes[X][Y].Value = value
else:
self.Nodes[X][Y].Type = value
def __init__(self, tilemap): def __init__(self, tilemap_types, tilemap_values):
self.FieldSizeX = len(tilemap) - 1 self.FieldSizeX = len(tilemap_types) - 1
self.FieldSizeY = len(tilemap[0]) - 1 self.FieldSizeY = len(tilemap_types[0]) - 1
if(tilemap != []): if(tilemap_types != []):
for x in range(0, self.FieldSizeX + 1): for x in range(0, self.FieldSizeX + 1):
self.Nodes.append([]) self.Nodes.append([])
for y in range(0, self.FieldSizeY + 1): for y in range(0, self.FieldSizeY + 1):
node = Node(x, y, tilemap[x][y]) node = Node(x, y, tilemap_types[x][y], tilemap_values[x][y])
self.Nodes[x].append(node) self.Nodes[x].append(node)
def get_type(self, pos):
X, Y = pos
return self.Nodes[X][Y].Type
def PrintField(self): def PrintFieldTypes(self):
for x in range(len(self.Nodes)): for x in range(len(self.Nodes)):
print() print()
for y in range(len(self.Nodes[0])): for y in range(len(self.Nodes[0])):
print(self.Nodes[x][y].Type, end=' ') print(self.Nodes[x][y].Type, end=' ')
def PrintFieldValues(self):
for x in range(len(self.Nodes)):
print()
for y in range(len(self.Nodes[0])):
print(self.Nodes[x][y].Values, end=' ')
def Neighbors(field, pos): def Neighbors(field, pos):
x, y, dir = pos x, y, dir = pos
neighbors = [] neighbors = []
if(x == 11): if(x == 11):
x = 11 x = 11
if(x > 0 and field[x - 1, y] != TREE and dir == 3): if(x > 0 and field.get_type([x - 1, y]) != TREE and dir == 3):
neighbors.append((x - 1, y, dir)) neighbors.append((x - 1, y, dir))
if(y > 0 and field[x, y - 1] != TREE and dir == 0): if(y > 0 and field.get_type([x, y - 1]) != TREE and dir == 0):
neighbors.append((x, y - 1, dir)) neighbors.append((x, y - 1, dir))
if(x < field.FieldSizeX and field[x + 1, y] != TREE and dir == 1): if(x < field.FieldSizeX and field.get_type([x + 1, y]) != TREE and dir == 1):
neighbors.append((x+1, y, dir)) neighbors.append((x+1, y, dir))
if(y < field.FieldSizeY and field[x, y+1] != TREE and dir == 2): if(y < field.FieldSizeY and field.get_type([x, y + 1]) != TREE and dir == 2):
neighbors.append((x, y+1, dir)) neighbors.append((x, y+1, dir))
neighbors.append((x, y, (dir + 1) % 4)) neighbors.append((x, y, (dir + 1) % 4))
if(dir == 0): if(dir == 0):
@ -57,16 +70,20 @@ class Node:
X = 0 X = 0
Y = 0 Y = 0
Type = None Type = None
Value = 0
def __init__(self, X, Y, Type): def __init__(self, X, Y, Type, Value):
self.X = X self.X = X
self.Y = Y self.Y = Y
self.Type = Type self.Type = Type
self.Value = Value
SHORT = 1 SHORT = 1
TALL = 80 TALL = 80
TREE = 100 TREE = 100
SHORT_VALUE = 1
TALL_VALUE = 100
TREE_VALUE = 100

View File

@ -37,13 +37,13 @@ Directions = {
3 : 'west' 3 : 'west'
} }
tiles = { tiles_types = {
SHORT: pygame.image.load('spritesNtiles/shortGrass64.png'), SHORT: pygame.image.load('spritesNtiles/shortGrass64.png'),
TALL: pygame.image.load('spritesNtiles/tallGrass64.png'), TALL: pygame.image.load('spritesNtiles/tallGrass64.png'),
TREE: pygame.image.load('spritesNtiles/dirtTree64.png') TREE: pygame.image.load('spritesNtiles/dirtTree64.png')
} }
tilemap = [ tilemap_types = [
[SHORT, SHORT, SHORT, TREE, SHORT, TALL, TALL, SHORT, SHORT, TREE, SHORT, SHORT, TALL, SHORT, SHORT], [SHORT, SHORT, SHORT, TREE, SHORT, TALL, TALL, SHORT, SHORT, TREE, SHORT, SHORT, TALL, SHORT, SHORT],
[SHORT, SHORT, SHORT, TALL, TALL, SHORT, SHORT, TREE, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT], [SHORT, SHORT, SHORT, TALL, TALL, SHORT, SHORT, TREE, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT],
[SHORT, SHORT, SHORT, SHORT, TALL, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT, TALL, SHORT, SHORT], [SHORT, SHORT, SHORT, SHORT, TALL, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT, SHORT, TALL, SHORT, SHORT],
@ -60,6 +60,21 @@ tilemap = [
] ]
#example tilemap values
tilemap_values = []
SHORT_VALUE = 5
TALL_VALUE = 100
TREE_VALUE = 50
for X in range(0, len(tilemap_types)):
tilemap_values.append([])
for Y in range(0, len(tilemap_types[X])):
if tilemap_types[X][Y] == SHORT: value = SHORT_VALUE
elif tilemap_types[X][Y] == TALL: value = TALL_VALUE
elif tilemap_types[X][Y] == TREE: value = TREE_VALUE
tilemap_values[X].append(value)
tilemapSizeX = 12 tilemapSizeX = 12
tilemapSizeY = 15 tilemapSizeY = 15
tileSize = 64 tileSize = 64
@ -70,18 +85,7 @@ tileSize = 64
disX = tilemapSizeX * tileSize disX = tilemapSizeX * tileSize
disY = tilemapSizeY * tileSize disY = tilemapSizeY * tileSize
# tilemap = [
# [0, 0, 0, 10, 0, 1, 1, 0, 0, 10],
# [0, 0, 0, 1, 1, 0, 0, 10, 0, 0],
# [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
# [1, 10, 0, 0, 0, 10, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 10, 0, 10],
# [0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
# [0, 0, 1, 0, 0, 0, 0, 0, 10, 0],
# [0, 10, 0, 10, 0, 0, 0, 0, 1, 1],
# [0, 0, 0, 0, 1, 0, 0, 0, 0, 10],
# [1, 0, 0, 0, 0, 0, 10, 0, 0, 0]
# ]
pygame.init() pygame.init()
@ -93,7 +97,7 @@ bee_x = 2
bee_y = 1 bee_y = 1
bee_dir = 'west' bee_dir = 'west'
field = Field(tilemap) field = Field(tilemap_types, tilemap_values)
@ -141,7 +145,7 @@ def A_star(field, start, end):
while True: while True:
random_x = random.randint(0, tilemapSizeX-2) random_x = random.randint(0, tilemapSizeX-2)
random_y = random.randint(0, tilemapSizeY-2) random_y = random.randint(0, tilemapSizeY-2)
if(field[random_x, random_y] != TREE): if(field.get_type([random_x, random_y]) != TREE):
break break
@ -175,8 +179,8 @@ while True:
dis.fill(white) dis.fill(white)
for x in range(tilemapSizeX): for x in range(tilemapSizeX):
for y in range(tilemapSizeY): for y in range(tilemapSizeY):
tile = tilemap[x][y] tile = tilemap_types[x][y]
dis.blit(tiles[tile], (x * tileSize, y * tileSize)) dis.blit(tiles_types[tile], (x * tileSize, y * tileSize))
# rotate and render the bee # rotate and render the bee
if bee_dir == 'west': if bee_dir == 'west':