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
|
height: 600
|
||||||
icon: "img/tetris_icon.png"
|
icon: "img/tetris_icon.png"
|
||||||
title: "Tetris"
|
title: "Tetris"
|
||||||
|
bg-color: "#6495ED" # cornflower-blue
|
||||||
|
|
||||||
engine:
|
engine:
|
||||||
fps: 60
|
fps: 60
|
||||||
tile-size: 20
|
tile-size: 20
|
||||||
|
|
||||||
# https://coolors.co/6495ed-ee6352-59cd90-fac05e-dfd9e2
|
# https://coolors.co/6495ed-ee6352-59cd90-fac05e-dfd9e2
|
||||||
# TODO change config color names, should be generic
|
|
||||||
color:
|
color:
|
||||||
cornflower-blue: "#6495ED"
|
base-1: "#EE6352"
|
||||||
fire-opal: "#EE6352"
|
base-2: "#59CD90"
|
||||||
emerald: "#59CD90"
|
base-3: "#59CD90"
|
||||||
maximum-yellow-red: "#59CD90"
|
border: "#DFD9E2"
|
||||||
languid-lavender: "#DFD9E2"
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from util.ConfigurationManager import ConfigurationManager
|
|||||||
For information on the Tetris piece Tetromino go here:
|
For information on the Tetris piece Tetromino go here:
|
||||||
https://tetris.fandom.com/wiki/Tetromino
|
https://tetris.fandom.com/wiki/Tetromino
|
||||||
'''
|
'''
|
||||||
class Piece:
|
class Piece: # TODO game objects base class / interface?
|
||||||
|
|
||||||
def __init__(self, shape, position, color):
|
def __init__(self, shape, position, color):
|
||||||
self.color = color
|
self.color = color
|
||||||
@@ -27,9 +27,9 @@ class Piece:
|
|||||||
|
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
tile_size = ConfigurationManager.configuration["engine"]["tile-size"]
|
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)
|
border_color = pygame.Color(hex_color)
|
||||||
|
|
||||||
for sub_points in self.points:
|
for sub_points in self.points:
|
||||||
|
|||||||
29
main.py
29
main.py
@@ -3,17 +3,14 @@ import pygame
|
|||||||
from entity.Piece import Piece
|
from entity.Piece import Piece
|
||||||
from util.ConfigurationManager import ConfigurationManager
|
from util.ConfigurationManager import ConfigurationManager
|
||||||
|
|
||||||
def draw(screen, obj):
|
def draw(screen, objects):
|
||||||
# draw window bg
|
# 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)
|
screen.fill(bg_color)
|
||||||
|
|
||||||
fire_opal = ConfigurationManager.configuration["color"]["fire-opal"]
|
# draw all game objects
|
||||||
|
for object in objects:
|
||||||
if not "i_piece" in obj:
|
object.draw(screen)
|
||||||
obj["i_piece"] = Piece(Piece.I_SHAPE, (100, 100), fire_opal)
|
|
||||||
obj["i_piece"].move((1, 1))
|
|
||||||
obj["i_piece"].draw(screen)
|
|
||||||
|
|
||||||
# update display
|
# update display
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
@@ -35,7 +32,9 @@ def main():
|
|||||||
pygame.display.set_caption(win_title)
|
pygame.display.set_caption(win_title)
|
||||||
pygame.display.set_icon(loaded_icon)
|
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
|
is_running = True
|
||||||
while is_running:
|
while is_running:
|
||||||
clock.tick(fps)
|
clock.tick(fps)
|
||||||
@@ -43,8 +42,18 @@ def main():
|
|||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
is_running = False
|
is_running = False
|
||||||
|
|
||||||
|
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, obj)
|
draw(screen, [i_piece])
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user