import dataclasses import json import os.path import sys from enum import Enum import pygame from Serializer import EnumSerializer CONFIG_JSON = 'configs/config.json' CONFIG_BLOCKS_JSON = 'configs/blocks.json' CONFIG_ADD_BLOCKS_JSON = 'configs/blocks_ADD.json' @dataclasses.dataclass class Block: name: str color: tuple[int, int, int] min_score: int shapes: list class BlockType(Enum): """ Define block types with their shapes and colors All shapes draws starting in top left corner from left to right """ O = { 'name': 'Square', 'color': (255, 255, 0), # Yellow 'min_score': 0, 'shapes': [ [ [1, 1], [1, 1] ] ] } I = { 'name': 'Line', 'color': (0, 255, 255), # Cyan 'min_score': 0, 'shapes': [ [[1, 1, 1, 1]], [[1], [1], [1], [1]] ] } L = { 'name': 'L-Shape', 'color': (255, 0, 255), # Magenta 'min_score': 0, 'shapes': [ [[1, 0], [1, 0], [1, 1]], [[1, 1, 1], [1, 0, 0]], [[1, 1], [0, 1], [0, 1]], [[0, 0, 1], [1, 1, 1]] ] } J = { 'name': 'J-Shape', 'color': (0, 0, 255), # Blue 'min_score': 900, 'shapes': [ [[0, 1], [0, 1], [1, 1]], [[1, 0, 0], [1, 1, 1]], [[1, 1], [1, 0], [1, 0]], [[1, 1, 1], [0, 0, 1]] ] } S = { 'name': 'S-Shape', 'color': (0, 255, 0), # Green 'min_score': 1000, 'shapes': [ [[0, 1, 1], [1, 1, 0]], [[1, 0], [1, 1], [0, 1]] ] } Z = { 'name': 'Z-Shape', 'color': (255, 0, 0), # Red 'min_score': 2500, 'shapes': [ [[1, 1, 0], [0, 1, 1]], [[0, 1], [1, 1], [1, 0]] ] } T = { 'name': 'T-Shape', 'color': (255, 255, 255), # White 'min_score': 2500, 'shapes': [ [[1, 1, 1], [0, 1, 0]], [[1, 0], [1, 1], [1, 0]], [[0, 1, 0], [1, 1, 1]], [[0, 1], [1, 1], [0, 1]] ] } U = { 'name': 'U-Shape', 'color': (135, 135, 135), # Change color 'min_score': 4500, 'shapes': [ [[1, 0, 1], [1, 1, 1]], [[1, 1], [1, 0], [1, 1]], [[1, 1, 1], [1, 0, 1]], [[1, 1], [0, 1], [1, 1]] ] } # T1 = { # 'name': 'T-Shape+', # 'color': (200, 200, 200), # Change color # 'min_score': 10000, # 'shapes': [ # [[1, 1, 1], [0, 1, 0], [0, 1, 0]], # [[0, 0, 1], [1, 1, 1], [0, 0, 1]], # [[0, 1, 0], [0, 1, 0], [1, 1, 1]], # [[1, 0, 0], [1, 1, 1], [1, 0, 0]] # ] # } # U1 = { # 'name': 'U-Shape+', # 'color': (250, 250, 250), # Change color # 'min_score': 10000, # 'shapes': [ # [[1, 1, 1], [1, 0, 1], [1, 0, 1]], # [[1, 1, 1], [1, 0, 0], [1, 1, 1]], # [[1, 0, 1], [1, 0, 1], [1, 1, 1]], # [[1, 1, 1], [0, 0, 1], [1, 1, 1]] # ] # } # X = { # 'name': 'X-Shape', # 'color': (250, 250, 250), # Change color # 'min_score': 15000, # 'shapes': [ # [[0, 1, 0], [1, 1, 1], [0, 1, 0]] # ] # } BLOCK_TYPE = BlockType # Key configuration KEY_CONFIG = { 'left': pygame.K_LEFT, 'right': pygame.K_RIGHT, 'rotate': pygame.K_UP, 'drop': pygame.K_DOWN, 'pause': pygame.K_p, 'restart': pygame.K_r, 'exit': pygame.K_ESCAPE } CONTROLS_TEXT = [ "Controls:", "Left/Right Move", "Up - Rotate", "Dn - Drop", "P - Pause", "R - Restart", "ESC - Exit" ] # Store width configuration STORE_WIDTH = 10 # number of blocks wide BLOCK_SIZE = 30 # pixels per block # Colors COLORS = { 'background': (20, 20, 40), 'field': (30, 30, 60), 'border': (100, 100, 150), 'text': (255, 255, 255), 'game_over': (255, 0, 0) } def store_config(): blocks_to_save = {'ALL_BLOCKS': BlockType} es = EnumSerializer() es.save_to_file(blocks_to_save, CONFIG_BLOCKS_JSON) config_to_save = { 'CONFIG': { 'keys': KEY_CONFIG, 'controls_text': CONTROLS_TEXT, 'colors': COLORS, 'store_width': STORE_WIDTH, 'block_size': BLOCK_SIZE } } es.save_to_file(config_to_save, CONFIG_JSON) def load_config(): global KEY_CONFIG, COLORS, CONTROLS_TEXT, STORE_WIDTH, BLOCK_SIZE, BLOCK_TYPE es = EnumSerializer if not os.path.isfile(CONFIG_JSON) or not os.path.isfile(CONFIG_JSON) : print('No config files was found') sys.exit(10) config_data = es.load_from_file(CONFIG_JSON) KEY_CONFIG = config_data['CONFIG']['keys'] CONTROLS_TEXT = config_data['CONFIG']['controls_text'] COLORS = config_data['CONFIG']['colors'] STORE_WIDTH = config_data['CONFIG']['store_width'] BLOCK_SIZE = config_data['CONFIG']['block_size'] with open(CONFIG_BLOCKS_JSON) as block_fd: blocks_data = json.load(block_fd) blocks0 = blocks_data['ALL_BLOCKS']['members'] blocks1 = {} if os.path.isfile(CONFIG_ADD_BLOCKS_JSON): with open(CONFIG_ADD_BLOCKS_JSON) as block_fd: blocks_data1 = json.load(block_fd) blocks1 = blocks_data1['ADDITIONAL_BLOCKS']['members'] blocks = blocks0 | blocks1 BLOCK_TYPE = Enum('BlockType', blocks) if __name__ == '__main__': pass