feat: add ability to move piece with arrow keys
This commit is contained in:
11
config.yaml
11
config.yaml
@@ -3,16 +3,15 @@ window:
|
||||
height: 600
|
||||
icon: "img/tetris_icon.png"
|
||||
title: "Tetris"
|
||||
bg-color: "#6495ED" # cornflower-blue
|
||||
|
||||
engine:
|
||||
fps: 60
|
||||
tile-size: 20
|
||||
|
||||
# https://coolors.co/6495ed-ee6352-59cd90-fac05e-dfd9e2
|
||||
# TODO change config color names, should be generic
|
||||
color:
|
||||
cornflower-blue: "#6495ED"
|
||||
fire-opal: "#EE6352"
|
||||
emerald: "#59CD90"
|
||||
maximum-yellow-red: "#59CD90"
|
||||
languid-lavender: "#DFD9E2"
|
||||
base-1: "#EE6352"
|
||||
base-2: "#59CD90"
|
||||
base-3: "#59CD90"
|
||||
border: "#DFD9E2"
|
||||
|
||||
@@ -6,7 +6,7 @@ from util.ConfigurationManager import ConfigurationManager
|
||||
For information on the Tetris piece Tetromino go here:
|
||||
https://tetris.fandom.com/wiki/Tetromino
|
||||
'''
|
||||
class Piece:
|
||||
class Piece: # TODO game objects base class / interface?
|
||||
|
||||
def __init__(self, shape, position, color):
|
||||
self.color = color
|
||||
@@ -27,9 +27,9 @@ class Piece:
|
||||
|
||||
def draw(self, surface):
|
||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
||||
hex_color = ConfigurationManager.configuration["color"]["languid-lavender"]
|
||||
hex_color = ConfigurationManager.configuration["color"]["border"] # TODO Should abstract color call?
|
||||
|
||||
base_color = pygame.Color(self.color) # TODO Should abstract color call?
|
||||
base_color = pygame.Color(self.color)
|
||||
border_color = pygame.Color(hex_color)
|
||||
|
||||
for sub_points in self.points:
|
||||
|
||||
29
main.py
29
main.py
@@ -3,17 +3,14 @@ import pygame
|
||||
from entity.Piece import Piece
|
||||
from util.ConfigurationManager import ConfigurationManager
|
||||
|
||||
def draw(screen, obj):
|
||||
def draw(screen, objects):
|
||||
# draw window bg
|
||||
bg_color = pygame.Color(ConfigurationManager.configuration["color"]["cornflower-blue"])
|
||||
bg_color = pygame.Color(ConfigurationManager.configuration["window"]["bg-color"])
|
||||
screen.fill(bg_color)
|
||||
|
||||
fire_opal = ConfigurationManager.configuration["color"]["fire-opal"]
|
||||
|
||||
if not "i_piece" in obj:
|
||||
obj["i_piece"] = Piece(Piece.I_SHAPE, (100, 100), fire_opal)
|
||||
obj["i_piece"].move((1, 1))
|
||||
obj["i_piece"].draw(screen)
|
||||
# draw all game objects
|
||||
for object in objects:
|
||||
object.draw(screen)
|
||||
|
||||
# update display
|
||||
pygame.display.update()
|
||||
@@ -35,7 +32,9 @@ def main():
|
||||
pygame.display.set_caption(win_title)
|
||||
pygame.display.set_icon(loaded_icon)
|
||||
|
||||
obj = { }
|
||||
base_one_color = ConfigurationManager.configuration["color"]["base-1"]
|
||||
i_piece = Piece(Piece.I_SHAPE, (100, 100), base_one_color)
|
||||
|
||||
is_running = True
|
||||
while is_running:
|
||||
clock.tick(fps)
|
||||
@@ -44,7 +43,17 @@ def main():
|
||||
if event.type == pygame.QUIT:
|
||||
is_running = False
|
||||
|
||||
draw(screen, obj)
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_LEFT]:
|
||||
i_piece.move((-5, 0))
|
||||
if keys[pygame.K_RIGHT]:
|
||||
i_piece.move((5, 0))
|
||||
if keys[pygame.K_UP]:
|
||||
i_piece.move((0, -5))
|
||||
if keys[pygame.K_DOWN]:
|
||||
i_piece.move((0, 5))
|
||||
|
||||
draw(screen, [i_piece])
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user