mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): add block colors
Each block value has different color
This commit is contained in:
parent
789bebabfa
commit
e45e6b16b3
@ -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:
|
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.x = new_x
|
||||||
self.rect.y = new_y
|
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)
|
||||||
|
|||||||
@ -46,10 +46,21 @@ class Game:
|
|||||||
elif event.key == pygame.K_q:
|
elif event.key == pygame.K_q:
|
||||||
self.exit()
|
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:
|
for block in self.sprites:
|
||||||
block.move(dx, dy)
|
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:
|
def generate_random_block(self, count: int) -> None:
|
||||||
for _ in range(count):
|
for _ in range(count):
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user