Fixed bugs with A*. Added new class Rubbish.

This commit is contained in:
HelQ 2022-06-04 21:52:30 +02:00
parent 7daf17c19b
commit ac3397fd42
4 changed files with 45 additions and 30 deletions

57
main.py
View File

@ -3,6 +3,7 @@ from queue import PriorityQueue
from path_algorithms.a_star import a_star from path_algorithms.a_star import a_star
# from path_algorithms.bfs import bfs # from path_algorithms.bfs import bfs
from rubbish import *
from truck import Truck from truck import Truck
from surface import * from surface import *
@ -10,55 +11,60 @@ RESOLUTION = 900
SIZE = 60 SIZE = 60
# matrix for display # matrix for display
matrix = [[1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], matrix = [[0, 1, 1, 2, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1], [0, 0, 3, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 3, 1, 2, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1], [0, 0, 3, 0, 2, 1, 5, 3, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 5, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0],
[3, 3, 3, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1], [3, 3, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
] ]
pygame.init() pygame.init()
screen = pygame.display.set_mode([RESOLUTION, RESOLUTION]) screen = pygame.display.set_mode([RESOLUTION, RESOLUTION])
truck = Truck(screen) truck = Truck(screen)
surface_list = [] surface_list = []
rubbish_list = []
# x and y are swapped on display in pygame # x and y are swapped on display in pygame
for i in range(15): for i in range(15):
for j in range(15): for j in range(15):
if matrix[i][j] == 1: if matrix[i][j] == 0:
surface_list.append(Grass(screen, j * 60, i * 60)) surface_list.append(Grass(screen, j * 60, i * 60))
if matrix[i][j] == 1:
surface_list.append(Sand(screen, j * 60, i * 60))
if matrix[i][j] == 2: if matrix[i][j] == 2:
surface_list.append(Rock(screen, j * 60, i * 60)) surface_list.append(Rock(screen, j * 60, i * 60))
if matrix[i][j] == 3: if matrix[i][j] == 3:
surface_list.append(Water(screen, j * 60, i * 60)) surface_list.append(Water(screen, j * 60, i * 60))
if matrix[i][j] == 5:
surface_list.append(Grass(screen, j * 60, i * 60))
rubbish_list.append(Rubbish(screen, j * 60, i * 60))
run = 1
path = [] path = []
start = truck.state
direction = truck.direction
while True: while True:
pygame.time.delay(500) pygame.time.delay(500)
for i in surface_list: for i in surface_list:
i.draw_surface() i.draw_surface()
for i in rubbish_list:
i.draw_rubbish()
truck.draw_truck() truck.draw_truck()
if run == 1: if rubbish_list and not path:
start = truck.state start = (truck.y / 60, truck.x / 60)
direction = truck.direction direction = truck.direction
endpoint = (0, 5) currentRubbish = rubbish_list[0]
endpoint = (currentRubbish.y / 60, currentRubbish.x / 60)
# path = bfs(surface_list, endpoint).tree_search(deque(), start, direction) # path = bfs(surface_list, endpoint).tree_search(deque(), start, direction)
path = a_star(surface_list, endpoint).tree_search(PriorityQueue(), start, direction) path = a_star(surface_list, endpoint).tree_search(PriorityQueue(), start, direction)
print(path)
run = 0
if path: if path:
action = path.pop(0) action = path.pop(0)
@ -66,6 +72,9 @@ while True:
truck.move() truck.move()
else: else:
truck.change_direction(action) truck.change_direction(action)
if not path:
if rubbish_list:
print(rubbish_list.pop(0).x)
pygame.display.flip() pygame.display.flip()

View File

@ -17,7 +17,7 @@ class a_star:
# checking borders and impassable surface # checking borders and impassable surface
def limitation_check(self, x, y): def limitation_check(self, x, y):
for surface in self.surface_list: for surface in self.surface_list:
if (surface.y / 60 == x) and (surface.x / 60 == y) and (surface.weight != 3): if (surface.y / 60 == x) and (surface.x / 60 == y) and (surface.weight != -1):
return True return True
return False return False
@ -35,7 +35,7 @@ class a_star:
# cost relative to surface weight # cost relative to surface weight
def g(self, cond): def g(self, cond):
if cond.action == 'L' or cond.action == 'R': if cond.action == 'L' or cond.action == 'R':
cond.weight = cond.parent.weight cond.weight = cond.parent.weight + 1
else: else:
cond.weight = cond.parent.weight + self.current_surface(cond.state[0], cond.state[1]).weight cond.weight = cond.parent.weight + self.current_surface(cond.state[0], cond.state[1]).weight
return cond.weight return cond.weight

View File

@ -4,7 +4,6 @@ import pygame
class Surface: class Surface:
def __init__(self, screen, x, y): def __init__(self, screen, x, y):
self.state = (x, y)
self.x = x self.x = x
self.y = y self.y = y
self.weight = 0 self.weight = 0
@ -29,13 +28,21 @@ class Rock(Surface):
def __init__(self, screen, x, y): def __init__(self, screen, x, y):
super().__init__(screen, x, y) super().__init__(screen, x, y)
self.weight = 50 self.weight = 9
self.image = pygame.image.load('images/rock.png') self.image = pygame.image.load('images/rock.png')
class Sand(Surface):
def __init__(self, screen, x, y):
super().__init__(screen, x, y)
self.weight = 3
self.image = pygame.image.load('images/sand.png')
class Water(Surface): class Water(Surface):
def __init__(self, screen, x, y): def __init__(self, screen, x, y):
super().__init__(screen, x, y) super().__init__(screen, x, y)
self.weight = 3 self.weight = -1
self.image = pygame.image.load('images/water.png') self.image = pygame.image.load('images/water.png')

View File

@ -6,7 +6,6 @@ class Truck:
def __init__(self, screen): def __init__(self, screen):
self.x = 0 self.x = 0
self.y = 0 self.y = 0
self.state = (self.x, self.y)
self.direction = 'R' self.direction = 'R'
self.screen = screen self.screen = screen
self.image = pygame.image.load('images/truck.png') self.image = pygame.image.load('images/truck.png')