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

View File

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

View File

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