basic_agent

This commit is contained in:
v7eZ3t 2021-03-28 22:42:36 +02:00
parent 803757806a
commit ba447abfb9
6 changed files with 92 additions and 53 deletions

View File

@ -1,12 +1,13 @@
import random
from Main.constants import *
board = []
from Main.Field import Field
from Main.constants import *
def generate():
for i in range(int(HORIZONTAL_TILES_NUMBER)):
board.append([random.randint(0, 3)])
for j in range(int(VERTICAL_TILES_NUMBER)):
board[i].append(random.randint(0, 3))
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

5
Field.py Normal file
View File

@ -0,0 +1,5 @@
class Field:
def __init__(self, horizontal, vertical, state):
self.horizontal = horizontal
self.vertical = vertical
self.state = state

View File

@ -5,7 +5,7 @@ DISPLAY_SIZE_HORIZONTAL = 600
DISPLAY_SIZE_VERTICAL = 600
#TILE DIVIDER = TILE SIZE
TILE_SIZE = 2
TILE_SIZE = 50
# number of tiles
HORIZONTAL_TILES_NUMBER = DISPLAY_SIZE_HORIZONTAL/TILE_SIZE
@ -29,7 +29,7 @@ TRACTOR_WIDTH = TILE_SIZE
TRACTOR_HEIGHT = TILE_SIZE
#FRAMES PER SECOND
FPS = 144
FPS = 5

28
drawUI.py Normal file
View File

@ -0,0 +1,28 @@
from Main.constants import *
import pygame
def makeField(board, display):
color = BLACK
for i in range(int(HORIZONTAL_TILES_NUMBER)):
for j in range(int(VERTICAL_TILES_NUMBER)):
field = board[i][j]
if field.state == 0:
color = WHITE
elif field.state == 1:
color = RED
elif field.state == 2:
color = YELLOW
elif field.state == 3:
color = GREEN
elif field.state == 4:
color = BLACK
pygame.draw.rect(display, color,
[i * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, TILE_SIZE])
def drawTractor(board, display, tractor_horizontal_index, tractor_vertical_index):
pygame.draw.rect(display, BLACK,
[tractor_horizontal_index * TILE_SIZE, tractor_vertical_index * TILE_SIZE, TRACTOR_WIDTH,
TRACTOR_HEIGHT])

27
driving.py Normal file
View File

@ -0,0 +1,27 @@
from Main.constants 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:
horizontal_change = -1
elif event.key == pygame.K_RIGHT and tractor_horizontal_index < HORIZONTAL_TILES_NUMBER - 1:
horizontal_change = 1
elif event.key == pygame.K_UP and tractor_vertical_index > 0:
vertical_change = -1
elif event.key == pygame.K_DOWN and tractor_vertical_index < VERTICAL_TILES_NUMBER - 1:
vertical_change = 1
return horizontal_change, vertical_change

68
main.py
View File

@ -1,9 +1,9 @@
import pygame
# wersja 1.05
from constants import *
from Main import Board
from Main import Board, driving, drawUI
from Main.constants import *
from Main.Field import Field
pygame.init()
@ -13,9 +13,7 @@ display = pygame.display.set_mode((DISPLAY_SIZE_HORIZONTAL, DISPLAY_SIZE_VERTICA
pygame.display.set_caption('Tractor')
working = True
tractor_horizontal_location = TRACTOR_HORIZONTAL_LOCATION
tractor_vertical_location = TRACTOR_VERTICAL_LOCATION
cruiseControl = True
horizontal_change = 0
vertical_change = 0
@ -25,57 +23,37 @@ board = Board.generate()
color = BLACK
clock = pygame.time.Clock()
tractor_horizontal_index = 0
tractor_vertical_index = 0
while working:
for event in pygame.event.get():
if event.type == pygame.QUIT:
working = False
if event.type == pygame.KEYDOWN:
print(tractor_horizontal_location, " ", tractor_vertical_location)
if event.key == pygame.K_LEFT and tractor_horizontal_location > 0:
horizontal_change = -TRACTOR_WIDTH
vertical_change = 0
elif event.key == pygame.K_RIGHT and tractor_horizontal_location < DISPLAY_SIZE_HORIZONTAL - TRACTOR_WIDTH:
horizontal_change = TRACTOR_WIDTH
vertical_change = 0
elif event.key == pygame.K_UP and tractor_vertical_location > 0:
vertical_change = -TRACTOR_HEIGHT
horizontal_change = 0
elif event.key == pygame.K_DOWN and tractor_vertical_location < DISPLAY_SIZE_VERTICAL - TRACTOR_HEIGHT:
vertical_change = TRACTOR_HEIGHT
horizontal_change = 0
elif event.key == pygame.K_SPACE:
change_tile = True
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_horizontal_location += horizontal_change
tractor_vertical_location += vertical_change
tractor_vertical_index += vertical_change
display.fill(WHITE)
for i in range(int(HORIZONTAL_TILES_NUMBER)):
for j in range(int(VERTICAL_TILES_NUMBER)):
if change_tile and i * TILE_SIZE == tractor_horizontal_location and j * TILE_SIZE == tractor_vertical_location:
board[i][j] = 4
change_tile = False
if board[i][j] == 0:
color = WHITE
elif board[i][j] == 1:
color = RED
elif board[i][j] == 2:
color = YELLOW
elif board[i][j] == 3:
color = GREEN
elif board[i][j] == 4:
color = BLACK
drawUI.makeField(board, display)
pygame.draw.rect(display, color,
[i * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, TILE_SIZE])
pygame.draw.rect(display, BLACK,
[tractor_horizontal_location, tractor_vertical_location, TRACTOR_WIDTH, TRACTOR_HEIGHT])
drawUI.drawTractor(board, display, tractor_horizontal_index, tractor_vertical_index)
pygame.display.update()
clock.tick(FPS)
horizontal_change = 0
vertical_change = 0
# commit test 2
horizontal_change, vertical_change = driving.cruiseControl(cruiseControl, horizontal_change, vertical_change,
tractor_horizontal_index, tractor_vertical_index)
print(tractor_horizontal_index + horizontal_change, " ", tractor_vertical_index + vertical_change)
print(horizontal_change, " ", vertical_change)
pygame.quit()
quit()