feat: add score, lines and level calc

This commit is contained in:
2021-06-14 14:00:33 -04:00
parent 6e954c0204
commit 14fd7dc9d1
4 changed files with 39 additions and 22 deletions

View File

@@ -16,8 +16,16 @@ image:
engine:
fps: 60
tile-size: 20
piece-gravity-time: 400
piece-gravity-time: 600
piece-set-time: 600
piece-gravity-increase: 56
lines-per-level: 5
points-per-lines-completed:
- 0 # 0 line
- 40 # 1 line
- 80 # 2 lines
- 120 # 3 lines
- 400 # 4 lines
color:
window-bg: "#000000"

View File

@@ -140,8 +140,8 @@ class Piece(Entity):
gravity_time = ConfigurationManager.get("engine", "piece-gravity-time")
set_time = ConfigurationManager.get("engine", "piece-set-time")
if Controller.key_pressed(pygame.K_DOWN):
self.gravity_time = gravity_time // 8
self.set_time = set_time // 8
self.gravity_time = max(1, gravity_time // 10)
self.set_time = max(1, set_time // 10)
if not Controller.key_pressed(pygame.K_DOWN):
self.gravity_time = gravity_time
self.set_time = set_time
@@ -151,7 +151,7 @@ class Piece(Entity):
# ghost piece
for square in self._get_ghost_piece_points(well, stack):
pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(tile_size // 6, 1)) # TDOD change white to yaml
pygame.draw.polygon(surface, pygame.Color("#FFFFFF"), square, max(tile_size // 6, 1)) # TODO add white to the yaml
super().draw(surface)
@@ -297,12 +297,18 @@ class Stack(Entity):
def __init__(self, color: str, border_color: str):
super().__init__([], color, border_color)
self.rows_completed_count = 0
self.row_completion_sound = mixer.Channel(1)
self.lines_completed_count = 0
self.line_completed_sound = mixer.Channel(1)
def update(self, elapsed_time: int) -> None:
def update(self, elapsed_time: int, game: "Game") -> None:
super().update(elapsed_time)
self.rows_completed_count += self._complete_rows()
lines_completed = self._complete_rows()
current_level = game.get_level()
points_per_lines_completed = ConfigurationManager.get("engine", "points-per-lines-completed")
game.score += points_per_lines_completed[lines_completed] * (current_level + 1)
self.lines_completed_count += lines_completed
def add_piece(self, piece: Piece) -> None:
self.points += piece.points
@@ -327,7 +333,7 @@ class Stack(Entity):
if len(squares_to_exclude) == 0:
return 0
self._play_row_completion_sound()
self._play_line_completed_sound()
tile_size = ConfigurationManager.get("engine", "tile-size")
new_points = []
@@ -344,9 +350,9 @@ class Stack(Entity):
return len(rows_completed)
def _play_row_completion_sound(self) -> None:
row_completion_sound_file = ConfigurationManager.get("sound", "row-completion")
self.row_completion_sound.play(mixer.Sound(row_completion_sound_file))
def _play_line_completed_sound(self) -> None:
line_completed_sound_file = ConfigurationManager.get("sound", "row-completion")
self.line_completed_sound.play(mixer.Sound(line_completed_sound_file))
"""
TODO description

View File

@@ -38,13 +38,12 @@ class Game:
self.main_music = mixer.Channel(0)
self.well = Well((280, 80), ConfigurationManager.get("color", "well-1"), ConfigurationManager.get("color", "well-border-1")) # TODO calculate position later and redo color config for well
self.stack = Stack(ConfigurationManager.get("color", "stack-1"), ConfigurationManager.get("color", "stack-border-1"))
self.score = 0
loaded_icon = pygame.image.load(win_icon)
pygame.display.set_caption(win_title)
pygame.display.set_icon(loaded_icon)
self.is_pressing_down = False # TODO move into control util later
main_music_file = ConfigurationManager.get("sound", "main-music")
self.main_music.set_volume(0.7) # TODO add volume to the config
self.main_music.play(mixer.Sound(main_music_file), -1)
@@ -63,14 +62,13 @@ class Game:
sys.exit()
if self.stack:
self.stack.update(elapsed_time)
self.stack.update(elapsed_time, self)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def draw(self) -> None:
# TODO write not initialized exception
@@ -86,17 +84,23 @@ class Game:
if self.current_piece:
self.current_piece.draw(self.screen, self.well, self.stack)
#TextGenerator.draw("12345678901234567890123456789012345678901234567890", (0, 0), self.screen)
score = str(self.score).zfill(6)
lines = str(self.stack.lines_completed_count).zfill(4)
level = str(self.get_level()).zfill(2)
TextGenerator.draw("Top", (80, 120), self.screen)
TextGenerator.draw("000000", (80, 140), self.screen)
TextGenerator.draw("Score", (80, 180), self.screen)
TextGenerator.draw("000000", (80, 200), self.screen)
TextGenerator.draw("Lines 0000", (300, 40), self.screen)
TextGenerator.draw(score, (80, 200), self.screen)
TextGenerator.draw("Lines " + lines, (300, 40), self.screen)
TextGenerator.draw("Next", (600, 120), self.screen)
TextGenerator.draw("LVL 00", (600, 220), self.screen)
TextGenerator.draw("LVL " + level, (600, 220), self.screen)
# update display
pygame.display.update()
def get_level(self) -> int:
lines_per_level = ConfigurationManager.get("engine", "lines-per-level")
return 0 if not self.stack else self.stack.lines_completed_count // lines_per_level

View File

@@ -85,7 +85,6 @@ class TextGenerator:
surface.blit(cls.sheet, (position[0] + x_position, position[1]), pygame.Rect(cls.characters[char_.upper()], (cls.glyph_size[0], cls.glyph_size[1])))
x_position += cls.glyph_size[0]
"""
TODO description
"""