Projekt_Sztuczna_Inteligencja/assets/display_assets.py

287 lines
8.2 KiB
Python

import pygame
import project_constants as const
from assets import asset_constants as asset
from objects.mine_models.standard_mine import StandardMine
from objects.mine_models.chained_mine import ChainedMine
from objects.mine_models.time_mine import TimeMine
# ================================= #
# === SO THE OLD BLITTING WORKS === #
# ================================= #
tile_asset_options = {
"MUD": asset.ASSET_MUD,
"GRASS": asset.ASSET_GRASS,
"CONCRETE": asset.ASSET_CONCRETE
}
mine_asset_options = {
"MINE": asset.ASSET_MINE,
"CHAINS": asset.ASSET_CHAINS,
"TIME_MINE": asset.ASSET_TIME_MINE,
}
# ====================== #
# === MAIN FUNCTIONS === #
# ====================== #
def blit_graphics(minefield):
# background grid (fills frame with white, blits grid)
const.SCREEN.fill((255, 255, 255))
const.SCREEN.blit(
asset.ASSET_BACKGROUND,
(
const.V_SCREEN_PADDING,
const.V_SCREEN_PADDING
)
)
for row in minefield.matrix:
for tile in row:
# calculate tile position on the screen
tile_screen_coords = calculate_screen_position(tile.position)
# draw a tile
const.SCREEN.blit(
tile_asset_options.get(tile.terrain_type),
tile_screen_coords
)
# darkening every other tile
if (tile.position[0]+tile.position[1]) % 2 == 0:
pass # darken_tile(tile.position)
# draw a mine on top if there is one
if tile.mine is not None:
if isinstance(tile.mine, StandardMine):
const.SCREEN.blit(mine_asset_options["MINE"], tile_screen_coords)
if isinstance(tile.mine, ChainedMine):
predecessor = tile.mine.predecessor
predecessor_position = \
predecessor.position if predecessor is not None and predecessor.active else None
display_chained_mine(tile.position, predecessor_position)
elif isinstance(tile.mine, TimeMine):
minutes = int(tile.mine.timer / 60)
seconds = tile.mine.timer % 60
display_time_mine(tile.position, minutes, "0" + str(seconds))
# changed sapper's movement from jumping to continuous movement (moved everything to Agent's class)
# # sapper
# display_sapper(
# minefield.agent.position,
# minefield.agent.direction
# )
# update graphics after blitting
# pygame.display.update()
# moved here from minefield
def calculate_screen_position(coords):
coords = (
const.V_NUMBER_PADDING + const.V_SCREEN_PADDING + const.V_TILE_SIZE * coords[1],
const.V_NUMBER_PADDING + const.V_SCREEN_PADDING + const.V_TILE_SIZE * coords[0],
)
return coords
def darken_tile(coords):
shadow = pygame.Surface((const.V_TILE_SIZE, const.V_TILE_SIZE))
shadow.fill((220, 220, 220))
const.SCREEN.blit(
shadow,
calculate_screen_position(coords),
special_flags=pygame.BLEND_RGB_MULT
)
# ============== #
# === SAPPER === #
# ============== #
def display_sapper(coords, direction):
sapper_screen_coords = calculate_screen_position(coords)
if direction == const.Direction.UP:
const.SCREEN.blit(
asset.ASSET_SAPPER,
sapper_screen_coords
)
elif direction == const.Direction.RIGHT:
const.SCREEN.blit(
pygame.transform.rotate(asset.ASSET_SAPPER, 270),
sapper_screen_coords
)
elif direction == const.Direction.DOWN:
const.SCREEN.blit(
pygame.transform.rotate(asset.ASSET_SAPPER, 180),
sapper_screen_coords
)
elif direction == const.Direction.LEFT:
const.SCREEN.blit(
pygame.transform.rotate(asset.ASSET_SAPPER, 90),
sapper_screen_coords
)
# ============= #
# === TILES === #
# ============= #
def display_concrete(coords):
const.SCREEN.blit(
asset.ASSET_CONCRETE,
calculate_screen_position(coords)
)
def display_mud(coords):
const.SCREEN.blit(
asset.ASSET_MUD,
calculate_screen_position(coords)
)
def display_grass(coords):
const.SCREEN.blit(
asset.ASSET_GRASS,
calculate_screen_position(coords)
)
# ============= #
# === MINES === #
# ============= #
def display_mine(coords):
const.SCREEN.blit(
asset.ASSET_MINE,
calculate_screen_position(coords)
)
def display_chained_mine(coords, parent_coords=None):
def display_number(which_coord, number):
number_asset = asset.ASSET_NUMBER_CHAINS_0
if number == 1:
number_asset = asset.ASSET_NUMBER_CHAINS_1
elif number == 3:
number_asset = asset.ASSET_NUMBER_CHAINS_3
elif number == 4:
number_asset = asset.ASSET_NUMBER_CHAINS_4
elif number == 5:
number_asset = asset.ASSET_NUMBER_CHAINS_5
elif number == 6:
number_asset = asset.ASSET_NUMBER_CHAINS_6
elif number == 7:
number_asset = asset.ASSET_NUMBER_CHAINS_7
elif number == 8:
number_asset = asset.ASSET_NUMBER_CHAINS_8
elif number == 9:
number_asset = asset.ASSET_NUMBER_CHAINS_9
elif number == 10:
number_asset = asset.ASSET_NUMBER_CHAINS_10
elif number == 11:
number_asset = asset.ASSET_NUMBER_CHAINS_11
elif number == 12:
number_asset = asset.ASSET_NUMBER_CHAINS_12
elif number == 13:
number_asset = asset.ASSET_NUMBER_CHAINS_13
elif number == 14:
number_asset = asset.ASSET_NUMBER_CHAINS_14
elif number == 15:
number_asset = asset.ASSET_NUMBER_CHAINS_15
mine_coords = calculate_screen_position(coords)
number_coords = mine_coords
if which_coord == const.Coords.X:
number_coords = (mine_coords[0] + 20, mine_coords[1] + 24)
elif which_coord == const.Coords.Y:
number_coords = (mine_coords[0] + 34, mine_coords[1] + 24)
const.SCREEN.blit(
number_asset,
number_coords
)
display_mine(coords)
const.SCREEN.blit(
asset.ASSET_CHAINS,
calculate_screen_position(coords)
)
if parent_coords is not None:
const.SCREEN.blit(
asset.ASSET_CHAIN_COUNTER,
calculate_screen_position(coords)
)
display_number(const.Coords.X, parent_coords[0])
display_number(const.Coords.Y, parent_coords[1])
def display_time_mine(coords, minutes, seconds):
def display_number(which_digit, number):
number_asset = asset.ASSET_NUMBER_TIME_0
if number == 1:
number_asset = asset.ASSET_NUMBER_TIME_1
elif number == 2:
number_asset = asset.ASSET_NUMBER_TIME_2
elif number == 3:
number_asset = asset.ASSET_NUMBER_TIME_3
elif number == 4:
number_asset = asset.ASSET_NUMBER_TIME_4
elif number == 5:
number_asset = asset.ASSET_NUMBER_TIME_5
elif number == 6:
number_asset = asset.ASSET_NUMBER_TIME_6
elif number == 7:
number_asset = asset.ASSET_NUMBER_TIME_7
elif number == 8:
number_asset = asset.ASSET_NUMBER_TIME_8
elif number == 9:
number_asset = asset.ASSET_NUMBER_TIME_9
mine_coords = calculate_screen_position(coords)
number_coords = mine_coords
if which_digit == const.Digit.ONES:
number_coords = (mine_coords[0] + 44, mine_coords[1] + 22)
elif which_digit == const.Digit.TENS:
number_coords = (mine_coords[0] + 36, mine_coords[1] + 22)
elif which_digit == const.Digit.MINUTES:
number_coords = (mine_coords[0] + 18, mine_coords[1] + 22)
const.SCREEN.blit(
number_asset,
number_coords
)
const.SCREEN.blit(
asset.ASSET_TIME_MINE,
calculate_screen_position(coords)
)
display_number(const.Digit.ONES, int(str(seconds)[-1]))
display_number(const.Digit.TENS, int(str(seconds)[-2]))
display_number(const.Digit.MINUTES, minutes)