Compare commits

...

2 Commits

Author SHA1 Message Date
MarRac
dd4f656ea2 fixed tractor's trail and improved some visuals 2024-06-09 22:58:40 +02:00
MarRac
0e2d63fbbf bug fix regarding obstacles 2024-06-09 22:09:36 +02:00
8 changed files with 78 additions and 14 deletions

View File

@ -148,29 +148,53 @@ class Tractor:
def draw_tractor(self, win):
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
win.blit(imageTractor, (self.rect.x, self.rect.y))
pygame.display.flip()
def store_tile_image(self, tile):
return pygame.image.load(tile.image).convert_alpha()
def restore_tile_image(self, screen, tile):
image = pygame.image.load(tile.image).convert_alpha()
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
screen.blit(image, (tile.x, tile.y))
pygame.display.update()
#translates move_list generated by bfs into the actual movement:
def do_actions(tractor, WIN, move_list):
trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
# trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
# trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
tile_images = {}
for tile in tiles:
tile_images[(tile.x, tile.y)] = tractor.store_tile_image(tile)
pygame.display.update()
for move in move_list:
WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
# WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
current_tile = None
for tile in tiles:
if tile.x == tractor.rect.x and tile.y == tractor.rect.y:
current_tile = tile
break
if current_tile:
tractor.restore_tile_image(WIN, current_tile)
if move == "move":
tractor.move()
elif move == "rotate_right":
tractor.rotate_to_right()
elif move == "rotate_left":
tractor.rotate_to_left()
tractor.draw_tractor(WIN)
pygame.display.update()
time.sleep(0.5)
#displays results of the "work_on_field" function next to the field:
def display_work_results(screen, text, position):
font = pygame.font.Font(None, 30)

View File

@ -91,7 +91,7 @@ def heuristic(current_x, current_y, end_x, end_y):
# actions(string): move, rotate_to_left, rotate_to_right
# main search function:
def a_star(istate, succ, goaltest, tractor):
def a_star(istate, succ_astar, goaltest):
fringe = []
explored = set()
node = Node(0, istate.get_x(), istate.get_y(), istate.get_direction(), None, None, 0)
@ -109,7 +109,7 @@ def a_star(istate, succ, goaltest, tractor):
explored.add(elem)
for (action, state) in succ(temp, tractor):
for (action, state) in succ_astar(temp):
fringe_tuple = []
explored_tuple = []

View File

@ -159,6 +159,42 @@ def succ(elem, tractor):
return actions_states
#its the copy of successor function for A* only - tractor can ride through stones if there is no other way:
def succ_astar(elem):
actions_states = []
temp = copy.copy(elem.get_direction())
if temp == 1:
temp = 4
else:
temp -= 1
actions_states.append(("rotate_left", (elem.get_x(), elem.get_y(), temp)))
temp = copy.copy(elem.get_direction())
if temp == 4:
temp = 1
else:
temp += 1
actions_states.append(("rotate_right", (elem.get_x(), elem.get_y(), temp)))
temp_move_east = elem.get_x() + TILE_SIZE
temp_move_west = elem.get_x() - TILE_SIZE
temp_move_north = elem.get_y() - TILE_SIZE
temp_move_south = elem.get_y() + TILE_SIZE
if Tractor.can_it_move_node(elem) == "move east":
actions_states.append(("move", (temp_move_east, elem.get_y(), elem.get_direction())))
elif Tractor.can_it_move_node(elem) == "move west":
actions_states.append(("move", (temp_move_west, elem.get_y(), elem.get_direction())))
elif Tractor.can_it_move_node(elem) == "move north":
actions_states.append(("move", (elem.get_x(), temp_move_north, elem.get_direction())))
elif Tractor.can_it_move_node(elem) == "move south":
actions_states.append(("move", (elem.get_x(), temp_move_south, elem.get_direction())))
return actions_states
#returns list of actions
def get_moves(elem):

View File

@ -10,7 +10,7 @@ from area.field import tiles, fieldX, fieldY
from area.field import get_tile_coordinates, get_tile_index
from ground import Dirt
from plant import Plant
from bfs import graphsearch, Istate, succ
from bfs import graphsearch, Istate, succ_astar, succ
from astar import a_star
from NN.neural_network import load_model, load_image, guess_image, display_image, display_result, clear_text_area
from PIL import Image
@ -26,13 +26,10 @@ pygame.display.set_caption('Intelligent tractor')
def main():
run = True
window = drawWindow(WIN)
pygame.display.update()
#Tractor initialization:
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
tractor.rect.x += fieldX
@ -71,13 +68,11 @@ def main():
goaltest[1]=tile_y
#moves = (graphsearch(istate, succ, goaltest, tractor)) #<-------BFS
moves = (a_star(istate, succ, goaltest, tractor))
moves = (a_star(istate, succ_astar, goaltest))
print(moves)
# movement based on route-planning:
tractor.draw_tractor(WIN)
time.sleep(1)
if moves != False:
@ -216,6 +211,15 @@ def main():
#update the initial state for the next target:
istate = Istate(tile_x, tile_y, tractor.direction)
#old goalTile is displayed with a black border - to show that it was an old target:
tiles[tile_index].image = "resources/images/sampling_old_goal.png"
image = pygame.image.load(tiles[tile_index].image).convert()
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
WIN.blit(image, (tiles[tile_index].x, tiles[tile_index].y))
pygame.display.flip()
tractor.draw_tractor(WIN)
time.sleep(2)
print("\n")

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB