v. 1.10
13
Board.py
Normal file
@ -0,0 +1,13 @@
|
||||
import random
|
||||
|
||||
from Main.Field import Field
|
||||
from Main.constants import *
|
||||
|
||||
|
||||
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.randint(0, 3)))
|
||||
return board
|
13
Field.py
Normal file
@ -0,0 +1,13 @@
|
||||
class Field:
|
||||
def __init__(self, __horizontal, __vertical, __state):
|
||||
self.horizontal = __horizontal
|
||||
self.vertical = __vertical
|
||||
self.__state = __state
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self.__state
|
||||
|
||||
@state.setter
|
||||
def state(self, state):
|
||||
self.__state = state
|
5
Testfile.py
Normal file
@ -0,0 +1,5 @@
|
||||
from Main import Board
|
||||
|
||||
board = Board.generate()
|
||||
|
||||
print(board[0][0])
|
62
Tractor.py
Normal file
@ -0,0 +1,62 @@
|
||||
from Main.TractorLoad import TractorTrailer
|
||||
from Main.constants import HORIZONTAL_TILES_NUMBER, VERTICAL_TILES_NUMBER
|
||||
|
||||
|
||||
class Tractor:
|
||||
def __init__(self, horizontal_index, vertical_index, hitch, header, fuel_tank):
|
||||
self.__horizontal_index = horizontal_index
|
||||
self.__vertical_index = vertical_index
|
||||
self.__hitch = hitch
|
||||
self.__header = header
|
||||
self.__fuel_tank = fuel_tank
|
||||
|
||||
@property
|
||||
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:
|
||||
self.__horizontal_index = horizontal_index
|
||||
|
||||
@property
|
||||
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:
|
||||
self.__vertical_index = vertical_index
|
||||
|
||||
@property
|
||||
def hitch(self):
|
||||
return self.__hitch
|
||||
|
||||
@hitch.setter
|
||||
def hitch(self, hitch):
|
||||
if hitch is "Tillage unit" or "Crop Trailer" or TractorTrailer or "Nothing":
|
||||
self.__hitch = hitch
|
||||
|
||||
@property
|
||||
def fuel_tank(self):
|
||||
return self.__fuel_tank
|
||||
|
||||
def __fuel_tank(self, fuel_tank):
|
||||
if 0 < fuel_tank < 100:
|
||||
self.__fuel_tank = fuel_tank
|
||||
|
||||
def fill_tank(self):
|
||||
self.__fuel_tank(100)
|
||||
|
||||
def reduce_fuel(self):
|
||||
if 0 < self.fuel_tank < 100:
|
||||
self.__fuel_tank = self.__fuel_tank - 1
|
||||
|
||||
@property
|
||||
def header(self):
|
||||
return self.__header
|
||||
|
||||
@header.setter
|
||||
def header(self, header):
|
||||
if header is True or False:
|
||||
self.__header = header
|
59
constants.py
Normal file
@ -0,0 +1,59 @@
|
||||
#constants
|
||||
|
||||
# display size in pixels
|
||||
DISPLAY_SIZE_HORIZONTAL = 600
|
||||
DISPLAY_SIZE_VERTICAL = 600
|
||||
|
||||
#TILE DIVIDER = TILE SIZE
|
||||
TILE_SIZE = 30
|
||||
|
||||
# number of tiles
|
||||
HORIZONTAL_TILES_NUMBER = DISPLAY_SIZE_HORIZONTAL/TILE_SIZE
|
||||
VERTICAL_TILES_NUMBER = DISPLAY_SIZE_VERTICAL/TILE_SIZE
|
||||
|
||||
#TILE_SIZE
|
||||
|
||||
#colors
|
||||
WHITE = (255, 255, 255) #SUPER UPRAWA
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0) #DO ZASIANIA
|
||||
YELLOW = (255, 255, 0) #DO PODLANIA
|
||||
GREEN = (0, 255, 0) #DO SCIECIA
|
||||
|
||||
|
||||
#tractor const
|
||||
|
||||
TRACTOR_WIDTH = TILE_SIZE
|
||||
TRACTOR_HEIGHT = TILE_SIZE
|
||||
|
||||
#FRAMES PER SECOND
|
||||
FPS = 5
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
44
drawUI.py
Normal file
@ -0,0 +1,44 @@
|
||||
from Main.constants import *
|
||||
from Main.images import *
|
||||
import pygame
|
||||
|
||||
|
||||
def drawUI(board, display, tractor_horizontal_index, tractor_vertical_index, direction):
|
||||
display.fill(WHITE)
|
||||
makeField(board, display)
|
||||
drawTractor(display, tractor_horizontal_index, tractor_vertical_index, direction)
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
def makeField(board, screen: pygame.Surface):
|
||||
for i in range(int(HORIZONTAL_TILES_NUMBER)):
|
||||
for j in range(int(VERTICAL_TILES_NUMBER)):
|
||||
field = board[i][j]
|
||||
|
||||
pos_x = i * TILE_SIZE + TILE_SIZE // 2
|
||||
pos_y = j * TILE_SIZE + TILE_SIZE // 2
|
||||
|
||||
if field.state == 0:
|
||||
do_podlania_rect.center = (pos_x, pos_y)
|
||||
screen.blit(do_podlania, do_podlania_rect)
|
||||
elif field.state == 1:
|
||||
do_zaorania_rect.center = (pos_x, pos_y)
|
||||
screen.blit(do_zaorania, do_zaorania_rect)
|
||||
elif field.state == 2:
|
||||
do_zasiania_rect.center = (pos_x, pos_y)
|
||||
screen.blit(do_zasiania, do_zasiania_rect)
|
||||
elif field.state == 3:
|
||||
do_zebrania_rect.center = (pos_x, pos_y)
|
||||
screen.blit(do_zebrania, do_zebrania_rect)
|
||||
|
||||
|
||||
def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertical_index, direction):
|
||||
if direction == "UP":
|
||||
screen.blit(tractor_up, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
|
||||
elif direction is "DOWN":
|
||||
screen.blit(tractor_down, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
|
||||
elif direction == "LEFT":
|
||||
screen.blit(tractor_left, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
|
||||
|
||||
elif direction == "RIGHT":
|
||||
screen.blit(tractor_right, (tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE))
|
44
driving.py
Normal file
@ -0,0 +1,44 @@
|
||||
from Main.drawUI import *
|
||||
import pygame
|
||||
|
||||
|
||||
def cruiseControl(cruiseControl, horizontal_change, vertical_change, tractor_horizontal_index, tractor_vertical_index):
|
||||
if not cruiseControl:
|
||||
horizontal_change = 0
|
||||
vertical_change = 0
|
||||
if tractor_horizontal_index < 1 or tractor_horizontal_index >= HORIZONTAL_TILES_NUMBER - 1:
|
||||
horizontal_change = 0
|
||||
if tractor_vertical_index < 1 or tractor_vertical_index >= VERTICAL_TILES_NUMBER - 1:
|
||||
vertical_change = 0
|
||||
return horizontal_change, vertical_change
|
||||
|
||||
|
||||
def manualTurning(event, tractor_horizontal_index, tractor_vertical_index, horizontal_change=0, vertical_change=0):
|
||||
if event.type == pygame.KEYDOWN:
|
||||
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
|
||||
|
||||
return horizontal_change, vertical_change
|
||||
|
||||
|
||||
def getDirection(horizontal_change, vertical_change):
|
||||
direction = "NO"
|
||||
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
|
37
images.py
Normal file
@ -0,0 +1,37 @@
|
||||
import pygame
|
||||
from constants import *
|
||||
|
||||
from Main.constants import TILE_SIZE
|
||||
|
||||
tractor_up_image = pygame.image.load("resources/traktor_up.png")
|
||||
tractor_up = pygame.transform.scale(tractor_up_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_right_image = pygame.image.load("resources/traktor_right.png")
|
||||
tractor_right = pygame.transform.scale(tractor_right_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_down_image = pygame.image.load("resources/traktor_down.png")
|
||||
tractor_down = pygame.transform.scale(tractor_down_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_left_image = pygame.image.load("resources/traktor_left.png")
|
||||
tractor_left = pygame.transform.scale(tractor_left_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
do_podlania_image = pygame.image.load("resources/do_podlania.png")
|
||||
do_podlania = pygame.transform.scale(do_podlania_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
do_zaorania_image = pygame.image.load("resources/do_zaorania.png")
|
||||
do_zaorania = pygame.transform.scale(do_zaorania_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
do_zasiania_image = pygame.image.load("resources/do_zasiania.png")
|
||||
do_zasiania = pygame.transform.scale(do_zasiania_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
do_zebrania_image = pygame.image.load("resources/do_zebrania.png")
|
||||
do_zebrania = pygame.transform.scale(do_zebrania_image, (TILE_SIZE, TILE_SIZE))
|
||||
|
||||
tractor_up_rect = tractor_up.get_rect()
|
||||
tractor_right_rect = tractor_right.get_rect()
|
||||
tractor_down_rect = tractor_down.get_rect()
|
||||
tractor_left_rect = tractor_left.get_rect()
|
||||
do_podlania_rect = do_podlania.get_rect()
|
||||
do_zaorania_rect = do_zaorania.get_rect()
|
||||
do_zasiania_rect = do_zasiania.get_rect()
|
||||
do_zebrania_rect = do_zebrania.get_rect()
|
52
main.py
Normal file
@ -0,0 +1,52 @@
|
||||
import pygame
|
||||
# wersja 1.05
|
||||
|
||||
from Main import Board, driving, drawUI
|
||||
from Main.Tractor import Tractor
|
||||
from Main.constants import *
|
||||
|
||||
pygame.init()
|
||||
|
||||
display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICAL))
|
||||
pygame.display.set_caption('Tractor')
|
||||
|
||||
working = True
|
||||
cruiseControl = True
|
||||
|
||||
horizontal_change = 0
|
||||
vertical_change = 0
|
||||
|
||||
board = Board.generate()
|
||||
|
||||
tractor = Tractor(horizontal_index=0, vertical_index=0, hitch="nothing", header=False, fuel_tank=100)
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
while working:
|
||||
for event in pygame.event.get():
|
||||
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 = 4
|
||||
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.horizontal_index, tractor.vertical_index)
|
||||
|
||||
drawUI.drawUI(board, display, tractor.horizontal_index, tractor.vertical_index,
|
||||
driving.getDirection(horizontal_change, vertical_change))
|
||||
|
||||
clock.tick(FPS)
|
||||
|
||||
print(tractor.horizontal_index + horizontal_change, " ", tractor.vertical_index + vertical_change)
|
||||
print(horizontal_change, " ", vertical_change)
|
||||
|
||||
pygame.quit()
|
||||
quit()
|
BIN
resources/do_podlania.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
resources/do_zaorania.png
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
resources/do_zasiania.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
resources/do_zebrania.png
Normal file
After Width: | Height: | Size: 29 KiB |
BIN
resources/traktor_down.png
Normal file
After Width: | Height: | Size: 144 KiB |
BIN
resources/traktor_left.png
Normal file
After Width: | Height: | Size: 140 KiB |
BIN
resources/traktor_right.png
Normal file
After Width: | Height: | Size: 140 KiB |
BIN
resources/traktor_up.png
Normal file
After Width: | Height: | Size: 144 KiB |