wszystko działa

This commit is contained in:
v7eZ3t 2021-03-29 14:33:16 +02:00
parent 087cb6031d
commit f1b61a0889
5 changed files with 48 additions and 21 deletions

View File

@ -1,4 +1,4 @@
from Main.TractorLoad import TillageUnitLoad from Main.TractorLoad import TillageUnit
from Main.constants import HORIZONTAL_TILES_NUMBER, VERTICAL_TILES_NUMBER from Main.constants import HORIZONTAL_TILES_NUMBER, VERTICAL_TILES_NUMBER
@ -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 = 100 self.__fuel_tank = 10000000
self.__engineWorking = False self.__engineWorking = False
@property @property
@ -37,7 +37,7 @@ class Tractor:
@hitch.setter @hitch.setter
def hitch(self, hitch): def hitch(self, hitch):
if hitch == "Tillage unit" or "Crop Trailer" or TillageUnitLoad or "Nothing": if hitch == "Tillage unit" or hitch == "Crop Trailer" or isinstance(hitch, TillageUnit) or hitch == "Nothing":
self.__hitch = hitch self.__hitch = hitch
@property @property

View File

@ -1,18 +1,19 @@
from Main import TractorLoad from Main import TractorLoad
from Main.TractorLoad import TillageUnit
def action(field, tractor): def action(field, tractor, tillageUnit):
if tractor.header and tractor.hitch == "Crop Trailer": if tractor.header and tractor.hitch == "Crop Trailer" and field.state == "toCut":
return "toPlow" return "toPlow"
elif tractor.hitch == "Tillage Unit" and TractorLoad.TillageUnitLoad == "Fertilizer" and field.state == "toFertilize": elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Fertilizer" and field.state == "toFertilize":
return "toSeed" return "toSeed"
elif tractor.hitch == "Tillage Unit" and TractorLoad.TillageUnitLoad == "Seeds" and field.state == "toSeed": elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Seeds" and field.state == "toSeed":
return "toWater" return "toWater"
elif tractor.hitch == "Tillage Unit" and TractorLoad.TillageUnitLoad == "Water" and field.state == "toWater": elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Water" and field.state == "toWater":
return "toCut" return "toCut"
elif tractor.hitch == "Tillage Unit" and TractorLoad.TillageUnitLoad == "None": elif isinstance(tractor.hitch, TillageUnit) and tillageUnit.load == "Nothing" and field.state == "toPlow":
return "toFertilize" return "toFertilize"

View File

@ -1,4 +1,4 @@
class TillageUnitLoad: class TillageUnit:
def __init__(self, __load): def __init__(self, __load):
self.__load = __load self.__load = __load
@ -8,5 +8,5 @@ class TillageUnitLoad:
@load.setter @load.setter
def load(self, load): def load(self, load):
if self.__load is "Fertilizer" or "Seeds" or "Water": if load == "Fertilizer" or load == "Seeds" or load == "Water" or load == "Nothing":
self.__load = load self.__load = load

View File

@ -3,25 +3,29 @@ from Main.images import *
import pygame import pygame
def drawUI(board, display, tractor, direction): def drawUI(board, display, tractor, direction, tillageUnit, field):
display.fill(WHITE) display.fill(WHITE)
makeField(board, display) makeField(board, display)
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction) drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction)
drawInfo(display, tractor) drawInfo(display, tractor, tillageUnit, field)
pygame.display.update() pygame.display.update()
def drawInfo(display, tractor): def drawInfo(display, tractor, tillageUnit, field):
myfont = pygame.font.SysFont('Comic Sans MS', 30) myfont = pygame.font.SysFont('Comic Sans MS', 30)
text = f"Fuel: {tractor.fuel_tank} \t Hitches: {tractor.hitch} \t Header: {tractor.header} \t Engine working: {tractor.engineWorking}" text = f"Fuel: {tractor.fuel_tank} \t Hitches: {tractor.hitch} \t Header: {tractor.header} \t Engine working: {tractor.engineWorking}"
textsurface = myfont.render(text, False, (0, 0, 0)) textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 200))) display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 200)))
text = f"" text = f"Tillage Unit Load: {tillageUnit.load}"
textsurface = myfont.render(text, False, (0, 0, 0)) textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 150))) display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 150)))
text = f"Current field: {field.state}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 100)))
def makeField(board, screen: pygame.Surface): def makeField(board, screen: pygame.Surface):
for i in range(int(HORIZONTAL_TILES_NUMBER)): for i in range(int(HORIZONTAL_TILES_NUMBER)):
@ -48,7 +52,7 @@ def makeField(board, screen: pygame.Surface):
def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertical_index, direction): def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertical_index, direction):
if direction == "UP": if direction == "UP":
screen.blit(tractor_up, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE)) screen.blit(tractor_up, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
elif direction is "DOWN": elif direction == "DOWN":
screen.blit(tractor_down, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE)) screen.blit(tractor_down, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
elif direction == "LEFT": elif direction == "LEFT":
screen.blit(tractor_left, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE)) screen.blit(tractor_left, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))

32
main.py
View File

@ -5,6 +5,7 @@ from pygame import sysfont
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
from Main.TractorLoad import TillageUnit
from Main.constants import * from Main.constants import *
pygame.init() pygame.init()
@ -13,15 +14,20 @@ display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICA
pygame.display.set_caption('Tractor') pygame.display.set_caption('Tractor')
working = True working = True
cruiseControl = True cruiseControl = False
lastDirection = "RIGHT" lastDirection = "RIGHT"
horizontal_change = 0 horizontal_change = 0
vertical_change = 0 vertical_change = 0
hitchCounter = 0 hitchCounter = 0
loadCounter = 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")
tractor.turnOnEngine() tractor.turnOnEngine()
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -33,13 +39,15 @@ 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) field.state = action(field, tractor, tillageUnit)
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:
tractor.hitch = "Crop Trailer" tractor.hitch = "Crop Trailer"
if hitchCounter == 1: if hitchCounter == 1:
tractor.hitch = "Tillage Unit" print(type(tillageUnit))
print(isinstance(tillageUnit, TillageUnit))
tractor.hitch = tillageUnit
if hitchCounter == 2: if hitchCounter == 2:
tractor.hitch = "Nothing" tractor.hitch = "Nothing"
if event.key == pygame.K_w: if event.key == pygame.K_w:
@ -47,6 +55,18 @@ while working:
tractor.header = False tractor.header = False
else: else:
tractor.header = True 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"
horizontal_change, vertical_change = driving.manualTurning(event, tractor.horizontal_index, horizontal_change, vertical_change = driving.manualTurning(event, tractor.horizontal_index,
tractor.vertical_index, horizontal_change, tractor.vertical_index, horizontal_change,
@ -60,11 +80,13 @@ while working:
direction = driving.getDirection(horizontal_change, vertical_change) direction = driving.getDirection(horizontal_change, vertical_change)
field = board[tractor.horizontal_index][tractor.vertical_index]
if direction != "STOP": if direction != "STOP":
lastDirection = direction lastDirection = direction
drawUI.drawUI(board, display, tractor, direction) drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
else: else:
drawUI.drawUI(board, display, tractor, lastDirection) drawUI.drawUI(board, display, tractor, lastDirection, tillageUnit, field)
clock.tick(FPS) clock.tick(FPS)
tractor.reduce_fuel() tractor.reduce_fuel()