Some improvements and corrections. Added 'Dot' 1x1 sape

This commit is contained in:
Anry Das 2025-11-15 16:22:24 +02:00
parent 5833b7251b
commit 836f635a6b
3 changed files with 20 additions and 52 deletions

View File

@ -3,6 +3,14 @@
"__type__": "EnumClass", "__type__": "EnumClass",
"name": "BlockType", "name": "BlockType",
"members": { "members": {
"D": {
"name": "Dot",
"color": [255, 255, 100],
"min_score": 3500,
"shapes": [
[[1]]
]
},
"U": { "U": {
"name": "U-Shape", "name": "U-Shape",
"color": [135, 135, 135], "color": [135, 135, 135],

View File

@ -63,15 +63,15 @@ class TetrisGame:
# Initialize game # Initialize game
self.spawn_piece() self.spawn_piece()
def get_block_min_score(self, block_type: cfg.BlockType) -> int: def get_block_min_score(self, block_type: BLOCK_TYPE) -> int:
"""Get minimal score of block to be shown""" """Get minimal score of block to be shown"""
return block_type.value['min_score'] return block_type.value['min_score']
def get_block_shapes(self, block_type: cfg.BlockType) -> List[List[List[int]]]: def get_block_shapes(self, block_type: BLOCK_TYPE) -> List[List[List[int]]]:
"""Get all rotation shapes for a block type""" """Get all rotation shapes for a block type"""
return block_type.value['shapes'] return block_type.value['shapes']
def get_block_color(self, block_type: cfg.BlockType) -> Tuple[int, int, int]: def get_block_color(self, block_type: BLOCK_TYPE) -> Tuple[int, int, int]:
"""Get color for a block type""" """Get color for a block type"""
return block_type.value['color'] return block_type.value['color']
@ -97,18 +97,18 @@ class TetrisGame:
self.current_block_rotation, self.current_block): self.current_block_rotation, self.current_block):
self.game_over = True self.game_over = True
def choice_random_block(self) -> cfg.BlockType: def choice_random_block(self) -> BLOCK_TYPE:
block: cfg.BlockType = random.choice(list(cfg.BlockType)) block: BLOCK_TYPE = random.choice(list(BLOCK_TYPE))
while self.get_block_min_score(block) > self.score: while self.get_block_min_score(block) > self.score:
block = random.choice(list(cfg.BlockType)) block = random.choice(list(BLOCK_TYPE))
return block return block
def get_block_shape(self, block_type: cfg.BlockType, rotation: int) -> List[List[int]]: def get_block_shape(self, block_type: BLOCK_TYPE, rotation: int) -> List[List[int]]:
"""Get the shape matrix for a block at specific rotation""" """Get the shape matrix for a block at specific rotation"""
shapes = self.get_block_shapes(block_type) shapes = self.get_block_shapes(block_type)
return shapes[rotation % len(shapes)] return shapes[rotation % len(shapes)]
def can_place_block(self, x: int, y: int, rotation: int, block_type: cfg.BlockType) -> bool: def can_place_block(self, x: int, y: int, rotation: int, block_type: BLOCK_TYPE) -> bool:
"""Check if a block can be placed at given position""" """Check if a block can be placed at given position"""
shape = self.get_block_shape(block_type, rotation) shape = self.get_block_shape(block_type, rotation)
@ -130,7 +130,7 @@ class TetrisGame:
return True return True
def place_block(self, x: int, y: int, rotation: int, block_type: cfg.BlockType, block_id: int): def place_block(self, x: int, y: int, rotation: int, block_type: BLOCK_TYPE, block_id: int):
"""Place a block on the field""" """Place a block on the field"""
shape = self.get_block_shape(block_type, rotation) shape = self.get_block_shape(block_type, rotation)
@ -176,7 +176,7 @@ class TetrisGame:
# Block can't move down, place it # Block can't move down, place it
self.place_block(self.current_block_x, self.current_block_y, self.place_block(self.current_block_x, self.current_block_y,
self.current_block_rotation, self.current_block, self.current_block_rotation, self.current_block,
list(cfg.BlockType).index(self.current_block) + 1) list(BLOCK_TYPE).index(self.current_block) + 1)
# Check for complete lines # Check for complete lines
self.clear_lines() self.clear_lines()
@ -348,7 +348,7 @@ class TetrisRenderer:
for row_idx, row in enumerate(game.field): for row_idx, row in enumerate(game.field):
for col_idx, cell in enumerate(row): for col_idx, cell in enumerate(row):
if cell != 0: if cell != 0:
block_type = list(cfg.BlockType)[cell - 1] block_type = list(BLOCK_TYPE)[cell - 1]
color = game.get_block_color(block_type) color = game.get_block_color(block_type)
x = self.field_x + col_idx * self.block_size x = self.field_x + col_idx * self.block_size
y = self.field_y + row_idx * self.block_size y = self.field_y + row_idx * self.block_size

View File

@ -23,6 +23,7 @@ class BlockType(Enum):
""" """
Define block types with their shapes and colors Define block types with their shapes and colors
All shapes draws starting in top left corner from left to right All shapes draws starting in top left corner from left to right
Every line mean the share rotated in clockwise direction
""" """
O = { O = {
'name': 'Square', 'name': 'Square',
@ -95,47 +96,6 @@ class BlockType(Enum):
[[0, 1], [1, 1], [0, 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 BLOCK_TYPE = BlockType