feat(game): add block colors

Each block value has different color
This commit is contained in:
Kristofers Solo 2023-12-26 21:52:14 +02:00
parent 789bebabfa
commit e45e6b16b3
2 changed files with 46 additions and 1 deletions

View File

@ -28,3 +28,37 @@ class Block(pygame.sprite.Sprite):
if 0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE:
self.rect.x = new_x
self.rect.y = new_y
def increase_value(self, num: int = 2) -> None:
"""Increase the value of the block `num` times"""
self.value *= num
self.update()
def update(self) -> None:
self.change_color()
self.draw_value()
def change_color(self) -> None:
match self.value:
case 2:
self.image.fill(COLORS.BLUE)
case 4:
self.image.fill(COLORS.BLUE0)
case 8:
self.image.fill(COLORS.BLUE1)
case 16:
self.image.fill(COLORS.BLUE2)
case 32:
self.image.fill(COLORS.DARK3)
case 64:
self.image.fill(COLORS.BORDER_HIGHLIGHT)
case 128:
self.image.fill(COLORS.BLUE5)
case 256:
self.image.fill(COLORS.BLUE6)
case 512:
self.image.fill(COLORS.BLUE7)
case 1024:
self.image.fill(COLORS.ORANGE)
case 2048:
self.image.fill(COLORS.RED)

View File

@ -46,10 +46,21 @@ class Game:
elif event.key == pygame.K_q:
self.exit()
def move_blocks(self, dx, dy) -> None:
def move_blocks(self, dx: int, dy: int) -> None:
moved_blocks = pygame.sprite.Group()
for block in self.sprites:
block.move(dx, dy)
for block in self.sprites: # FIX: different value block merge
collidin_blocks = pygame.sprite.spritecollide(block, self.sprites, False)
for other_block in collidin_blocks:
if block != other_block and block.value == other_block.value and other_block not in moved_blocks:
block.increase_value()
self.sprites.remove(other_block)
moved_blocks.add(block)
self.update()
def generate_random_block(self, count: int) -> None:
for _ in range(count):
while True: