Files
tetri5/main.py
2021-06-04 00:23:35 -04:00

76 lines
2.3 KiB
Python

'''
Tetris 101:
https://strategywiki.org/wiki/Tetris/Getting_Started
https://tetris.com/play-tetris
'''
import pygame
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"]
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)
pieces = []
well = Well((280, 80))
x = 50
y = 50
for _ in range(6):
pieces.append(PieceGenerator.get_piece((x, y)))
x += 120
y += 100
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:
[piece.rotate() for piece in pieces]
if event.key == pygame.K_LEFT:
[piece.move((-tile_size, 0)) for piece in pieces]
if event.key == pygame.K_RIGHT:
[piece.move((tile_size, 0)) for piece in pieces]
if event.key == pygame.K_UP:
[piece.move((0, -tile_size)) for piece in pieces]
if event.key == pygame.K_DOWN:
[piece.move((0, tile_size)) for piece in pieces]
draw(screen, pieces + [well])
pygame.quit()
# start main function
main()