AI_PRO/drawUI.py
2021-05-31 21:09:45 +02:00

103 lines
4.2 KiB
Python

from TractorLoad import TillageUnit
from constants import *
from images import *
import pygame
def drawUI(board, display, tractor, direction, tillageUnit, field, animationSpeed, weather, day_time, temperature, wind, humidy, decision):
if animationSpeed == 1:
display.fill(WHITE)
makeField(board, display)
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction, 0)
drawInfo(display, tractor, tillageUnit, field, direction, weather, day_time, temperature, wind, humidy, decision)
pygame.display.update()
for i in range(1, animationSpeed):
display.fill(WHITE)
makeField(board, display)
drawTractor(display, tractor.horizontal_index, tractor.vertical_index, direction, i)
drawInfo(display, tractor, tillageUnit, field, direction)
pygame.display.update()
def drawInfo(display, tractor, tillageUnit, field, direction, weather, day_time, temperature, wind, humidy, decision):
myfont = pygame.font.SysFont('Comic Sans MS', 30)
hitches = tractor.hitch
if isinstance(tractor.hitch, TillageUnit):
hitches = "Tillage Unit"
if decision == [0]:
decision = "Wait"
else:
decision = "Make Action"
text = f"(Q)Hitches: {hitches} (W)Header: {tractor.header} (R)Engine working: {tractor.engineWorking} (T)Autodrive: {tractor.autodrive} Fuel: {tractor.fuel_tank}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 400)))
text = f"(E)Tillage Unit Load: {tillageUnit.load} (^) Direction: {direction}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 350)))
text = f"Current field: {field.horizontal_index/100, field.vertical_index/100, field.state} Action: {decision}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 300)))
text = f"Weather: {weather} Day Time: {day_time} Temperature: {temperature} Wind: {wind} Humidy: {humidy}"
textsurface = myfont.render(text, False, (0, 0, 0))
display.blit(textsurface, (50, (DISPLAY_SIZE_VERTICAL - 250)))
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 == "toWater":
to_water_rect.center = (pos_x, pos_y)
screen.blit(to_water, to_water_rect)
elif field.state == "toPlow":
to_plow_rect.center = (pos_x, pos_y)
screen.blit(to_plow, to_plow_rect)
elif field.state == "toSeed":
to_seed_rect.center = (pos_x, pos_y)
screen.blit(to_seed, to_seed_rect)
elif field.state == "toCut":
to_cut_rect.center = (pos_x, pos_y)
screen.blit(to_cut, to_cut_rect)
elif field.state == "toFertilize":
to_fertilizer_rect.center = (pos_x, pos_y)
screen.blit(to_fertilizer, to_fertilizer_rect)
elif field.state == "TOOLS_FIELD":
tools_rect.center = (pos_x, pos_y)
screen.blit(tools, tools_rect)
elif field.state == "FUEL_FIELD":
fuel_rect.center = (pos_x, pos_y)
screen.blit(fuel, fuel_rect)
def drawTractor(screen: pygame.Surface, tractor_horizontal_index, tractor_vertical_index, direction, i):
tractor_pic = tractor_up
horizontal = tractor_horizontal_index * TILE_SIZE
vertical = tractor_vertical_index * TILE_SIZE
i = i/ANIMATION_PART
if direction == "UP":
tractor_pic = tractor_up
vertical = vertical - (TILE_SIZE * i)
if direction == "DOWN":
tractor_pic = tractor_down
vertical = vertical + (TILE_SIZE * i)
if direction == "LEFT":
tractor_pic = tractor_left
horizontal = horizontal - (TILE_SIZE * i)
if direction == "RIGHT":
tractor_pic = tractor_right
horizontal = horizontal + (TILE_SIZE * i)
screen.blit(tractor_pic, (horizontal, vertical))