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.__hitch = hitch
self.__header = header
self.__fuel_tank = 10000000
self.__fuel_tank = 100
self.__engineWorking = False
@property
@ -51,7 +51,7 @@ class Tractor:
def reduce_fuel(self):
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:
self.__engineWorking = False
@ -77,14 +77,14 @@ class Tractor:
def turnOffEngine(self):
self.__engineWorking = False
def turn(self, direction):
def drive(self, direction):
mRange = 1
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:
self.__vertical_index += 1
self.__vertical_index += mRange
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:
self.__horizontal_index += -1
self.__horizontal_index += -mRange

View File

@ -2,18 +2,38 @@ from Main import TractorLoad
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":
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"
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"
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"
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"
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
#FRAMES PER SECOND
FPS = 5
FPS = 100

View File

@ -9,7 +9,7 @@ def cruiseControl(tractor, direction, cruiseControl):
def manualTurning(event, tractor):
direction = "NONE"
direction = "STOP"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and tractor.horizontal_index > 0:
direction = "LEFT"
@ -20,7 +20,7 @@ def manualTurning(event, tractor):
elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1:
direction = "DOWN"
tractor.turn(direction)
tractor.drive(direction)
return direction
@ -36,3 +36,31 @@ def getDirection(horizontal_change, vertical_change):
elif horizontal_change == -1:
direction = "LEFT"
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.Tractor import Tractor
from Main.TractorAction import action
from Main.TractorAction import action, chooseToolset
from Main.TractorLoad import TillageUnit
from Main.constants import *
from Main.driving import autodrive, isComebackTime
pygame.init()
@ -15,20 +16,19 @@ pygame.display.set_caption('Tractor')
working = True
cruiseControl = True
autoAction = True
autonomousDrive = False
autonomousDrive = True
comeback = False
lastDirection = "RIGHT"
horizontal_change = 1
vertical_change = 0
direction = "RIGHT"
hitchCounter = 0
loadCounter = 0
toolCounter = 0
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")
tractor.turnOnEngine()
@ -42,7 +42,7 @@ while working:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
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:
hitchCounter = (hitchCounter + 1) % 3
if hitchCounter == 0:
@ -71,34 +71,32 @@ while working:
direction = driving.manualTurning(event, tractor)
if direction == lastDirection:
tractor.turn(driving.cruiseControl(tractor, direction, cruiseControl))
print(tractor.horizontal_index, " ", tractor.vertical_index)
field = board[tractor.horizontal_index][tractor.vertical_index]
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":
lastDirection = direction
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
else:
drawUI.drawUI(board, display, tractor, lastDirection, tillageUnit, field)
direction = lastDirection
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
direction = autodrive(tractor, autonomousDrive, direction, comeback)
comeback, direction = isComebackTime(tractor, direction, comeback)
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
clock.tick(FPS)
tractor.reduce_fuel()