Fixed bugs with A*. Added new class Rubbish.
This commit is contained in:
parent
7daf17c19b
commit
ac3397fd42
57
main.py
57
main.py
@ -3,6 +3,7 @@ from queue import PriorityQueue
|
||||
|
||||
from path_algorithms.a_star import a_star
|
||||
# from path_algorithms.bfs import bfs
|
||||
from rubbish import *
|
||||
from truck import Truck
|
||||
from surface import *
|
||||
|
||||
@ -10,55 +11,60 @@ RESOLUTION = 900
|
||||
SIZE = 60
|
||||
|
||||
# matrix for display
|
||||
matrix = [[1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 3, 1, 2, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1],
|
||||
[3, 3, 3, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 1, 1, 1, 1, 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],
|
||||
[0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 3, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 3, 0, 2, 1, 5, 3, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 5, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0],
|
||||
[3, 3, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
]
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode([RESOLUTION, RESOLUTION])
|
||||
|
||||
truck = Truck(screen)
|
||||
surface_list = []
|
||||
rubbish_list = []
|
||||
# x and y are swapped on display in pygame
|
||||
for i 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))
|
||||
if matrix[i][j] == 1:
|
||||
surface_list.append(Sand(screen, j * 60, i * 60))
|
||||
if matrix[i][j] == 2:
|
||||
surface_list.append(Rock(screen, j * 60, i * 60))
|
||||
if matrix[i][j] == 3:
|
||||
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 = []
|
||||
start = truck.state
|
||||
direction = truck.direction
|
||||
while True:
|
||||
pygame.time.delay(500)
|
||||
|
||||
for i in surface_list:
|
||||
i.draw_surface()
|
||||
for i in rubbish_list:
|
||||
i.draw_rubbish()
|
||||
truck.draw_truck()
|
||||
|
||||
if run == 1:
|
||||
start = truck.state
|
||||
if rubbish_list and not path:
|
||||
start = (truck.y / 60, truck.x / 60)
|
||||
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 = a_star(surface_list, endpoint).tree_search(PriorityQueue(), start, direction)
|
||||
print(path)
|
||||
run = 0
|
||||
|
||||
if path:
|
||||
action = path.pop(0)
|
||||
@ -66,6 +72,9 @@ while True:
|
||||
truck.move()
|
||||
else:
|
||||
truck.change_direction(action)
|
||||
if not path:
|
||||
if rubbish_list:
|
||||
print(rubbish_list.pop(0).x)
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
|
@ -17,7 +17,7 @@ class a_star:
|
||||
# checking borders and impassable surface
|
||||
def limitation_check(self, x, y):
|
||||
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 False
|
||||
|
||||
@ -35,7 +35,7 @@ class a_star:
|
||||
# cost relative to surface weight
|
||||
def g(self, cond):
|
||||
if cond.action == 'L' or cond.action == 'R':
|
||||
cond.weight = cond.parent.weight
|
||||
cond.weight = cond.parent.weight + 1
|
||||
else:
|
||||
cond.weight = cond.parent.weight + self.current_surface(cond.state[0], cond.state[1]).weight
|
||||
return cond.weight
|
||||
|
13
surface.py
13
surface.py
@ -4,7 +4,6 @@ import pygame
|
||||
class Surface:
|
||||
|
||||
def __init__(self, screen, x, y):
|
||||
self.state = (x, y)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.weight = 0
|
||||
@ -29,13 +28,21 @@ class Rock(Surface):
|
||||
|
||||
def __init__(self, screen, x, y):
|
||||
super().__init__(screen, x, y)
|
||||
self.weight = 50
|
||||
self.weight = 9
|
||||
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):
|
||||
|
||||
def __init__(self, screen, x, y):
|
||||
super().__init__(screen, x, y)
|
||||
self.weight = 3
|
||||
self.weight = -1
|
||||
self.image = pygame.image.load('images/water.png')
|
||||
|
Loading…
Reference in New Issue
Block a user