little bit of code clean-up

This commit is contained in:
s452635 2021-03-14 19:18:23 +01:00
parent 691fcb1ca9
commit aa4762867b
3 changed files with 36 additions and 35 deletions

19
main.py
View File

@ -3,20 +3,20 @@ import pygame
from pyglet.gl import * # for blocky textures from pyglet.gl import * # for blocky textures
# other files of this project # other files of this project
import project_constants import project_constants as const
import minefield as mf import minefield as mf
def main(): def main():
pygame.init() pygame.init()
pygame.display.set_caption(project_constants.V_NAME_OF_WINDOW) pygame.display.set_caption(const.V_NAME_OF_WINDOW)
# for blocky textures # for blocky textures
glEnable(GL_TEXTURE_2D) glEnable(GL_TEXTURE_2D)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# create an instance of Minefield, pass necessary data # create an instance of Minefield, pass necessary data
minefield = mf.Minefield(project_constants.MAP_RANDOM_10x10) minefield = mf.Minefield(const.MAP_RANDOM_10x10)
running = True running = True
while running: while running:
@ -26,17 +26,17 @@ def main():
# ================ # # ================ #
# background grid (fills frame with white, blits grid) # background grid (fills frame with white, blits grid)
project_constants.SCREEN.fill((255, 255, 255)) const.SCREEN.fill((255, 255, 255))
project_constants.SCREEN.blit( const.SCREEN.blit(
project_constants.ASSET_BACKGROUND, const.ASSET_BACKGROUND,
( (
project_constants.V_SCREEN_PADDING, const.V_SCREEN_PADDING,
project_constants.V_SCREEN_PADDING const.V_SCREEN_PADDING
) )
) )
# draw tiles and mines # draw tiles and mines
minefield.draw(project_constants.SCREEN) minefield.draw(const.SCREEN)
pygame.display.update() pygame.display.update()
# ============== # # ============== #
@ -48,7 +48,6 @@ def main():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
# else: event_interpreter.interpret( event )
# Assigning all input from keyboard as variables into an array # Assigning all input from keyboard as variables into an array
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()

View File

@ -1,31 +1,30 @@
import pygame
import json import json
import project_constants as consts import project_constants as const
import tile as tl import tile as tl
import mine as mn import mine as mn
tile_asset_options = { tile_asset_options = {
"BLUE": consts.ASSET_TILE_BLUE, "BLUE": const.ASSET_TILE_BLUE,
"GREEN": consts.ASSET_TILE_GREEN, "GREEN": const.ASSET_TILE_GREEN,
"ORANGE": consts.ASSET_TILE_ORANGE, "ORANGE": const.ASSET_TILE_ORANGE,
"PURPLE": consts.ASSET_TILE_PURPLE, "PURPLE": const.ASSET_TILE_PURPLE,
"RED": consts.ASSET_TILE_RED, "RED": const.ASSET_TILE_RED,
"WHITE": consts.ASSET_TILE_WHITE, "WHITE": const.ASSET_TILE_WHITE,
"YELLOW": consts.ASSET_TILE_YELLOW "YELLOW": const.ASSET_TILE_YELLOW
} }
mine_asset_options = { mine_asset_options = {
'A': consts.ASSET_MINE_A, 'A': const.ASSET_MINE_A,
'B': consts.ASSET_MINE_B, 'B': const.ASSET_MINE_B,
'F': consts.ASSET_MINE_F, 'F': const.ASSET_MINE_F,
'K': consts.ASSET_MINE_K 'K': const.ASSET_MINE_K
} }
def calculate_screen_position(x, y): def calculate_screen_position(x, y):
coords = ( coords = (
consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * x, const.V_SCREEN_PADDING + const.V_TILE_SIZE * x,
consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * y, const.V_SCREEN_PADDING + const.V_TILE_SIZE * y,
) )
return coords return coords
@ -41,13 +40,13 @@ class Minefield:
# create matrix of a desired size, fill it with default tile objects # create matrix of a desired size, fill it with default tile objects
self.matrix = [ self.matrix = [
[ [
tl.Tile((i, j)) for i in range(consts.V_GRID_VER_TILES) tl.Tile((i, j)) for i in range(const.V_GRID_VER_TILES)
] for j in range(consts.V_GRID_HOR_TILES) ] for j in range(const.V_GRID_HOR_TILES)
] ]
# iterate through tiles, set their colors and add mines # iterate through tiles, set their colors and add mines
for x in range(consts.V_GRID_HOR_TILES): for x in range(const.V_GRID_HOR_TILES):
for y in range(consts.V_GRID_VER_TILES): for y in range(const.V_GRID_VER_TILES):
# load tile's data from json # load tile's data from json
tile_data = data[f"{x},{y}"] tile_data = data[f"{x},{y}"]
@ -82,18 +81,24 @@ class Minefield:
# draw the sapper # draw the sapper
sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_position[1]) sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_position[1])
window.blit(consts.ASSET_SAPPER, sapper_screen_coords) window.blit(const.ASSET_SAPPER, sapper_screen_coords)
# ================ #
# === MOVEMENT === #
# ================ #
# Make sure that sapper won't step on the mine. # Make sure that sapper won't step on the mine.
def check_legal_move(self, target): def check_legal_move(self, target):
if self.matrix[target[1]][target[0]].mine is None: if self.matrix[target[1]][target[0]].mine is None:
return target return target
else: else:
return self.sapper_position return self.sapper_position
# Here are defined functions that move our agent. They are being called in main when certain key is pressed # Here are defined functions that move our agent. They are being called in main when certain key is pressed
def go_right(self): def go_right(self):
if self.sapper_position[0] < consts.V_GRID_HOR_TILES - 1: if self.sapper_position[0] < const.V_GRID_HOR_TILES - 1:
self.sapper_position = self.check_legal_move( self.sapper_position = self.check_legal_move(
(int(self.sapper_position[0]) + 1, int(self.sapper_position[1]))) (int(self.sapper_position[0]) + 1, int(self.sapper_position[1])))
@ -108,6 +113,6 @@ class Minefield:
(int(self.sapper_position[0]), int(self.sapper_position[1]) - 1)) (int(self.sapper_position[0]), int(self.sapper_position[1]) - 1))
def go_down(self): def go_down(self):
if self.sapper_position[1] < consts.V_GRID_VER_TILES - 1: if self.sapper_position[1] < const.V_GRID_VER_TILES - 1:
self.sapper_position = self.check_legal_move( self.sapper_position = self.check_legal_move(
(int(self.sapper_position[0]), int(self.sapper_position[1]) + 1)) (int(self.sapper_position[0]), int(self.sapper_position[1]) + 1))

View File

@ -1,6 +1,3 @@
import mine as mn
class Tile: class Tile:
def __init__(self, position, color=None, mine=None): def __init__(self, position, color=None, mine=None):
self.position = position self.position = position