added tractor movement based on bfs

This commit is contained in:
MarRac 2024-04-14 17:20:27 +02:00
parent cd4b714090
commit 8fe3d25324
3 changed files with 35 additions and 25 deletions

View File

@ -1,7 +1,7 @@
from crop_protection_product import CropProtectionProduct from crop_protection_product import CropProtectionProduct
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT, WIDTH, HEIGHT
from area.field import fieldX,fieldY
import pygame import pygame
import time
class Tractor: class Tractor:
@ -79,19 +79,15 @@ class Tractor:
def move(self): def move(self):
if self.direction == DIRECTION_EAST: if self.direction == DIRECTION_EAST:
#self.x += TILE_SIZE
self.rect.x += TILE_SIZE self.rect.x += TILE_SIZE
elif self.direction == DIRECTION_WEST: elif self.direction == DIRECTION_WEST:
#self.x -= TILE_SIZE
self.rect.x -= TILE_SIZE self.rect.x -= TILE_SIZE
elif self.direction == DIRECTION_NORTH: elif self.direction == DIRECTION_NORTH:
#self.y -= TILE_SIZE
self.rect.y -= TILE_SIZE self.rect.y -= TILE_SIZE
elif self.direction == DIRECTION_SOUTH: elif self.direction == DIRECTION_SOUTH:
#self.y += TILE_SIZE
self.rect.y += TILE_SIZE self.rect.y += TILE_SIZE
@ -129,3 +125,20 @@ class Tractor:
win.blit(imageTractor, (self.rect.x, self.rect.y)) win.blit(imageTractor, (self.rect.x, self.rect.y))
pygame.display.flip() pygame.display.flip()
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))
pygame.display.update()
for move in move_list:
WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
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(1)

View File

@ -4,7 +4,7 @@ import random
from area.constants import WIDTH, HEIGHT, TILE_SIZE, GREY from area.constants import WIDTH, HEIGHT, TILE_SIZE, GREY
from area.field import drawWindow from area.field import drawWindow
from area.tractor import Tractor from area.tractor import Tractor, do_actions
from area.field import tiles, fieldX, fieldY from area.field import tiles, fieldX, fieldY
from area.field import get_tile_coordinates from area.field import get_tile_coordinates
from ground import Dirt from ground import Dirt
@ -13,26 +13,26 @@ from bfs import graphsearch, Istate, succ
WIN = pygame.display.set_mode((WIDTH, HEIGHT)) WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Intelligent tractor') pygame.display.set_caption('Intelligent tractor')
trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
def main(): def main():
run = True run = True
window = drawWindow(WIN) window = drawWindow(WIN)
pygame.display.update() pygame.display.update()
#getting coordinates of a certain tile - test: #getting coordinates of a certain tile - test:
tile_index=22 tile_index=30
tile_x, tile_y = get_tile_coordinates(tile_index) tile_x, tile_y = get_tile_coordinates(tile_index)
if tile_x is not None and tile_y is not None: if tile_x is not None and tile_y is not None:
print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})") print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})")
else: print("Such tile does not exist") else: print("Such tile does not exist")
#graphsearch activation:
istate = Istate(170, 100, 2) istate = Istate(170, 100, 2)
goaltest = [] goaltest = []
goaltest.append(tile_x) goaltest.append(tile_x)
goaltest.append(tile_y) goaltest.append(tile_y)
moves = (graphsearch(istate, succ, goaltest))
print(graphsearch(istate, succ, goaltest)) print(moves)
#main loop: #main loop:
while run: while run:
@ -48,21 +48,18 @@ def main():
d1.pests_and_weeds() d1.pests_and_weeds()
tile1.ground=d1 tile1.ground=d1
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 1) #movement based on route-planning test:
tractor.rotate_to_right() tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2)
tractor.rect.x += fieldX tractor.rect.x += fieldX
tractor.rect.y += fieldY tractor.rect.y += fieldY
tractor.draw_tractor(WIN) tractor.draw_tractor(WIN)
time.sleep(1) time.sleep(1)
for _ in range(5): if moves != None:
WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE)) do_actions(tractor, WIN, moves)
tractor.move()
tractor.draw_tractor(WIN) tractor.work_on_field(tile1, d1, p1)
pygame.display.flip() time.sleep(2)
tractor.work_on_field(tile1, d1, p1) print("\n")
time.sleep(2)
print("\n")