AI_PRO/main.py
secret_dude 09eb0bbc2f V1.34
2021-03-30 00:54:29 +02:00

118 lines
3.5 KiB
Python

import pygame
# wersja 1.05
import Board, driving, drawUI
import TractorAction
from Tractor import Tractor
from TractorAction import action
from TractorLoad import TillageUnit
from constants import *
from driving import autodrive, isComebackTime
pygame.init()
display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICAL))
pygame.display.set_caption('Tractor')
working = True
cruiseControl = True
autoAction = True
animationSpeed = ANIMATION_PART
comeback = False
lastDirection = "RIGHT"
direction = "RIGHT"
hitchCounter = 0
loadCounter = 0
toolCounter = - 1
board = Board.generate()
tractor = Tractor(horizontal_index=-0, vertical_index=0, hitch="nothing", header=False, autodrive=True)
tillageUnit = TillageUnit("Nothing")
tractor.turnOnEngine()
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]
field.state = action(field, tractor)
if event.key == pygame.K_q:
hitchCounter = (hitchCounter + 1) % 3
if hitchCounter == 0:
tractor.hitch = "Crop Trailer"
if hitchCounter == 1:
tractor.hitch = tillageUnit
if hitchCounter == 2:
tractor.hitch = "Nothing"
if event.key == pygame.K_w:
if tractor.header:
tractor.header = False
else:
tractor.header = True
if event.key == pygame.K_e:
loadCounter = (loadCounter + 1) % 4
if loadCounter == 0:
tillageUnit.load = "Nothing"
elif loadCounter == 1:
tillageUnit.load = "Seeds"
elif loadCounter == 2:
tillageUnit.load = "Water"
elif loadCounter == 3:
tillageUnit.load = "Fertilizer"
if event.key == pygame.K_r:
if tractor.engineWorking:
tractor.turnOffEngine()
else:
tractor.turnOnEngine()
if event.key == pygame.K_t:
if tractor.autodrive:
tractor.autodrive = False
cruiseControl = False
autoAction = False
animationSpeed = 1
else:
tractor.autodrive = True
animationSpeed = ANIMATION_PART
cruiseControl = True
autoAction = True
direction = driving.manualTurning(event, tractor)
#print(tractor.horizontal_index, " ", tractor.vertical_index)
field = board[tractor.horizontal_index][tractor.vertical_index]
if autoAction:
tractor, tillageUnit, toolCounter = TractorAction.autoAction(tractor, tillageUnit, toolCounter)
field.state = action(field, tractor)
tractor.drive(driving.cruiseControl(tractor, direction, cruiseControl))
if direction != "STOP":
lastDirection = direction
else:
direction = lastDirection
direction = autodrive(tractor, direction, comeback)
comeback, direction = isComebackTime(tractor, direction, comeback)
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field, animationSpeed)
clock.tick(FPS)
tractor.reduce_fuel()
pygame.quit()
quit()