This commit is contained in:
v7eZ3t 2021-03-29 19:15:36 +02:00
parent 11b1d1834c
commit a5d9403a07
5 changed files with 86 additions and 40 deletions

View File

@ -8,7 +8,7 @@ class Tractor:
self.__vertical_index = vertical_index self.__vertical_index = vertical_index
self.__hitch = hitch self.__hitch = hitch
self.__header = header self.__header = header
self.__fuel_tank = 10000000 self.__fuel_tank = 100
self.__engineWorking = False self.__engineWorking = False
@property @property
@ -51,7 +51,7 @@ class Tractor:
def reduce_fuel(self): def reduce_fuel(self):
if 0 < self.fuel_tank <= 100: if 0 < self.fuel_tank <= 100:
self.__fuel_tank = self.__fuel_tank - 1 self.__fuel_tank = self.__fuel_tank - 0.5
if self.__fuel_tank <= 0: if self.__fuel_tank <= 0:
self.__engineWorking = False self.__engineWorking = False
@ -77,14 +77,14 @@ class Tractor:
def turnOffEngine(self): def turnOffEngine(self):
self.__engineWorking = False self.__engineWorking = False
def turn(self, direction): def drive(self, direction):
mRange = 1
if direction == "UP" and self.vertical_index > 0: if direction == "UP" and self.vertical_index > 0:
self.__vertical_index += -1 self.__vertical_index += - mRange
elif direction == "DOWN" and self.vertical_index < VERTICAL_TILES_NUMBER-1: elif direction == "DOWN" and self.vertical_index < VERTICAL_TILES_NUMBER-1:
self.__vertical_index += 1 self.__vertical_index += mRange
elif direction == "RIGHT" and self.horizontal_index < HORIZONTAL_TILES_NUMBER-1: elif direction == "RIGHT" and self.horizontal_index < HORIZONTAL_TILES_NUMBER-1:
self.__horizontal_index += 1 self.__horizontal_index += mRange
elif direction == "LEFT" and self.horizontal_index > 0: elif direction == "LEFT" and self.horizontal_index > 0:
self.__horizontal_index += -1 self.__horizontal_index += -mRange

View File

@ -2,18 +2,38 @@ from Main import TractorLoad
from Main.TractorLoad import TillageUnit from Main.TractorLoad import TillageUnit
def action(field, tractor, tillageUnit): def action(field, tractor):
if tractor.header and tractor.hitch == "Crop Trailer" and field.state == "toCut": if tractor.header and tractor.hitch == "Crop Trailer" and field.state == "toCut":
return "toPlow" return "toPlow"
elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Fertilizer" and field.state == "toFertilize": elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Fertilizer" and field.state == "toFertilize":
return "toSeed" return "toSeed"
elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Seeds" and field.state == "toSeed": elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Seeds" and field.state == "toSeed":
return "toWater" return "toWater"
elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Water" and field.state == "toWater": elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Water" and field.state == "toWater":
return "toCut" return "toCut"
elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Nothing" and field.state == "toPlow": elif isinstance(tractor.hitch, TillageUnit) and tractor.hitch.load == "Nothing" and field.state == "toPlow":
return "toFertilize" return "toFertilize"
def chooseToolset(tractor, tillageUnit, set):
if set == 0:
tractor.hitch = tillageUnit
tractor.hitch.load = "Nothing"
if set == 1:
tractor.hitch = tillageUnit
tractor.hitch.load = "Fertilizer"
if set == 2:
tractor.hitch = tillageUnit
tractor.hitch.load = "Seeds"
if set == 3:
tractor.hitch = tillageUnit
tractor.hitch.load = "Water"
if set == 4:
tractor.header = True
tractor.hitch = "Crop Trailer"
return tractor, tillageUnit

View File

@ -29,7 +29,7 @@ TRACTOR_WIDTH = TILE_SIZE
TRACTOR_HEIGHT = TILE_SIZE TRACTOR_HEIGHT = TILE_SIZE
#FRAMES PER SECOND #FRAMES PER SECOND
FPS = 5 FPS = 100

View File

@ -9,7 +9,7 @@ def cruiseControl(tractor, direction, cruiseControl):
def manualTurning(event, tractor): def manualTurning(event, tractor):
direction = "NONE" direction = "STOP"
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and tractor.horizontal_index > 0: if event.key == pygame.K_LEFT and tractor.horizontal_index > 0:
direction = "LEFT" direction = "LEFT"
@ -20,7 +20,7 @@ def manualTurning(event, tractor):
elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1: elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1:
direction = "DOWN" direction = "DOWN"
tractor.turn(direction) tractor.drive(direction)
return direction return direction
@ -36,3 +36,31 @@ def getDirection(horizontal_change, vertical_change):
elif horizontal_change == -1: elif horizontal_change == -1:
direction = "LEFT" direction = "LEFT"
return direction return direction
def autodrive(tractor, autonomousDrive, direction, comeback):
if autonomousDrive:
if not comeback:
if direction == "RIGHT" and tractor.horizontal_index == HORIZONTAL_TILES_NUMBER - 1:
direction = "DOWN"
elif direction == "DOWN" and tractor.horizontal_index == HORIZONTAL_TILES_NUMBER - 1:
direction = "LEFT"
elif direction == "LEFT" and tractor.horizontal_index == 0:
direction = "DOWN"
elif direction == "DOWN" and tractor.horizontal_index == 0:
direction = "RIGHT"
else:
direction = "UP"
return direction
def isComebackTime(tractor, direction, comeback):
if tractor.vertical_index == 5 and tractor.horizontal_index == 0:
comeback = True
print(comeback)
direction = "UP"
if tractor.vertical_index == 0 and tractor.horizontal_index == 0:
comeback = False
print(comeback)
direction = "RIGHT"
return comeback, direction

46
main.py
View File

@ -3,9 +3,10 @@ import pygame
from Main import Board, driving, drawUI from Main import Board, driving, drawUI
from Main.Tractor import Tractor from Main.Tractor import Tractor
from Main.TractorAction import action from Main.TractorAction import action, chooseToolset
from Main.TractorLoad import TillageUnit from Main.TractorLoad import TillageUnit
from Main.constants import * from Main.constants import *
from Main.driving import autodrive, isComebackTime
pygame.init() pygame.init()
@ -15,20 +16,19 @@ pygame.display.set_caption('Tractor')
working = True working = True
cruiseControl = True cruiseControl = True
autoAction = True autoAction = True
autonomousDrive = False autonomousDrive = True
comeback = False
lastDirection = "RIGHT" lastDirection = "RIGHT"
horizontal_change = 1
vertical_change = 0
direction = "RIGHT" direction = "RIGHT"
hitchCounter = 0 hitchCounter = 0
loadCounter = 0 loadCounter = 0
toolCounter = 0
board = Board.generate() board = Board.generate()
tractor = Tractor(horizontal_index=0, vertical_index=0, hitch="nothing", header=False) tractor = Tractor(horizontal_index=-0, vertical_index=0, hitch="nothing", header=False)
tillageUnit = TillageUnit("Nothing") tillageUnit = TillageUnit("Nothing")
tractor.turnOnEngine() tractor.turnOnEngine()
@ -42,7 +42,7 @@ while working:
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: if event.key == pygame.K_SPACE:
field = board[tractor.horizontal_index][tractor.vertical_index] field = board[tractor.horizontal_index][tractor.vertical_index]
field.state = action(field, tractor, tillageUnit) field.state = action(field, tractor)
if event.key == pygame.K_q: if event.key == pygame.K_q:
hitchCounter = (hitchCounter + 1) % 3 hitchCounter = (hitchCounter + 1) % 3
if hitchCounter == 0: if hitchCounter == 0:
@ -71,34 +71,32 @@ while working:
direction = driving.manualTurning(event, tractor) direction = driving.manualTurning(event, tractor)
if direction == lastDirection:
tractor.turn(driving.cruiseControl(tractor, direction, cruiseControl))
print(tractor.horizontal_index, " ", tractor.vertical_index) print(tractor.horizontal_index, " ", tractor.vertical_index)
field = board[tractor.horizontal_index][tractor.vertical_index] field = board[tractor.horizontal_index][tractor.vertical_index]
if autoAction: if autoAction:
field.state = action(field, tractor, tillageUnit) if tractor.horizontal_index == 0 and tractor.vertical_index == 0:
toolCounter = (toolCounter + 1) % 5
tractor, tillageUnit = chooseToolset(tractor, tillageUnit, toolCounter)
tractor.fill_tank()
field.state = action(field, tractor)
print(direction)
tractor.drive(driving.cruiseControl(tractor, direction, cruiseControl))
if direction != "STOP": if direction != "STOP":
lastDirection = direction lastDirection = direction
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
else: else:
drawUI.drawUI(board, display, tractor, lastDirection, tillageUnit, field) direction = lastDirection
if autonomousDrive: direction = autodrive(tractor, autonomousDrive, direction, comeback)
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
comeback, direction = isComebackTime(tractor, direction, comeback)
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
clock.tick(FPS) clock.tick(FPS)
tractor.reduce_fuel() tractor.reduce_fuel()