Merge pull request 'bfsAlgorithm' (#14) from bfsAlgorithm into master
Reviewed-on: #14
This commit is contained in:
commit
e55ae78366
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
8
.idea/WALL-E.iml
Normal file
8
.idea/WALL-E.iml
Normal 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>
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal 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
4
.idea/misc.xml
Normal 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
8
.idea/modules.xml
Normal 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
6
.idea/vcs.xml
Normal 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>
|
50
SearchBfs.py
Normal file
50
SearchBfs.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
class BreadthSearchAlgorithm:
|
||||||
|
def __init__(self, start, target):
|
||||||
|
self.graph = self.getData()
|
||||||
|
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 visited
|
||||||
|
neighbours = self.graph.get(node[0], [])
|
||||||
|
for neighbour in neighbours:
|
||||||
|
can_go.append([neighbour, node[0]])
|
||||||
|
# print(visited)
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def getData(self):
|
||||||
|
with open("data.txt", "r") as f:
|
||||||
|
matrix = [
|
||||||
|
[int(x) for x in line.split(",") if x != "\n"] for line in f.readlines()
|
||||||
|
]
|
||||||
|
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
|
35
agent.py
35
agent.py
@ -1,30 +1,43 @@
|
|||||||
import pygame.image
|
import pygame.image
|
||||||
|
|
||||||
|
|
||||||
class trashmaster(pygame.sprite.Sprite):
|
class trashmaster(pygame.sprite.Sprite):
|
||||||
|
|
||||||
def __init__(self,x,y,img):
|
def __init__(self, x, y, img):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.width=x
|
self.width = x
|
||||||
self.height=y
|
self.height = y
|
||||||
|
|
||||||
self.x = 0
|
self.x = 0
|
||||||
self.y = 0
|
self.y = 0
|
||||||
|
|
||||||
self.image = pygame.image.load(img)
|
self.image = pygame.image.load(img)
|
||||||
self.image = pygame.transform.scale(self.image, (self.width,self.height))
|
self.image = pygame.transform.scale(self.image, (self.width, self.height))
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
def movement(self, key, vel):
|
def movement(self, key, vel):
|
||||||
if key == pygame.K_LEFT:
|
if key == pygame.K_LEFT:
|
||||||
self.x -= vel
|
self.x -= vel
|
||||||
|
|
||||||
if key == pygame.K_RIGHT:
|
if key == pygame.K_RIGHT:
|
||||||
self.x += vel
|
self.x += vel
|
||||||
|
|
||||||
if key == pygame.K_UP:
|
if key == pygame.K_UP:
|
||||||
self.y -= vel
|
self.y -= vel
|
||||||
|
|
||||||
if key == pygame.K_DOWN:
|
if key == pygame.K_DOWN:
|
||||||
self.y += vel
|
self.y += vel
|
||||||
return (self.x, self.y)
|
return (self.x, self.y)
|
||||||
|
|
||||||
|
def move_up(self):
|
||||||
|
self.y -= 64
|
||||||
|
|
||||||
|
def move_down(self):
|
||||||
|
self.y += 64
|
||||||
|
|
||||||
|
def move_right(self):
|
||||||
|
self.x += 64
|
||||||
|
|
||||||
|
def move_left(self):
|
||||||
|
self.x -= 64
|
||||||
|
6
data.txt
Normal file
6
data.txt
Normal 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
|
40
main.py
40
main.py
@ -2,22 +2,26 @@ from calendar import c
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
import sys
|
import sys
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
|
import SearchBfs
|
||||||
from map import *
|
from map import *
|
||||||
# from agent import trashmaster
|
# from agent import trashmaster
|
||||||
# from house import House
|
# from house import House
|
||||||
from sprites import *
|
from sprites import *
|
||||||
from settings import *
|
from settings import *
|
||||||
|
from SearchBfs import *
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
class Game():
|
class Game():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pg.init()
|
pg.init()
|
||||||
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
||||||
pg.display.set_caption("Trashmaster")
|
pg.display.set_caption("Trashmaster")
|
||||||
self.clock = pg.time.Clock()
|
self.clock = pg.time.Clock()
|
||||||
self.load_data()
|
self.load_data()
|
||||||
|
|
||||||
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')
|
||||||
@ -26,8 +30,8 @@ class Game():
|
|||||||
self.map_img = self.map.make_map()
|
self.map_img = self.map.make_map()
|
||||||
self.map_rect = self.map_img.get_rect()
|
self.map_rect = self.map_img.get_rect()
|
||||||
|
|
||||||
self.player_img = pg.image.load(path.join(img_folder,PLAYER_IMG)).convert_alpha()
|
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
||||||
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH,PLAYER_HEIGHT) )
|
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
||||||
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
|
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
|
||||||
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
|
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
|
||||||
|
|
||||||
@ -46,8 +50,8 @@ class Game():
|
|||||||
# self.screen.blit(self.map_img, (0,0))
|
# self.screen.blit(self.map_img, (0,0))
|
||||||
self.camera = Camera(self.map.width, self.map.height)
|
self.camera = Camera(self.map.width, self.map.height)
|
||||||
self.draw_debug = False
|
self.draw_debug = False
|
||||||
|
|
||||||
#self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
# self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# game loop - set self.playing = False to end the game
|
# game loop - set self.playing = False to end the game
|
||||||
@ -57,7 +61,7 @@ class Game():
|
|||||||
self.events()
|
self.events()
|
||||||
self.update()
|
self.update()
|
||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
pg.quit()
|
pg.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
@ -67,14 +71,13 @@ class Game():
|
|||||||
self.all_sprites.update()
|
self.all_sprites.update()
|
||||||
self.camera.update(self.player)
|
self.camera.update(self.player)
|
||||||
# pygame.display.update()
|
# pygame.display.update()
|
||||||
|
|
||||||
def draw_grid(self):
|
def draw_grid(self):
|
||||||
for x in range(0, WIDTH, TILESIZE):
|
for x in range(0, WIDTH, TILESIZE):
|
||||||
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
||||||
for y in range(0, HEIGHT, TILESIZE):
|
for y in range(0, HEIGHT, TILESIZE):
|
||||||
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
||||||
|
|
||||||
|
|
||||||
# def draw(self, drawable_object, pos):
|
# def draw(self, drawable_object, pos):
|
||||||
# # pos => (x, y)
|
# # pos => (x, y)
|
||||||
# # drawable object must have .image field inside class
|
# # drawable object must have .image field inside class
|
||||||
@ -83,7 +86,7 @@ class Game():
|
|||||||
def draw(self):
|
def draw(self):
|
||||||
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
||||||
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
||||||
|
|
||||||
for sprite in self.all_sprites:
|
for sprite in self.all_sprites:
|
||||||
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
||||||
if self.draw_debug:
|
if self.draw_debug:
|
||||||
@ -91,7 +94,7 @@ class Game():
|
|||||||
if self.draw_debug:
|
if self.draw_debug:
|
||||||
for wall in self.walls:
|
for wall in self.walls:
|
||||||
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(wall.rect), 1)
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(wall.rect), 1)
|
||||||
|
|
||||||
pg.display.flip()
|
pg.display.flip()
|
||||||
|
|
||||||
def events(self):
|
def events(self):
|
||||||
@ -117,11 +120,12 @@ class Game():
|
|||||||
# def reloadMap(self):
|
# def reloadMap(self):
|
||||||
# #self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
|
# #self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
|
||||||
# self.screen.blit(self.map_img, (0,0))
|
# self.screen.blit(self.map_img, (0,0))
|
||||||
|
|
||||||
|
|
||||||
# def main():
|
# def main():
|
||||||
# game = WalleGame()
|
# game = WalleGame()
|
||||||
# game.update_window()
|
# game.update_window()
|
||||||
|
|
||||||
# smieciara_object = trashmaster(16,16,"./resources/textures/garbagetruck/trashmaster_blu.png")
|
# smieciara_object = trashmaster(16,16,"./resources/textures/garbagetruck/trashmaster_blu.png")
|
||||||
# game.draw_object(smieciara_object, (100, 100))
|
# game.draw_object(smieciara_object, (100, 100))
|
||||||
|
|
||||||
@ -132,7 +136,7 @@ class Game():
|
|||||||
# game.update_window()
|
# game.update_window()
|
||||||
|
|
||||||
# running = True
|
# running = True
|
||||||
|
|
||||||
# while running:
|
# while running:
|
||||||
# for event in pygame.event.get():
|
# for event in pygame.event.get():
|
||||||
# if event.type == pygame.QUIT:
|
# if event.type == pygame.QUIT:
|
||||||
@ -140,19 +144,21 @@ class Game():
|
|||||||
# if event.type == pygame.KEYDOWN:
|
# if event.type == pygame.KEYDOWN:
|
||||||
# game.reloadMap()
|
# game.reloadMap()
|
||||||
# game.draw_object(smieciara_object, smieciara_object.movement(event.key, 16))
|
# game.draw_object(smieciara_object, smieciara_object.movement(event.key, 16))
|
||||||
|
|
||||||
# game.update_window()
|
# game.update_window()
|
||||||
|
|
||||||
# pygame.quit()
|
# pygame.quit()
|
||||||
# if __name__ == '__main__':
|
# if __name__ == '__main__':
|
||||||
# main()
|
# main()
|
||||||
|
|
||||||
|
start_node = (0, 0)
|
||||||
|
target_node = (2, 2)
|
||||||
|
find_path = BreadthSearchAlgorithm(start_node, target_node)
|
||||||
# create the game object
|
# create the game object
|
||||||
g = Game()
|
g = Game()
|
||||||
g.show_start_screen()
|
g.show_start_screen()
|
||||||
while True:
|
while True:
|
||||||
g.new()
|
g.new()
|
||||||
|
path_found = find_path.bfs()
|
||||||
g.run()
|
g.run()
|
||||||
g.show_go_screen()
|
g.show_go_screen()
|
||||||
|
|
||||||
|
10
mapa.py
10
mapa.py
@ -2,7 +2,6 @@ import pygame as pg
|
|||||||
import pytmx
|
import pytmx
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# config
|
# config
|
||||||
# TILE_SIZE = 16
|
# TILE_SIZE = 16
|
||||||
|
|
||||||
@ -16,15 +15,14 @@ import pytmx
|
|||||||
# return surface
|
# return surface
|
||||||
|
|
||||||
class TiledMap:
|
class TiledMap:
|
||||||
#loading file
|
# loading file
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
tm = pytmx.load_pygame(filename, pixelalpha=True)
|
tm = pytmx.load_pygame(filename, pixelalpha=True)
|
||||||
self.width = tm.width * tm.tilewidth
|
self.width = tm.width * tm.tilewidth
|
||||||
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
|
||||||
for layer in self.tmxdata.visible_layers:
|
for layer in self.tmxdata.visible_layers:
|
||||||
@ -33,8 +31,8 @@ class TiledMap:
|
|||||||
tile = ti(gid)
|
tile = ti(gid)
|
||||||
if tile:
|
if tile:
|
||||||
surface.blit(tile, (x * self.tmxdata.tilewidth, y * self.tmxdata.tilewidth))
|
surface.blit(tile, (x * self.tmxdata.tilewidth, y * self.tmxdata.tilewidth))
|
||||||
|
|
||||||
def make_map(self):
|
def make_map(self):
|
||||||
temp_surface = pg.Surface((self.width, self.height))
|
temp_surface = pg.Surface((self.width, self.height))
|
||||||
self.render(temp_surface)
|
self.render(temp_surface)
|
||||||
return temp_surface
|
return temp_surface
|
||||||
|
Loading…
Reference in New Issue
Block a user