refactor: move logic in main to its on class
This commit is contained in:
65
main.py
65
main.py
@@ -4,71 +4,18 @@
|
||||
https://tetris.com/play-tetris
|
||||
'''
|
||||
|
||||
import pygame
|
||||
|
||||
from Tetris import Tetris
|
||||
from util.ConfigurationManager import ConfigurationManager
|
||||
from util.PieceGenerator import PieceGenerator
|
||||
from entity.Well import Well
|
||||
|
||||
def draw(screen, objects):
|
||||
# draw window bg
|
||||
bg_color = pygame.Color(ConfigurationManager.configuration["window"]["bg-color"])
|
||||
screen.fill(bg_color)
|
||||
|
||||
# draw all game objects
|
||||
for object in objects:
|
||||
object.draw(screen)
|
||||
|
||||
# update display
|
||||
pygame.display.update()
|
||||
|
||||
def main():
|
||||
ConfigurationManager.load()
|
||||
pygame.init()
|
||||
|
||||
win_width = ConfigurationManager.configuration["window"]["width"]
|
||||
win_height = ConfigurationManager.configuration["window"]["height"]
|
||||
win_icon = ConfigurationManager.configuration["window"]["icon"]
|
||||
win_title = ConfigurationManager.configuration["window"]["title"]
|
||||
fps = ConfigurationManager.configuration["engine"]["fps"]
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
tetris = Tetris()
|
||||
tetris.initialize()
|
||||
|
||||
screen = pygame.display.set_mode((win_width, win_height))
|
||||
clock = pygame.time.Clock()
|
||||
loaded_icon = pygame.image.load(win_icon)
|
||||
|
||||
pygame.display.set_caption(win_title)
|
||||
pygame.display.set_icon(loaded_icon)
|
||||
|
||||
current_piece = PieceGenerator.get_piece((300, 100))
|
||||
pieces = [current_piece]
|
||||
well = Well((280, 80))
|
||||
|
||||
is_running = True
|
||||
while is_running:
|
||||
clock.tick(fps)
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
is_running = False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_SPACE:
|
||||
current_piece.rotate()
|
||||
if event.key == pygame.K_LEFT:
|
||||
current_piece.move((-tile_size, 0))
|
||||
if event.key == pygame.K_RIGHT:
|
||||
current_piece.move((tile_size, 0))
|
||||
if event.key == pygame.K_UP:
|
||||
current_piece.move((0, -tile_size))
|
||||
if event.key == pygame.K_DOWN:
|
||||
current_piece.move((0, tile_size))
|
||||
if event.key == pygame.K_z:
|
||||
current_piece = PieceGenerator.get_piece((300, 100))
|
||||
pieces.append(current_piece)
|
||||
|
||||
draw(screen, pieces + [well])
|
||||
|
||||
pygame.quit()
|
||||
while True:
|
||||
tetris.update()
|
||||
tetris.draw()
|
||||
|
||||
# start main function
|
||||
main()
|
||||
Reference in New Issue
Block a user