diff --git a/src/Field.py b/src/Field.py index 4f4fd92e..adc0a04d 100644 --- a/src/Field.py +++ b/src/Field.py @@ -5,46 +5,59 @@ class Field: FieldSizeY = 0 - + #Get value method def __getitem__(self, 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 - 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): - self.FieldSizeX = len(tilemap) - 1 - self.FieldSizeY = len(tilemap[0]) - 1 - if(tilemap != []): + def __init__(self, tilemap_types, tilemap_values): + self.FieldSizeX = len(tilemap_types) - 1 + self.FieldSizeY = len(tilemap_types[0]) - 1 + if(tilemap_types != []): for x in range(0, self.FieldSizeX + 1): self.Nodes.append([]) 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) - + + 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)): print() for y in range(len(self.Nodes[0])): 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): x, y, dir = pos neighbors = [] if(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)) - 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)) - 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)) - 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, (dir + 1) % 4)) if(dir == 0): @@ -57,16 +70,20 @@ class Node: X = 0 Y = 0 Type = None + Value = 0 - def __init__(self, X, Y, Type): + def __init__(self, X, Y, Type, Value): self.X = X self.Y = Y self.Type = Type + self.Value = Value SHORT = 1 TALL = 80 TREE = 100 - +SHORT_VALUE = 1 +TALL_VALUE = 100 +TREE_VALUE = 100 diff --git a/src/__pycache__/Field.cpython-310.pyc b/src/__pycache__/Field.cpython-310.pyc index 0f50fc25..70c09d4d 100644 Binary files a/src/__pycache__/Field.cpython-310.pyc and b/src/__pycache__/Field.cpython-310.pyc differ diff --git a/src/main.py b/src/main.py index 08c50e51..81660f83 100644 --- a/src/main.py +++ b/src/main.py @@ -37,13 +37,13 @@ Directions = { 3 : 'west' } -tiles = { +tiles_types = { SHORT: pygame.image.load('spritesNtiles/shortGrass64.png'), TALL: pygame.image.load('spritesNtiles/tallGrass64.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, 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], @@ -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 tilemapSizeY = 15 tileSize = 64 @@ -70,18 +85,7 @@ tileSize = 64 disX = tilemapSizeX * 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() @@ -93,7 +97,7 @@ bee_x = 2 bee_y = 1 bee_dir = 'west' -field = Field(tilemap) +field = Field(tilemap_types, tilemap_values) @@ -141,7 +145,7 @@ def A_star(field, start, end): while True: random_x = random.randint(0, tilemapSizeX-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 @@ -175,8 +179,8 @@ while True: dis.fill(white) for x in range(tilemapSizeX): for y in range(tilemapSizeY): - tile = tilemap[x][y] - dis.blit(tiles[tile], (x * tileSize, y * tileSize)) + tile = tilemap_types[x][y] + dis.blit(tiles_types[tile], (x * tileSize, y * tileSize)) # rotate and render the bee if bee_dir == 'west':