wip!: add scenes to the game
This commit is contained in:
@@ -7,7 +7,9 @@ from tetris.entity import PieceGenerator
|
||||
from tetris.entity import Well
|
||||
from tetris.entity import Stack
|
||||
from tetris.online import MultiplayerService
|
||||
from tetris.scene import TitleScene
|
||||
|
||||
# TODO improve game assets https://www.spriters-resource.com/nes/tetris/
|
||||
# TODO should be a singleton and refactor the whole file?
|
||||
class Game:
|
||||
|
||||
@@ -16,15 +18,19 @@ class Game:
|
||||
self.tile_size = -1
|
||||
self.screen = None
|
||||
self.clock = None
|
||||
self.main_music = None
|
||||
|
||||
self.current_scene = TitleScene()
|
||||
|
||||
# In Game #
|
||||
self.current_piece = None
|
||||
self.next_piece = None
|
||||
self.well = None
|
||||
self.stack = None
|
||||
self.main_music = None
|
||||
|
||||
def initialize(self) -> None:
|
||||
pygame.init()
|
||||
TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20))
|
||||
TextGenerator.init(ConfigurationManager.get("image", "font"), (20, 20))
|
||||
|
||||
win_width = ConfigurationManager.get("window", "width")
|
||||
win_height = ConfigurationManager.get("window", "height")
|
||||
@@ -50,25 +56,29 @@ class Game:
|
||||
|
||||
# gets called from the games main loop
|
||||
def update(self) -> None:
|
||||
# TODO write not initialized exception
|
||||
elapsed_time = self.clock.tick(self.fps)
|
||||
|
||||
if not self.next_piece:
|
||||
self.next_piece = PieceGenerator.get_piece((620, 160))
|
||||
|
||||
if self.current_piece:
|
||||
self.current_piece.update(elapsed_time, self)
|
||||
if self.current_scene:
|
||||
self.current_scene.update()
|
||||
else:
|
||||
self.current_piece = self.next_piece
|
||||
self.current_piece.move((360 - 620, 100 - 160)) # TODO calculate spawn position correctly
|
||||
self.next_piece = PieceGenerator.get_piece((620, 160)) # (360, 100)
|
||||
if self.stack and self.current_piece.collide(self.stack): # TODO game over redo
|
||||
pygame.quit()
|
||||
MultiplayerService.quit()
|
||||
sys.exit()
|
||||
# TODO write not initialized exception
|
||||
elapsed_time = self.clock.tick(self.fps)
|
||||
|
||||
if self.stack:
|
||||
self.stack.update(elapsed_time, self)
|
||||
if not self.next_piece:
|
||||
self.next_piece = PieceGenerator.get_piece((620, 160))
|
||||
|
||||
if self.current_piece:
|
||||
self.current_piece.update(elapsed_time, self)
|
||||
else:
|
||||
self.current_piece = self.next_piece
|
||||
self.current_piece.move((360 - 620, 100 - 160)) # TODO calculate spawn position correctly
|
||||
self.next_piece = PieceGenerator.get_piece((620, 160)) # (360, 100)
|
||||
if self.stack and self.current_piece.collide(self.stack): # TODO game over redo
|
||||
pygame.quit()
|
||||
MultiplayerService.quit()
|
||||
sys.exit()
|
||||
|
||||
if self.stack:
|
||||
self.stack.update(elapsed_time, self)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
@@ -79,31 +89,34 @@ class Game:
|
||||
def draw(self) -> None:
|
||||
# TODO write not initialized exception
|
||||
|
||||
# draw window bg
|
||||
bg_color = pygame.Color(ConfigurationManager.get("color", "window-bg"))
|
||||
self.screen.fill(bg_color)
|
||||
if self.current_scene:
|
||||
self.current_scene.draw(self.screen)
|
||||
else:
|
||||
# draw window bg
|
||||
bg_color = pygame.Color(ConfigurationManager.get("color", "window-bg"))
|
||||
self.screen.fill(bg_color)
|
||||
|
||||
# draw all game objects
|
||||
if self.next_piece:
|
||||
self.next_piece.draw(self.screen)
|
||||
if self.well:
|
||||
self.well.draw(self.screen)
|
||||
if self.stack:
|
||||
self.stack.draw(self.screen)
|
||||
if self.current_piece:
|
||||
self.current_piece.draw(self.screen, self.well, self.stack)
|
||||
|
||||
score = str(self.score).zfill(6)
|
||||
lines = str(self.stack.lines_completed_count).zfill(4)
|
||||
level = str(self.get_level()).zfill(2)
|
||||
# draw all game objects
|
||||
if self.next_piece:
|
||||
self.next_piece.draw(self.screen)
|
||||
if self.well:
|
||||
self.well.draw(self.screen)
|
||||
if self.stack:
|
||||
self.stack.draw(self.screen)
|
||||
if self.current_piece:
|
||||
self.current_piece.draw(self.screen, self.well, self.stack)
|
||||
|
||||
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(score, (80, 200), self.screen)
|
||||
TextGenerator.draw("Lines " + lines, (300, 40), self.screen)
|
||||
TextGenerator.draw("Next", (600, 120), self.screen)
|
||||
TextGenerator.draw("LVL " + level, (600, 220), self.screen)
|
||||
TextGenerator.draw("Top", (80, 120), self.screen)
|
||||
TextGenerator.draw("000000", (80, 140), self.screen)
|
||||
TextGenerator.draw("Score", (80, 180), 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 " + level, (600, 220), self.screen)
|
||||
|
||||
# update display
|
||||
pygame.display.update()
|
||||
|
||||
Reference in New Issue
Block a user