refactor: move logic in main to its on class
This commit is contained in:
81
Tetris.py
Normal file
81
Tetris.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import sys
|
||||
import pygame
|
||||
from util.ConfigurationManager import ConfigurationManager
|
||||
from util.PieceGenerator import PieceGenerator
|
||||
from entity.Well import Well
|
||||
|
||||
# TODO should be a singleton?
|
||||
class Tetris:
|
||||
|
||||
def __init__(self):
|
||||
self.fps = -1
|
||||
self.tile_size = -1
|
||||
self.screen = None
|
||||
self.clock = None
|
||||
self.well = None
|
||||
self.current_piece = None
|
||||
|
||||
def initialize(self):
|
||||
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"]
|
||||
|
||||
self.fps = ConfigurationManager.configuration["engine"]["fps"]
|
||||
self.tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
self.screen = pygame.display.set_mode((win_width, win_height))
|
||||
self.clock = pygame.time.Clock()
|
||||
self.well = Well((280, 80)) # TODO calculate position later
|
||||
|
||||
loaded_icon = pygame.image.load(win_icon)
|
||||
pygame.display.set_caption(win_title)
|
||||
pygame.display.set_icon(loaded_icon)
|
||||
|
||||
# gets called from the games main loop
|
||||
def update(self):
|
||||
# TODO write not initialized exception
|
||||
|
||||
self.clock.tick(self.fps)
|
||||
|
||||
# TODO create control utility class
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if self.current_piece:
|
||||
if event.key == pygame.K_SPACE:
|
||||
self.current_piece.rotate()
|
||||
if event.key == pygame.K_LEFT:
|
||||
self.current_piece.move((-self.tile_size, 0))
|
||||
if event.key == pygame.K_RIGHT:
|
||||
self.current_piece.move((self.tile_size, 0))
|
||||
if event.key == pygame.K_UP:
|
||||
self.current_piece.move((0, -self.tile_size))
|
||||
if event.key == pygame.K_DOWN:
|
||||
self.current_piece.move((0, self.tile_size))
|
||||
if event.key == pygame.K_z:
|
||||
self.__generate_piece((300, 100))
|
||||
|
||||
def draw(self):
|
||||
# TODO write not initialized exception
|
||||
|
||||
# draw window bg
|
||||
bg_color = pygame.Color(ConfigurationManager.configuration["window"]["bg-color"])
|
||||
self.screen.fill(bg_color)
|
||||
|
||||
# draw all game objects
|
||||
if self.well:
|
||||
self.well.draw(self.screen)
|
||||
if self.current_piece:
|
||||
self.current_piece.draw(self.screen)
|
||||
|
||||
# update display
|
||||
pygame.display.update()
|
||||
|
||||
# TODO one line method is questionable
|
||||
def __generate_piece(self, position):
|
||||
self.current_piece = PieceGenerator.get_piece(position)
|
||||
|
||||
@@ -15,7 +15,7 @@ class Piece: # TODO game objects base class / interface?
|
||||
|
||||
def draw(self, surface):
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
hex_color = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract out color call?
|
||||
hex_color = ConfigurationManager.configuration["color"]["border"] # TODO should abstract out color call?
|
||||
|
||||
base_color = pygame.Color(self.color)
|
||||
border_color = pygame.Color(hex_color)
|
||||
@@ -46,6 +46,9 @@ class Piece: # TODO game objects base class / interface?
|
||||
point[0] = (k * -1) + self.center[0]
|
||||
point[1] = h + self.center[1]
|
||||
|
||||
def colliding(object):
|
||||
pass
|
||||
|
||||
def __get_points(self, shape, position):
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
|
||||
|
||||
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