map_gen_refactor #18

Merged
s462072 merged 2 commits from map_gen_refactor into master 2022-04-23 19:30:50 +02:00
12 changed files with 129 additions and 12 deletions
Showing only changes of commit a088678c33 - Show all commits

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

25
main.py
View File

@ -3,13 +3,17 @@ from game_objects.player import Player
import pygame as pg import pygame as pg
import sys import sys
from os import path from os import path
from map import *
# from agent import trashmaster # from agent import trashmaster
# from house import House # from house import House
from settings import * from settings import *
from map import map from map import map
from map import map_utils from map import map_utils
from SearchBfs import *
import math import math
class Game(): class Game():
def __init__(self): def __init__(self):
@ -19,12 +23,13 @@ class Game():
pg.display.set_caption("Trashmaster") pg.display.set_caption("Trashmaster")
self.load_data() self.load_data()
self.init_game() self.init_game()
self.init_bfs()
def init_game(self): def init_game(self):
# initialize all variables and do all the setup for a new game # initialize all variables and do all the setup for a new game
# sprite groups # sprite groups and map array for calculations
self.roadTiles, self.wallTiles = map.get_tiles() (self.roadTiles, self.wallTiles), self.mapArray = map.get_tiles()
self.agentSprites = pg.sprite.Group() self.agentSprites = pg.sprite.Group()
# player obj # player obj
@ -36,6 +41,21 @@ class Game():
# other # other
self.draw_debug = False 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): def load_data(self):
game_folder = path.dirname(__file__) game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'resources/textures') img_folder = path.join(game_folder, 'resources/textures')
@ -105,4 +125,3 @@ g.show_start_screen()
while True: while True:
g.run() g.run()
g.show_go_screen() g.show_go_screen()

View File

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

View File

@ -2,7 +2,6 @@ import pygame as pg
import pytmx import pytmx
# config # config
# TILE_SIZE = 16 # TILE_SIZE = 16
@ -23,7 +22,6 @@ class TiledMap:
self.height = tm.height * tm.tileheight self.height = tm.height * tm.tileheight
self.tmxdata = tm self.tmxdata = tm
# rendering map # rendering map
def render(self, surface): def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid ti = self.tmxdata.get_tile_image_by_gid

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