added display_assets, added new tiles (mud, grass and concrete)

This commit is contained in:
s452635 2021-05-03 20:03:32 +02:00
parent d47e46313c
commit b9128bc7c8
38 changed files with 409 additions and 61 deletions

View File

@ -3,45 +3,228 @@ import pygame
import project_constants as const
# moved here from minefield
def calculate_screen_position(row, column):
# ====================== #
# === MAIN FUNCTIONS === #
# ====================== #
def test_blits():
display_concrete((2, 3))
display_mud((5, 6))
display_mud((5, 7))
display_grass((2, 1))
display_mine((2, 7))
display_mine((5, 7))
display_concrete((0, 0))
display_chained_mine((1, 2), (2, 3))
display_chained_mine((2, 3))
display_chained_mine((4, 8), (11, 15))
display_time_mine((1, 8), 12)
display_time_mine((2, 8), 34)
def blit_graphics(minefield):
# background grid (fills frame with white, blits grid)
const.SCREEN.fill((255, 255, 255))
const.SCREEN.blit(
const.ASSET_BACKGROUND,
(
const.V_SCREEN_PADDING,
const.V_SCREEN_PADDING
)
)
# draw tiles and mines from minefield
minefield.draw(const.SCREEN)
# all the tests in one place
test_blits()
# 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 * column,
const.V_NUMBER_PADDING + const.V_SCREEN_PADDING + const.V_TILE_SIZE * row,
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 display_sapper(sapper_x, sapper_y, sapper_dir):
# ============== #
# === SAPPER === #
# ============== #
sapper_screen_coords = calculate_screen_position(
sapper_x,
sapper_y
)
if sapper_dir == const.Direction.UP:
def display_sapper(coords, direction):
sapper_screen_coords = calculate_screen_position(coords)
if direction == const.Direction.UP:
const.SCREEN.blit(
const.ASSET_SAPPER,
sapper_screen_coords
)
if sapper_dir == const.Direction.RIGHT:
elif direction == const.Direction.RIGHT:
const.SCREEN.blit(
pygame.transform.rotate(const.ASSET_SAPPER, 270),
sapper_screen_coords
)
if sapper_dir == const.Direction.DOWN:
elif direction == const.Direction.DOWN:
const.SCREEN.blit(
pygame.transform.rotate(const.ASSET_SAPPER, 180),
sapper_screen_coords
)
if sapper_dir == const.Direction.LEFT:
elif direction == const.Direction.LEFT:
const.SCREEN.blit(
pygame.transform.rotate(const.ASSET_SAPPER, 90),
sapper_screen_coords
)
# ============= #
# === TILES === #
# ============= #
def display_concrete(coords):
const.SCREEN.blit(
const.ASSET_CONCRETE,
calculate_screen_position(coords)
)
def display_mud(coords):
const.SCREEN.blit(
const.ASSET_MUD,
calculate_screen_position(coords)
)
def display_grass(coords):
const.SCREEN.blit(
const.ASSET_GRASS,
calculate_screen_position(coords)
)
# ============= #
# === MINES === #
# ============= #
def display_mine(coords):
const.SCREEN.blit(
const.ASSET_MINE,
calculate_screen_position(coords)
)
def display_chained_mine(coords, parent_coords=None):
def display_number(which_coord, number):
number_asset = const.ASSET_NUMBER_CHAINS_0
if number == 1:
number_asset = const.ASSET_NUMBER_CHAINS_1
elif number == 3:
number_asset = const.ASSET_NUMBER_CHAINS_3
elif number == 4:
number_asset = const.ASSET_NUMBER_CHAINS_4
elif number == 5:
number_asset = const.ASSET_NUMBER_CHAINS_5
elif number == 6:
number_asset = const.ASSET_NUMBER_CHAINS_6
elif number == 7:
number_asset = const.ASSET_NUMBER_CHAINS_7
elif number == 8:
number_asset = const.ASSET_NUMBER_CHAINS_8
elif number == 9:
number_asset = const.ASSET_NUMBER_CHAINS_9
elif number == 10:
number_asset = const.ASSET_NUMBER_CHAINS_10
elif number == 11:
number_asset = const.ASSET_NUMBER_CHAINS_11
elif number == 12:
number_asset = const.ASSET_NUMBER_CHAINS_12
elif number == 13:
number_asset = const.ASSET_NUMBER_CHAINS_13
elif number == 14:
number_asset = const.ASSET_NUMBER_CHAINS_14
elif number == 15:
number_asset = const.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)
if parent_coords is not None:
const.SCREEN.blit(
const.ASSET_CHAINS,
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, time):
def display_number(which_digit, number):
number_asset = const.ASSET_NUMBER_TIME_0
if number == 1:
number_asset = const.ASSET_NUMBER_TIME_1
elif number == 2:
number_asset = const.ASSET_NUMBER_TIME_2
elif number == 3:
number_asset = const.ASSET_NUMBER_TIME_3
elif number == 4:
number_asset = const.ASSET_NUMBER_TIME_4
elif number == 5:
number_asset = const.ASSET_NUMBER_TIME_5
elif number == 6:
number_asset = const.ASSET_NUMBER_TIME_6
elif number == 7:
number_asset = const.ASSET_NUMBER_TIME_7
elif number == 8:
number_asset = const.ASSET_NUMBER_TIME_8
elif number == 9:
number_asset = const.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] + 36, mine_coords[1] + 22)
elif which_digit == const.Digit.TENS:
number_coords = (mine_coords[0] + 44, mine_coords[1] + 22)
const.SCREEN.blit(
number_asset,
number_coords
)
const.SCREEN.blit(
const.ASSET_TIME_MINE,
calculate_screen_position(coords)
)
display_number(const.Digit.ONES, int(str(time)[-1]))
display_number(const.Digit.TENS, int(str(time)[-2]))

45
main.py
View File

@ -8,20 +8,21 @@ from pyglet.gl import * # for blocky textures
import project_constants as const
import minefield as mf
import searching_algorithms.bfs as bfs
from display_assets import display_sapper
from display_assets import blit_graphics
def main():
pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
# FPS clock
clock = pygame.time.Clock()
# for blocky textures
glEnable(GL_TEXTURE_2D)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# FPS clock
clock = pygame.time.Clock()
# create an instance of Minefield, pass necessary data
minefield = mf.Minefield(const.MAP_RANDOM_10x10)
@ -31,43 +32,22 @@ def main():
row=minefield.agent.position[0],
column=minefield.agent.position[1],
direction=const.Direction.UP),
minefield=minefield)
minefield=minefield)
running = True
while running:
# FPS control
clock.tick(const.V_FPS)
# ================ #
# === GRAPHICS === #
# ================ #
# background grid (fills frame with white, blits grid)
const.SCREEN.fill((255, 255, 255))
const.SCREEN.blit(
const.ASSET_BACKGROUND,
(
const.V_SCREEN_PADDING,
const.V_SCREEN_PADDING
)
)
# draw tiles and mines
minefield.draw(const.SCREEN)
# sapper
display_sapper(
minefield.agent.position[0],
minefield.agent.position[1],
minefield.agent.direction
)
# update graphics after blitting
pygame.display.update()
# graphics (from display_assets)
blit_graphics(minefield)
# make the next move from sequence of actions
if any(action_sequence):
action = action_sequence.pop(0)
if action == const.Action.ROTATE_LEFT:
minefield.agent.rotate_left()
elif action == const.Action.ROTATE_RIGHT:
@ -78,13 +58,12 @@ def main():
time.sleep(const.ACTION_INTERVAL)
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Assigning all input from keyboard as variables into an array
if __name__ == "__main__":
main()

View File

@ -39,7 +39,6 @@ SCREEN = pygame.display.set_mode(
# ==== ENUMS ==== #
# =============== #
class Direction(Enum):
UP = 0
RIGHT = 1
@ -61,6 +60,16 @@ class Action(Enum):
GO = 2
class Digit(Enum):
ONES = 0
TENS = 1
class Coords(Enum):
X = 0
Y = 1
# =============== #
# === STRUCTS === #
# =============== #
@ -104,12 +113,14 @@ STRUCT_MINE_ATTRIBUTE_TYPES = {
"time": [int]
}
# ============== #
# ==== MAPS ==== #
# ============== #
MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "secondmap.json")
# ============== #
# === ASSETS === #
# ============== #
@ -129,57 +140,232 @@ ASSET_WALL = pygame.transform.scale(
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CONCRETE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "concrete.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MUD = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "mud.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_GRASS = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "grass.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "mine.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CHAINS = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chains.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TIME_MINE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_mine.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
# ==================== #
# === TIME NUMBERS === #
# ==================== #
ASSET_NUMBER_TIME_0 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_0.png")),
(6, 16)
)
ASSET_NUMBER_TIME_1 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_1.png")),
(6, 16)
)
ASSET_NUMBER_TIME_2 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_2.png")),
(6, 16)
)
ASSET_NUMBER_TIME_3 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_3.png")),
(6, 16)
)
ASSET_NUMBER_TIME_4 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_4.png")),
(6, 16)
)
ASSET_NUMBER_TIME_5 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_5.png")),
(6, 16)
)
ASSET_NUMBER_TIME_6 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_6.png")),
(6, 16)
)
ASSET_NUMBER_TIME_7 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_7.png")),
(6, 16)
)
ASSET_NUMBER_TIME_8 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_8.png")),
(6, 16)
)
ASSET_NUMBER_TIME_9 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_9.png")),
(6, 16)
)
# ====================== #
# === CHAINS NUMBERS === #
# ====================== #
ASSET_NUMBER_CHAINS_0 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_0.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_1 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_1.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_2 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_2.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_3 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_3.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_4 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_4.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_5 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_5.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_6 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_6.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_7 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_7.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_8 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_8.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_9 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_9.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_10 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_10.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_11 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_11.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_12 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_12.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_13 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_13.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_14 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_14.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_15 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_15.png")),
(6, 12)
)
# ================== #
# === OLD ASSETS === #
# ================== #
ASSET_MINE_A = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple assets/mine_a.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_a.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_B = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple assets/mine_b.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_b.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_F = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple assets/mine_f.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_f.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_K = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple assets/mine_k.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_k.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_ORANGE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_orange.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_orange.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_RED = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_red.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_red.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_BLUE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_blue.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_blue.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_PURPLE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_purple.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_purple.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_GREEN = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_green.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_green.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_YELLOW = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_yellow.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_yellow.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_WHITE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "tile_white.png")),
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_white.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)

BIN
resources/assets/chains.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 423 B

View File

Before

Width:  |  Height:  |  Size: 210 B

After

Width:  |  Height:  |  Size: 210 B

View File

Before

Width:  |  Height:  |  Size: 214 B

After

Width:  |  Height:  |  Size: 214 B

View File

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

Before

Width:  |  Height:  |  Size: 198 B

After

Width:  |  Height:  |  Size: 198 B

BIN
resources/assets/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 472 B

BIN
resources/assets/mud.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 926 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 903 B

After

Width:  |  Height:  |  Size: 909 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 82 B

After

Width:  |  Height:  |  Size: 82 B

View File

Before

Width:  |  Height:  |  Size: 109 B

After

Width:  |  Height:  |  Size: 109 B

View File

Before

Width:  |  Height:  |  Size: 110 B

After

Width:  |  Height:  |  Size: 110 B

View File

Before

Width:  |  Height:  |  Size: 108 B

After

Width:  |  Height:  |  Size: 108 B

View File

Before

Width:  |  Height:  |  Size: 116 B

After

Width:  |  Height:  |  Size: 116 B

View File

Before

Width:  |  Height:  |  Size: 479 B

After

Width:  |  Height:  |  Size: 479 B

View File

Before

Width:  |  Height:  |  Size: 153 B

After

Width:  |  Height:  |  Size: 153 B

View File

Before

Width:  |  Height:  |  Size: 81 B

After

Width:  |  Height:  |  Size: 81 B

View File

Before

Width:  |  Height:  |  Size: 90 B

After

Width:  |  Height:  |  Size: 90 B

View File

Before

Width:  |  Height:  |  Size: 96 B

After

Width:  |  Height:  |  Size: 96 B

View File

Before

Width:  |  Height:  |  Size: 92 B

After

Width:  |  Height:  |  Size: 92 B

View File

Before

Width:  |  Height:  |  Size: 87 B

After

Width:  |  Height:  |  Size: 87 B

View File

Before

Width:  |  Height:  |  Size: 84 B

After

Width:  |  Height:  |  Size: 84 B

View File

Before

Width:  |  Height:  |  Size: 91 B

After

Width:  |  Height:  |  Size: 91 B

View File

Before

Width:  |  Height:  |  Size: 77 B

After

Width:  |  Height:  |  Size: 77 B

View File

Before

Width:  |  Height:  |  Size: 95 B

After

Width:  |  Height:  |  Size: 95 B

View File

Before

Width:  |  Height:  |  Size: 87 B

After

Width:  |  Height:  |  Size: 87 B