This commit is contained in:
v7eZ3t 2021-03-29 02:53:27 +02:00
parent 00b2790c8c
commit 968013d375
5 changed files with 28 additions and 16 deletions

View File

@ -10,4 +10,5 @@ class Field:
@state.setter @state.setter
def state(self, state): def state(self, state):
if state == 0 or state == 1 or state == 2 or state == 3 or state == 4:
self.__state = state self.__state = state

View File

@ -1,15 +1,17 @@
#constants #constants
# display size in pixels # display size in pixels
DISPLAY_SIZE_HORIZONTAL = 600 from math import ceil
DISPLAY_SIZE_VERTICAL = 600
DISPLAY_SIZE_HORIZONTAL = 1600
DISPLAY_SIZE_VERTICAL = 900
#TILE DIVIDER = TILE SIZE #TILE DIVIDER = TILE SIZE
TILE_SIZE = 30 TILE_SIZE = 100
# number of tiles # number of tiles
HORIZONTAL_TILES_NUMBER = DISPLAY_SIZE_HORIZONTAL/TILE_SIZE HORIZONTAL_TILES_NUMBER = ceil(DISPLAY_SIZE_HORIZONTAL/TILE_SIZE)
VERTICAL_TILES_NUMBER = DISPLAY_SIZE_VERTICAL/TILE_SIZE VERTICAL_TILES_NUMBER = ceil(DISPLAY_SIZE_VERTICAL/TILE_SIZE)
#TILE_SIZE #TILE_SIZE
@ -27,7 +29,7 @@ TRACTOR_WIDTH = TILE_SIZE
TRACTOR_HEIGHT = TILE_SIZE TRACTOR_HEIGHT = TILE_SIZE
#FRAMES PER SECOND #FRAMES PER SECOND
FPS = 5 FPS = 100

View File

@ -39,6 +39,7 @@ def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertic
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))
elif direction == "RIGHT": elif direction == "RIGHT":
screen.blit(tractor_right, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE)) screen.blit(tractor_right, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
else:
screen.blit(tractor_right, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))

View File

@ -6,9 +6,9 @@ def cruiseControl(cruiseControl, horizontal_change, vertical_change, tractor_hor
if not cruiseControl: if not cruiseControl:
horizontal_change = 0 horizontal_change = 0
vertical_change = 0 vertical_change = 0
if tractor_horizontal_index < 1 or tractor_horizontal_index >= HORIZONTAL_TILES_NUMBER - 1: if tractor_horizontal_index <= 0 or tractor_horizontal_index >= HORIZONTAL_TILES_NUMBER - 1:
horizontal_change = 0 horizontal_change = 0
if tractor_vertical_index < 1 or tractor_vertical_index >= VERTICAL_TILES_NUMBER - 1: if tractor_vertical_index <= 0 or tractor_vertical_index >= VERTICAL_TILES_NUMBER - 1:
vertical_change = 0 vertical_change = 0
return horizontal_change, vertical_change return horizontal_change, vertical_change
@ -32,7 +32,7 @@ def manualTurning(event, tractor_horizontal_index, tractor_vertical_index, horiz
def getDirection(horizontal_change, vertical_change): def getDirection(horizontal_change, vertical_change):
direction = "NO" direction = "STOP"
if vertical_change == -1: if vertical_change == -1:
direction = "UP" direction = "UP"
elif vertical_change == 1: elif vertical_change == 1:

16
main.py
View File

@ -12,6 +12,7 @@ pygame.display.set_caption('Tractor')
working = True working = True
cruiseControl = True cruiseControl = True
lastDirection = "RIGHT"
horizontal_change = 0 horizontal_change = 0
vertical_change = 0 vertical_change = 0
@ -33,15 +34,22 @@ while working:
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,
vertical_change) vertical_change)
#todo usunąć /10
tractor.horizontal_index += horizontal_change tractor.horizontal_index += horizontal_change/10
tractor.vertical_index += vertical_change tractor.vertical_index += vertical_change/10
horizontal_change, vertical_change = driving.cruiseControl(cruiseControl, horizontal_change, vertical_change, horizontal_change, vertical_change = driving.cruiseControl(cruiseControl, horizontal_change, vertical_change,
tractor.horizontal_index, tractor.vertical_index) tractor.horizontal_index, tractor.vertical_index)
direction = driving.getDirection(horizontal_change, vertical_change)
if direction != "STOP":
lastDirection = direction
drawUI.drawUI(board, display, tractor.horizontal_index, tractor.vertical_index, drawUI.drawUI(board, display, tractor.horizontal_index, tractor.vertical_index,
driving.getDirection(horizontal_change, vertical_change)) direction)
else:
drawUI.drawUI(board, display, tractor.horizontal_index, tractor.vertical_index,
lastDirection)
clock.tick(FPS) clock.tick(FPS)