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 area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT
from area.field import fieldX,fieldY
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT, WIDTH, HEIGHT
import pygame
import time
class Tractor:
@ -79,19 +79,15 @@ class Tractor:
def move(self):
if self.direction == DIRECTION_EAST:
#self.x += TILE_SIZE
self.rect.x += TILE_SIZE
elif self.direction == DIRECTION_WEST:
#self.x -= TILE_SIZE
self.rect.x -= TILE_SIZE
elif self.direction == DIRECTION_NORTH:
#self.y -= TILE_SIZE
self.rect.y -= TILE_SIZE
elif self.direction == DIRECTION_SOUTH:
#self.y += TILE_SIZE
self.rect.y += TILE_SIZE
@ -129,3 +125,20 @@ class Tractor:
win.blit(imageTractor, (self.rect.x, self.rect.y))
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.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 get_tile_coordinates
from ground import Dirt
@ -13,26 +13,26 @@ from bfs import graphsearch, Istate, succ
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
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():
run = True
window = drawWindow(WIN)
pygame.display.update()
#getting coordinates of a certain tile - test:
tile_index=22
tile_index=30
tile_x, tile_y = get_tile_coordinates(tile_index)
if tile_x is not None and tile_y is not None:
print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})")
else: print("Such tile does not exist")
#graphsearch activation:
istate = Istate(170, 100, 2)
goaltest = []
goaltest.append(tile_x)
goaltest.append(tile_y)
print(graphsearch(istate, succ, goaltest))
moves = (graphsearch(istate, succ, goaltest))
print(moves)
#main loop:
while run:
@ -48,21 +48,18 @@ def main():
d1.pests_and_weeds()
tile1.ground=d1
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 1)
tractor.rotate_to_right()
#movement based on route-planning test:
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2)
tractor.rect.x += fieldX
tractor.rect.y += fieldY
tractor.draw_tractor(WIN)
time.sleep(1)
for _ in range(5):
WIN.blit(trail, (tractor.rect.x, tractor.rect.y, TILE_SIZE, TILE_SIZE))
tractor.move()
if moves != None:
do_actions(tractor, WIN, moves)
tractor.draw_tractor(WIN)
pygame.display.flip()
tractor.work_on_field(tile1, d1, p1)
time.sleep(2)
print("\n")
tractor.work_on_field(tile1, d1, p1)
time.sleep(2)
print("\n")