Traktor/source/main.py
2024-04-27 22:49:47 +02:00

85 lines
2.6 KiB
Python

import pygame
import time
import random
from area.constants import WIDTH, HEIGHT, TILE_SIZE
from area.field import drawWindow
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
from plant import Plant
from bfs import graphsearch, Istate, succ
from astar import a_star
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Intelligent tractor')
def main():
run = True
window = drawWindow(WIN)
pygame.display.update()
#getting coordinates of our "goal tile":
tile_index=127
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")
#mark the goal tile:
tiles[tile_index].image = "resources/images/sampling_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()
#graphsearch activation:
istate = Istate(170, 100, 2) #initial state
goaltest = []
goaltest.append(tile_x) #final state (consists of x and y because direction doesnt matter)
goaltest.append(tile_y)
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
tractor.rect.x += fieldX
tractor.rect.y += fieldY
tractor.tractor_start = ((istate.get_x(), istate.get_y()))
#tractor.tractor_start = ((istate.get_x(), istate.get_y(), istate.get_direction))
tractor.tractor_end = ((goaltest[0], goaltest[1]))
#moves = (graphsearch(istate, succ, goaltest, tractor))
moves = (a_star(istate, succ, goaltest, tractor))
print(moves)
#main loop:
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#small test of work_on_field method:
time.sleep(1)
tile1 = tiles[0]
p1 = Plant('wheat', 'cereal', random.randint(1,100), random.randint(1,100), random.randint(1,100))
d1 = Dirt(random.randint(1, 100), random.randint(1,100))
d1.pests_and_weeds()
tile1.ground=d1
#movement based on route-planning (test):
tractor.draw_tractor(WIN)
time.sleep(1)
if moves != False:
do_actions(tractor, WIN, moves)
tractor.work_on_field(tile1, d1, p1)
time.sleep(30)
print("\n")
main()