This commit is contained in:
v7eZ3t 2021-03-29 17:18:14 +02:00
parent 5c8d3aabbd
commit 11b1d1834c
4 changed files with 59 additions and 48 deletions

View File

@ -15,9 +15,8 @@ class Tractor:
def horizontal_index(self):
return self.__horizontal_index
@horizontal_index.setter
def horizontal_index(self, horizontal_index):
if self.__horizontal_index > 1 or self.__horizontal_index < HORIZONTAL_TILES_NUMBER - 1:
def __horizontal_index(self, horizontal_index):
if 1 <= horizontal_index < HORIZONTAL_TILES_NUMBER:
if self.__engineWorking:
self.__horizontal_index = horizontal_index
@ -25,9 +24,8 @@ class Tractor:
def vertical_index(self):
return self.__vertical_index
@vertical_index.setter
def vertical_index(self, vertical_index):
if self.__vertical_index > 1 or self.__vertical_index < VERTICAL_TILES_NUMBER - 1:
def __vertical_index(self, vertical_index):
if 1 <= self.__vertical_index < VERTICAL_TILES_NUMBER-1:
if self.__engineWorking:
self.__vertical_index = vertical_index
@ -78,3 +76,15 @@ class Tractor:
def turnOffEngine(self):
self.__engineWorking = False
def turn(self, direction):
if direction == "UP" and self.vertical_index > 0:
self.__vertical_index += -1
elif direction == "DOWN" and self.vertical_index < VERTICAL_TILES_NUMBER-1:
self.__vertical_index += 1
elif direction == "RIGHT" and self.horizontal_index < HORIZONTAL_TILES_NUMBER-1:
self.__horizontal_index += 1
elif direction == "LEFT" and self.horizontal_index > 0:
self.__horizontal_index += -1

View File

@ -27,7 +27,7 @@ def drawInfo(display, tractor, tillageUnit, field):
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 150)))
text = f"Current field: {field.state}"
text = f"Current field: {field.state, field.horizontal/100, field.vertical/100}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 100)))

View File

@ -2,34 +2,27 @@ from Main.drawUI import *
import pygame
def cruiseControl(cruiseControl, horizontal_change, vertical_change, tractor):
def cruiseControl(tractor, direction, cruiseControl):
if cruiseControl == False or tractor.engineWorking == False:
horizontal_change = 0
vertical_change = 0
if tractor.horizontal_index <= 0 or tractor.horizontal_index >= HORIZONTAL_TILES_NUMBER - 1:
horizontal_change = 0
if tractor.vertical_index <= 0 or tractor.vertical_index >= VERTICAL_TILES_NUMBER - 1:
vertical_change = 0
return horizontal_change, vertical_change
direction = "STOP"
return direction
def manualTurning(event, tractor_horizontal_index, tractor_vertical_index, horizontal_change, vertical_change):
def manualTurning(event, tractor):
direction = "NONE"
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 event.key == pygame.K_LEFT and tractor_horizontal_index > 0:
vertical_change = 0
horizontal_change = -1
elif event.key == pygame.K_RIGHT and tractor_horizontal_index < HORIZONTAL_TILES_NUMBER - 1:
horizontal_change = 1
vertical_change = 0
elif event.key == pygame.K_UP and tractor_vertical_index > 0:
vertical_change = -1
horizontal_change = 0
elif event.key == pygame.K_DOWN and tractor_vertical_index < VERTICAL_TILES_NUMBER - 1:
vertical_change = 1
horizontal_change = 0
tractor.turn(direction)
return horizontal_change, vertical_change
return direction
def getDirection(horizontal_change, vertical_change):

46
main.py
View File

@ -1,6 +1,5 @@
import pygame
# wersja 1.05
from pygame import sysfont
from Main import Board, driving, drawUI
from Main.Tractor import Tractor
@ -14,12 +13,16 @@ display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICA
pygame.display.set_caption('Tractor')
working = True
cruiseControl = False
cruiseControl = True
autoAction = True
autonomousDrive = False
lastDirection = "RIGHT"
horizontal_change = 0
horizontal_change = 1
vertical_change = 0
direction = "RIGHT"
hitchCounter = 0
loadCounter = 0
@ -56,7 +59,7 @@ while working:
else:
tractor.header = True
if event.key == pygame.K_e:
loadCounter = (loadCounter+1)%4
loadCounter = (loadCounter + 1) % 4
if loadCounter == 0:
tillageUnit.load = "Nothing"
elif loadCounter == 1:
@ -66,34 +69,39 @@ while working:
elif loadCounter == 3:
tillageUnit.load = "Fertilizer"
direction = driving.manualTurning(event, tractor)
if direction == lastDirection:
tractor.turn(driving.cruiseControl(tractor, direction, cruiseControl))
horizontal_change, vertical_change = driving.manualTurning(event, tractor.horizontal_index,
tractor.vertical_index, horizontal_change,
vertical_change)
tractor.horizontal_index += horizontal_change
tractor.vertical_index += vertical_change
horizontal_change, vertical_change = driving.cruiseControl(cruiseControl, horizontal_change, vertical_change,
tractor)
direction = driving.getDirection(horizontal_change, vertical_change)
print(tractor.horizontal_index, " ", tractor.vertical_index)
field = board[tractor.horizontal_index][tractor.vertical_index]
if autoAction:
field.state = action(field, tractor, tillageUnit)
if direction != "STOP":
lastDirection = direction
drawUI.drawUI(board, display, tractor, direction, tillageUnit, field)
else:
drawUI.drawUI(board, display, tractor, lastDirection, tillageUnit, field)
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
clock.tick(FPS)
tractor.reduce_fuel()
print(tractor.fuel_tank)
print(tractor.horizontal_index + horizontal_change, " ", tractor.vertical_index + vertical_change)
print(horizontal_change, " ", vertical_change)
pygame.quit()
quit()