AI_PRO/main.py

108 lines
3.2 KiB
Python
Raw Normal View History

2021-03-29 02:19:55 +02:00
import pygame
# wersja 1.05
from Main import Board, driving, drawUI
from Main.Tractor import Tractor
2021-03-29 13:06:57 +02:00
from Main.TractorAction import action
2021-03-29 14:33:16 +02:00
from Main.TractorLoad import TillageUnit
2021-03-29 02:19:55 +02:00
from Main.constants import *
pygame.init()
display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICAL))
pygame.display.set_caption('Tractor')
working = True
2021-03-29 17:18:14 +02:00
cruiseControl = True
autoAction = True
autonomousDrive = False
2021-03-29 02:53:27 +02:00
lastDirection = "RIGHT"
2021-03-29 02:19:55 +02:00
2021-03-29 17:18:14 +02:00
horizontal_change = 1
2021-03-29 02:19:55 +02:00
vertical_change = 0
2021-03-29 14:33:16 +02:00
2021-03-29 17:18:14 +02:00
direction = "RIGHT"
2021-03-29 13:46:30 +02:00
hitchCounter = 0
2021-03-29 14:33:16 +02:00
loadCounter = 0
2021-03-29 02:19:55 +02:00
board = Board.generate()
2021-03-29 03:41:38 +02:00
tractor = Tractor(horizontal_index=0, vertical_index=0, hitch="nothing", header=False)
2021-03-29 14:33:16 +02:00
tillageUnit = TillageUnit("Nothing")
2021-03-29 03:41:38 +02:00
tractor.turnOnEngine()
2021-03-29 02:19:55 +02:00
clock = pygame.time.Clock()
while working:
for event in pygame.event.get():
if event.type == pygame.QUIT:
working = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
field = board[tractor.horizontal_index][tractor.vertical_index]
2021-03-29 14:33:16 +02:00
field.state = action(field, tractor, tillageUnit)
2021-03-29 13:32:51 +02:00
if event.key == pygame.K_q:
2021-03-29 13:46:30 +02:00
hitchCounter = (hitchCounter + 1) % 3
if hitchCounter == 0:
tractor.hitch = "Crop Trailer"
if hitchCounter == 1:
2021-03-29 14:33:16 +02:00
print(type(tillageUnit))
print(isinstance(tillageUnit, TillageUnit))
tractor.hitch = tillageUnit
2021-03-29 13:46:30 +02:00
if hitchCounter == 2:
tractor.hitch = "Nothing"
2021-03-29 13:32:51 +02:00
if event.key == pygame.K_w:
2021-03-29 13:46:30 +02:00
if tractor.header:
tractor.header = False
else:
tractor.header = True
2021-03-29 14:33:16 +02:00
if event.key == pygame.K_e:
2021-03-29 17:18:14 +02:00
loadCounter = (loadCounter + 1) % 4
2021-03-29 14:33:16 +02:00
if loadCounter == 0:
tillageUnit.load = "Nothing"
elif loadCounter == 1:
tillageUnit.load = "Seeds"
elif loadCounter == 2:
tillageUnit.load = "Water"
elif loadCounter == 3:
tillageUnit.load = "Fertilizer"
2021-03-29 17:18:14 +02:00
direction = driving.manualTurning(event, tractor)
2021-03-29 14:33:16 +02:00
2021-03-29 17:18:14 +02:00
if direction == lastDirection:
tractor.turn(driving.cruiseControl(tractor, direction, cruiseControl))
2021-03-29 13:46:30 +02:00
2021-03-29 17:18:14 +02:00
print(tractor.horizontal_index, " ", tractor.vertical_index)
2021-03-29 02:53:27 +02:00
2021-03-29 14:33:16 +02:00
field = board[tractor.horizontal_index][tractor.vertical_index]
2021-03-29 17:18:14 +02:00
if autoAction:
field.state = action(field, tractor, tillageUnit)
2021-03-29 02:53:27 +02:00
if direction != "STOP":
lastDirection = direction
2021-03-29 14:33:16 +02:00
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
2021-03-29 02:53:27 +02:00
else:
2021-03-29 14:33:16 +02:00
drawUI.drawUI(board, display, tractor, lastDirection, tillageUnit, field)
2021-03-29 02:19:55 +02:00
2021-03-29 17:18:14 +02:00
if autonomousDrive:
if vertical_change == 1:
vertical_change = 0
if tractor.vertical_index % 2 == 0:
horizontal_change = 1
else:
horizontal_change = -1
elif tractor.horizontal_index == HORIZONTAL_TILES_NUMBER - 1:
vertical_change = 1
elif tractor.horizontal_index == 0 and tractor.vertical_index != 0:
vertical_change = 1
2021-03-29 02:19:55 +02:00
clock.tick(FPS)
2021-03-29 03:41:38 +02:00
tractor.reduce_fuel()
2021-03-29 02:19:55 +02:00
pygame.quit()
quit()