This commit is contained in:
secret_dude 2021-03-30 00:54:29 +02:00
parent bad695ca49
commit 09eb0bbc2f
9 changed files with 83 additions and 28 deletions

View File

@ -5,10 +5,14 @@ from constants import *
states = ['toPlow', 'toSeed', 'toFertilize', 'toWater', 'toCut']
def generate():
board = []
for i in range(0, int(HORIZONTAL_TILES_NUMBER)):
board.append([])
for j in range(0, int(VERTICAL_TILES_NUMBER)):
board[i].append(Field(int(i * TILE_SIZE), int(j * TILE_SIZE), random.choice(states)))
board[0][0] = Field(int(0 * TILE_SIZE), int(0 * TILE_SIZE), "TOOLS_FIELD")
board[1][0] = Field(int(1 * TILE_SIZE), int(0 * TILE_SIZE), "FUEL_FIELD")
return board

View File

@ -10,5 +10,6 @@ class Field:
@state.setter
def state(self, state):
if state == "toPlow" or state == "toWater" or state == "toSeed" or state == "toFertilize" or state == "toCut":
if state == "toPlow" or state == "toWater" or state == "toSeed" or \
state == "toFertilize" or state == "toCut" or state == "TOOLS_FIELD" or state == "FUEL_FIELD":
self.__state = state

View File

@ -3,11 +3,12 @@ from constants import HORIZONTAL_TILES_NUMBER, VERTICAL_TILES_NUMBER
class Tractor:
def __init__(self, horizontal_index, vertical_index, hitch, header):
def __init__(self, horizontal_index, vertical_index, hitch, header, autodrive):
self.__horizontal_index = horizontal_index
self.__vertical_index = vertical_index
self.__hitch = hitch
self.__header = header
self.__autodrive = autodrive
self.__fuel_tank = 100
self.__engineWorking = False
@ -77,6 +78,15 @@ class Tractor:
def turnOffEngine(self):
self.__engineWorking = False
@property
def autodrive(self):
return self.__autodrive
@autodrive.setter
def autodrive(self, autodrive):
if isinstance(autodrive, bool):
self.__autodrive = autodrive
def drive(self, direction):
mRange = 1
if direction == "UP" and self.vertical_index > 0:

View File

@ -18,6 +18,13 @@ def action(field, tractor):
return "toCut"
def autoAction(tractor, tillageUnit, toolCounter):
if tractor.horizontal_index == 0 and tractor.vertical_index == 0:
toolCounter = (toolCounter + 1) % 5
tractor, tillageUnit = chooseToolset(tractor, tillageUnit, toolCounter)
tractor.fill_tank()
return tractor, tillageUnit, toolCounter
def chooseToolset(tractor, tillageUnit, set):
if set == 0:
tractor.hitch = tillageUnit

View File

@ -29,8 +29,11 @@ TRACTOR_WIDTH = TILE_SIZE
TRACTOR_HEIGHT = TILE_SIZE
#FRAMES PER SECOND
FPS = 5
FPS = 10
#ANIMATION_PART
ANIMATION_PART = 11

View File

@ -4,30 +4,36 @@ from images import *
import pygame
def drawUI(board, display, tractor, direction, tillageUnit, field):
for i in range(1, 11):
def drawUI(board, display, tractor, direction, tillageUnit, field, animationSpeed):
if animationSpeed == 1:
display.fill(WHITE)
makeField(board, display)
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction, 0)
drawInfo(display, tractor, tillageUnit, field, direction)
pygame.display.update()
for i in range(1, animationSpeed):
display.fill(WHITE)
makeField(board, display)
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction, i)
drawInfo(display, tractor, tillageUnit, field)
drawInfo(display, tractor, tillageUnit, field, direction)
pygame.display.update()
def drawInfo(display, tractor, tillageUnit, field):
def drawInfo(display, tractor, tillageUnit, field, direction):
myfont = pygame.font.SysFont('Comic Sans MS', 30)
hitches = tractor.hitch
if isinstance(tractor.hitch, TillageUnit):
hitches = "Tillage Unit"
text = f"Fuel: {tractor.fuel_tank} \t Hitches: {hitches} \t Header: {tractor.header} \t Engine working: {tractor.engineWorking}"
text = f"(Q)Hitches: {hitches} (W)Header: {tractor.header} (R)Engine working: {tractor.engineWorking} (T)Autodrive: {tractor.autodrive} Fuel: {tractor.fuel_tank}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 200)))
text = f"Tillage Unit Load: {tillageUnit.load}"
text = f"(E)Tillage Unit Load: {tillageUnit.load} (^) Direction: {direction}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 150)))
text = f"Current field: {field.state, field.horizontal/100, field.vertical/100}"
text = f"Current field: {field.horizontal/100, field.vertical/100, field.state} "
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 100)))
@ -56,14 +62,21 @@ def makeField(board, screen: pygame.Surface):
to_fertilizer_rect.center = (pos_x, pos_y)
screen.blit(to_fertilizer, to_fertilizer_rect)
elif field.state == "TOOLS_FIELD":
tools_rect.center = (pos_x, pos_y)
screen.blit(tools, tools_rect)
elif field.state == "FUEL_FIELD":
fuel_rect.center = (pos_x, pos_y)
screen.blit(fuel, fuel_rect)
def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertical_index, direction, i):
tractor_pic = tractor_up
horizontal = tractor_horizontal_index * TILE_SIZE
vertical = tractor_vertical_index * TILE_SIZE
i = i/10
i = i/ANIMATION_PART
if direction == "UP":
tractor_pic = tractor_up

View File

@ -20,7 +20,8 @@ def manualTurning(event, tractor):
elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1:
direction = "DOWN"
# tractor.drive(direction)
if not tractor.autodrive:
tractor.drive(direction)
return direction
@ -38,8 +39,8 @@ def getDirection(horizontal_change, vertical_change):
return direction
def autodrive(tractor, autonomousDrive, direction, comeback):
if autonomousDrive:
def autodrive(tractor, direction, comeback):
if tractor.autodrive:
if not comeback:
if direction == "RIGHT" and tractor.horizontal_index == HORIZONTAL_TILES_NUMBER - 1:
direction = "DOWN"

View File

@ -45,4 +45,4 @@ to_seed_rect = to_seed.get_rect()
to_cut_rect = to_cut.get_rect()
to_fertilizer_rect = to_fertilizer.get_rect()
fuel_rect = fuel.get_rect()
tools_rect = tools.get_rect()
tools_rect = tools.get_rect()

38
main.py
View File

@ -2,8 +2,9 @@ import pygame
# wersja 1.05
import Board, driving, drawUI
import TractorAction
from Tractor import Tractor
from TractorAction import action, chooseToolset
from TractorAction import action
from TractorLoad import TillageUnit
from constants import *
from driving import autodrive, isComebackTime
@ -16,7 +17,8 @@ pygame.display.set_caption('Tractor')
working = True
cruiseControl = True
autoAction = True
autonomousDrive = True
animationSpeed = ANIMATION_PART
comeback = False
lastDirection = "RIGHT"
@ -28,7 +30,7 @@ toolCounter = - 1
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, autodrive=True)
tillageUnit = TillageUnit("Nothing")
tractor.turnOnEngine()
@ -66,21 +68,35 @@ while working:
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)
#print(tractor.horizontal_index, " ", tractor.vertical_index)
field = board[tractor.horizontal_index][tractor.vertical_index]
if autoAction:
if tractor.horizontal_index == 0 and tractor.vertical_index == 0:
toolCounter = (toolCounter + 1) % 5
tractor, tillageUnit = chooseToolset(tractor, tillageUnit, toolCounter)
tractor.fill_tank()
tractor, tillageUnit, toolCounter = TractorAction.autoAction(tractor, tillageUnit, toolCounter)
field.state = action(field, tractor)
print(direction)
tractor.drive(driving.cruiseControl(tractor, direction, cruiseControl))
if direction != "STOP":
@ -88,11 +104,11 @@ while working:
else:
direction = lastDirection
direction = autodrive(tractor, autonomousDrive, direction, comeback)
direction = autodrive(tractor, direction, comeback)
comeback, direction = isComebackTime(tractor, direction, comeback)
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field, animationSpeed)
clock.tick(FPS)
tractor.reduce_fuel()