From 7dc8362f1dd31035842040fcffadcb21536ea140 Mon Sep 17 00:00:00 2001 From: secret_dude Date: Tue, 13 Apr 2021 17:52:27 +0200 Subject: [PATCH] SterowanieManualnePrzeniesione --- Tractor.py | 26 ++++++++++---- driving.py | 87 ++++++++++++++--------------------------------- main.py | 80 +++++++------------------------------------ manualSteering.py | 68 ++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 136 deletions(-) create mode 100644 manualSteering.py diff --git a/Tractor.py b/Tractor.py index 0466359..194f70f 100644 --- a/Tractor.py +++ b/Tractor.py @@ -3,7 +3,7 @@ from constants import HORIZONTAL_TILES_NUMBER, VERTICAL_TILES_NUMBER class Tractor: - def __init__(self, horizontal_index, vertical_index, hitch, header, autodrive): + def __init__(self, horizontal_index, vertical_index, hitch, header, autodrive, direction): self.__horizontal_index = horizontal_index self.__vertical_index = vertical_index self.__hitch = hitch @@ -11,6 +11,7 @@ class Tractor: self.__autodrive = autodrive self.__fuel_tank = 100 self.__engineWorking = False + self.__direction = direction @property def horizontal_index(self): @@ -87,13 +88,26 @@ class Tractor: if isinstance(autodrive, bool): self.__autodrive = autodrive - def drive(self, direction): + @property + def direction(self): + return self.__direction + + @direction.setter + def direction(self, direction): + if direction == "UP" or direction == "DOWN" or direction == "RIGHT" or direction == "LEFT": + self.__direction = direction + + + + def drive(self): mRange = 1 - if direction == "UP" and self.vertical_index > 0: + if not self.__engineWorking: + return + if self.__direction == "UP" and self.vertical_index > 0: self.__vertical_index += - mRange - elif direction == "DOWN" and self.vertical_index < VERTICAL_TILES_NUMBER-1: + elif self.__direction == "DOWN" and self.vertical_index < VERTICAL_TILES_NUMBER-1: self.__vertical_index += mRange - elif direction == "RIGHT" and self.horizontal_index < HORIZONTAL_TILES_NUMBER-1: + elif self.__direction == "RIGHT" and self.horizontal_index < HORIZONTAL_TILES_NUMBER-1: self.__horizontal_index += mRange - elif direction == "LEFT" and self.horizontal_index > 0: + elif self.__direction == "LEFT" and self.horizontal_index > 0: self.__horizontal_index += -mRange diff --git a/driving.py b/driving.py index 60f5608..0671666 100644 --- a/driving.py +++ b/driving.py @@ -2,66 +2,29 @@ from drawUI import * import pygame -def cruiseControl(tractor, direction, cruiseControl): - if cruiseControl == False or tractor.engineWorking == False: - direction = "STOP" - return direction +# def autodrive(tractor, direction, comeback): +# if tractor.autodrive: +# 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 manualTurning(event, tractor): - direction = "STOP" - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT and tractor.horizontal_index > 0: - direction = "LEFT" - elif event.key == pygame.K_RIGHT and tractor.horizontal_index < HORIZONTAL_TILES_NUMBER - 1: - direction = "RIGHT" - elif event.key == pygame.K_UP and tractor.vertical_index > 0: - direction = "UP" - elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1: - direction = "DOWN" - - if not tractor.autodrive: - tractor.drive(direction) - - return direction - - -def getDirection(horizontal_change, vertical_change): - direction = "STOP" - if vertical_change == -1: - direction = "UP" - elif vertical_change == 1: - direction = "DOWN" - elif horizontal_change == 1: - direction = "RIGHT" - elif horizontal_change == -1: - direction = "LEFT" - return direction - - -def autodrive(tractor, direction, comeback): - if tractor.autodrive: - 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 +# +# 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 diff --git a/main.py b/main.py index 83f37e8..36d4e67 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,15 @@ import pygame -# wersja 1.05 -import Board, driving, drawUI +import Board import TractorAction +import drawUI from Tractor import Tractor from TractorAction import action from TractorLoad import TillageUnit from constants import * -from driving import autodrive, isComebackTime +from manualSteering import manualSteeringDriver + +# wersja 1.05 pygame.init() @@ -15,14 +17,10 @@ display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICA pygame.display.set_caption('Tractor') working = True -cruiseControl = True autoAction = True animationSpeed = ANIMATION_PART -comeback = False -lastDirection = "RIGHT" -direction = "RIGHT" hitchCounter = 0 loadCounter = 0 @@ -30,7 +28,8 @@ toolCounter = - 1 board = Board.generate() -tractor = Tractor(horizontal_index=-0, vertical_index=0, hitch="nothing", header=False, autodrive=True) +tractor = Tractor(horizontal_index=-0, vertical_index=0, hitch="nothing", header=False, autodrive=True, + direction='RIGHT') tillageUnit = TillageUnit("Nothing") tractor.turnOnEngine() @@ -42,53 +41,7 @@ while working: 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) + manualSteeringDriver(event, board, tractor, hitchCounter, tillageUnit, loadCounter, ANIMATION_PART) field = board[tractor.horizontal_index][tractor.vertical_index] @@ -96,22 +49,13 @@ while working: tractor, tillageUnit, toolCounter = TractorAction.autoAction(tractor, tillageUnit, toolCounter) field.state = action(field, tractor) + tractor.drive() - 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) + drawUI.drawUI(board, display, tractor, tractor.direction, tillageUnit, field, animationSpeed) clock.tick(FPS) - tractor.reduce_fuel() + if tractor.engineWorking: + tractor.reduce_fuel() pygame.quit() quit() diff --git a/manualSteering.py b/manualSteering.py new file mode 100644 index 0000000..9233905 --- /dev/null +++ b/manualSteering.py @@ -0,0 +1,68 @@ +import driving +from constants import * +from TractorAction import action +import pygame + +def manualSteeringDriver(event, board, tractor, hitchCounter, tillageUnit, loadCounter, ANIMATION_PART): + 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 + + manualTurning(event, tractor) + +def manualTurning(event, tractor): + tractor.direction = "STOP" + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT and tractor.horizontal_index > 0: + tractor.direction = "LEFT" + elif event.key == pygame.K_RIGHT and tractor.horizontal_index < HORIZONTAL_TILES_NUMBER - 1: + tractor.direction = "RIGHT" + elif event.key == pygame.K_UP and tractor.vertical_index > 0: + tractor.direction = "UP" + elif event.key == pygame.K_DOWN and tractor.vertical_index < VERTICAL_TILES_NUMBER - 1: + tractor.direction = "DOWN" + + + + +