[map_gen_refactor] apply new map generation system, huge code refactor

This commit is contained in:
czorekk 2022-04-23 19:30:06 +02:00
commit a088678c33
12 changed files with 129 additions and 12 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

8
.idea/WALL-E.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/WALL-E.iml" filepath="$PROJECT_DIR$/.idea/WALL-E.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

51
SearchBfs.py Normal file
View File

@ -0,0 +1,51 @@
class BreadthSearchAlgorithm:
def __init__(self, start, target, mapArray):
self.graph = self.getData(mapArray)
self.start = start
self.target = target
def bfs(self):
print("It's showtime")
can_go = [[self.start, 0]]
visited = []
visitedPrint = []
if self.start == self.target:
print("Start = Target")
return -1
while can_go != []:
node = can_go.pop(0)
if node[0] not in visited:
visited.append(node[0])
visitedPrint.append(node)
if node[0] == self.target:
# print('final')
# print(visitedPrint)
return visitedPrint
neighbours = self.graph.get(node[0], [])
for neighbour in neighbours:
can_go.append([neighbour, node[0]])
# print(visited)
return -1
def getData(self, mapArray):
with open("data.txt", "r") as f:
# matrix = [
# [int(x) for x in line.split(",") if x != "\n"] for line in f.readlines()
# ]
matrix = mapArray
adj = {}
for yi, yvalue in enumerate(matrix):
for xi, xvalue in enumerate(matrix):
if xi - 1 >= 0 and matrix[yi][xi - 1] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi - 1, yi)]
if xi + 1 < len(matrix[yi]) and matrix[yi][xi + 1] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi + 1, yi)]
if yi - 1 >= 0 and matrix[yi - 1][xi] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi, yi - 1)]
if yi + 1 < len(matrix) and matrix[yi + 1][xi] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi, yi + 1)]
return adj

6
data.txt Normal file
View File

@ -0,0 +1,6 @@
0,0,0,0,0,0
0,1,1,0,0,0
0,1,0,0,0,0
0,1,0,0,0,0
0,1,0,0,0,0
0,1,0,0,0,0

29
main.py
View File

@ -3,15 +3,19 @@ from game_objects.player import Player
import pygame as pg
import sys
from os import path
from map import *
# from agent import trashmaster
# from house import House
from settings import *
from map import map
from map import map_utils
from SearchBfs import *
import math
class Game():
def __init__(self):
pg.init()
self.clock = pg.time.Clock()
@ -19,12 +23,13 @@ class Game():
pg.display.set_caption("Trashmaster")
self.load_data()
self.init_game()
self.init_bfs()
def init_game(self):
# initialize all variables and do all the setup for a new game
# sprite groups
self.roadTiles, self.wallTiles = map.get_tiles()
# sprite groups and map array for calculations
(self.roadTiles, self.wallTiles), self.mapArray = map.get_tiles()
self.agentSprites = pg.sprite.Group()
# player obj
@ -36,6 +41,21 @@ class Game():
# other
self.draw_debug = False
def init_bfs(self):
start_node = (0, 0)
target_node = (18, 18)
find_path = BreadthSearchAlgorithm(start_node, target_node, self.mapArray)
path = find_path.bfs()
# print(path)
realPath = []
nextNode = target_node
for i in range(len(path)-1, 0, -1):
node = path[i]
if node[0] == nextNode:
realPath.insert(0, node[0])
nextNode = node[1]
print(realPath)
def load_data(self):
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'resources/textures')
@ -51,7 +71,7 @@ class Game():
self.events()
self.update()
self.draw()
def quit(self):
pg.quit()
sys.exit()
@ -105,4 +125,3 @@ g.show_start_screen()
while True:
g.run()
g.show_go_screen()

View File

@ -7,7 +7,7 @@ def get_tiles():
array = map_utils.generate_map()
pattern = map_pattern.get_pattern()
tiles = map_utils.get_sprites(array, pattern)
return tiles
return tiles, array
def render_tiles(tiles, screen, camera, debug=False):
for tile in tiles:

10
mapa.py
View File

@ -2,7 +2,6 @@ import pygame as pg
import pytmx
# config
# TILE_SIZE = 16
@ -16,15 +15,14 @@ import pytmx
# return surface
class TiledMap:
#loading file
# loading file
def __init__(self, filename):
tm = pytmx.load_pygame(filename, pixelalpha=True)
self.width = tm.width * tm.tilewidth
self.height = tm.height * tm.tileheight
self.tmxdata = tm
#rendering map
# rendering map
def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
@ -33,8 +31,8 @@ class TiledMap:
tile = ti(gid)
if tile:
surface.blit(tile, (x * self.tmxdata.tilewidth, y * self.tmxdata.tilewidth))
def make_map(self):
temp_surface = pg.Surface((self.width, self.height))
self.render(temp_surface)
return temp_surface
return temp_surface

8
ok Normal file
View File

@ -0,0 +1,8 @@
* bfsAlgorithm
master
remotes/origin/HEAD -> origin/master
remotes/origin/Mapa
remotes/origin/agent_movement
remotes/origin/bfsAlgorithm
remotes/origin/mapa2
remotes/origin/master