Traktor/main.py
2024-04-14 14:37:32 +02:00

125 lines
4.4 KiB
Python

import pygame
from board import Board
from constant import width, height, rows, cols
from tractor import Tractor
from kolejka import Stan, Kolejka, Odwiedzone
fps = 5
WIN = pygame.display.set_mode((width, height))
pygame.display.set_caption('Inteligenty Traktor')
def goal_test(elem, goaltest, board):
if board.is_rock(goaltest.col, goaltest.row):
return True
if((elem.row == goaltest.row) and (elem.col == goaltest.col)):
return True
else:
return False
def actions(elem, istate):
akcje = []
while((elem.row != istate.row) or (elem.col != istate.col) or (elem.direction != istate.direction)):
akcje.append(elem.a)
elem = elem.p[0]
return akcje
def graphsearch(istate, goaltest, board):
explored = Odwiedzone()
fringe = Kolejka()
fringe.dodaj_stan(istate)
moves = ["up", "left", "right"]
while not fringe.czy_pusta():
elem = fringe.usun_stan()
if goal_test(elem, goaltest, board):
return actions(elem, istate)
explored.dodaj_stan(elem)
for action in moves:
stan = elem.succ(action, board)
if((not fringe.check(stan)) and (not explored.check(stan))):
stan.parrent(elem, action)
fringe.dodaj_stan(stan)
return "Brak sciezki"
def main():
rotation = ["left", "up", "right", "down"]
istate = Stan(4,4, "down")
goaltest = Stan(1,1, "up")
run = True
clock = pygame.time.Clock()
board = Board()
board.load_images()
actions = graphsearch(istate, goaltest, board)
print("akcje: >",actions )
tractor = Tractor(4, 4)
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if(tractor.direction == "up" and tractor.row > 0 ):
if board.is_weed(tractor.col, tractor.row - 1):
board.set_grass(tractor.col, tractor.row - 1)
tractor.row -= 1
elif board.is_dirt(tractor.col, tractor.row - 1):
board.set_soil(tractor.col, tractor.row - 1)
tractor.row -= 1
elif not board.is_rock(tractor.col, tractor.row - 1):
tractor.row -= 1
if(tractor.direction == "left" and tractor.col > 0):
if board.is_weed(tractor.col - 1, tractor.row):
board.set_grass(tractor.col - 1, tractor.row)
tractor.col -= 1
elif board.is_dirt(tractor.col - 1, tractor.row):
board.set_soil(tractor.col - 1, tractor.row)
tractor.col -= 1
elif not board.is_rock(tractor.col - 1, tractor.row):
tractor.col -= 1
if(tractor.direction == "down" and tractor.row < rows - 1):
if board.is_weed(tractor.col, tractor.row + 1):
board.set_grass(tractor.col, tractor.row + 1)
tractor.row += 1
elif board.is_dirt(tractor.col, tractor.row + 1):
board.set_soil(tractor.col, tractor.row + 1)
tractor.row += 1
elif not board.is_rock(tractor.col, tractor.row + 1):
tractor.row += 1
if(tractor.direction == "right" and tractor.col < cols - 1):
if board.is_weed(tractor.col + 1, tractor.row):
board.set_grass(tractor.col + 1, tractor.row)
tractor.col += 1
elif board.is_dirt(tractor.col + 1, tractor.row):
board.set_soil(tractor.col + 1, tractor.row)
tractor.col += 1
elif not board.is_rock(tractor.col + 1, tractor.row):
tractor.col += 1
if keys[pygame.K_LEFT]:
for i in range(0, 4):
if(tractor.direction == rotation[i]):
if(i == 0):
i = 4
tractor.direction = rotation[i-1]
break
if keys[pygame.K_RIGHT]:
for i in range(0, 4):
if(tractor.direction == rotation[i]):
if(i == 3):
i = -1
tractor.direction = rotation[i+1]
break
board.draw_cubes(WIN)
tractor.draw(WIN)
pygame.display.update()
pygame.quit()
main()