Made some design adjustments to make it look more modern.
This commit is contained in:
67
tetris.py
67
tetris.py
@@ -54,20 +54,26 @@ SOFT_DROP_SCORE = 1
|
||||
HARD_DROP_SCORE_PER_CELL = 2
|
||||
LINES_PER_LEVEL = 10
|
||||
|
||||
# Colors for pieces and board
|
||||
BG_COLOR = "#111417"
|
||||
GRID_COLOR = "#1a1f24"
|
||||
TEXT_COLOR = "#e6edf3"
|
||||
GHOST_COLOR = "#3a3f46"
|
||||
# Colors and fonts (modern dark theme)
|
||||
BG_COLOR = "#0b0f14" # canvas/background
|
||||
GRID_COLOR = "#131a21" # subtle grid lines
|
||||
TEXT_COLOR = "#e6edf3" # primary text
|
||||
GHOST_COLOR = "#2b3640" # ghost piece
|
||||
|
||||
# UI fonts
|
||||
FONT_FAMILY = "Helvetica" # broadly available modern sans
|
||||
FONT_LG = (FONT_FAMILY, 16, "bold")
|
||||
FONT_MD = (FONT_FAMILY, 12)
|
||||
FONT_SM = (FONT_FAMILY, 10)
|
||||
|
||||
PIECE_COLORS = {
|
||||
'I': "#00c0f2",
|
||||
'J': "#4169e1",
|
||||
'L': "#ff8c00",
|
||||
'O': "#f0e68c",
|
||||
'S': "#32cd32",
|
||||
'T': "#ba55d3",
|
||||
'Z': "#ff4d4d",
|
||||
'I': "#23b9f2", # cyan
|
||||
'J': "#5a7cff", # indigo
|
||||
'L': "#ff9f43", # orange
|
||||
'O': "#ffda6a", # yellow
|
||||
'S': "#2ecc71", # green
|
||||
'T': "#bd7bff", # purple
|
||||
'Z': "#ff6b6b", # red
|
||||
}
|
||||
|
||||
# Tetromino definitions as rotation states (list of (x,y) offsets)
|
||||
@@ -301,7 +307,7 @@ class Board:
|
||||
class TetrisApp:
|
||||
def __init__(self, root: tk.Tk):
|
||||
self.root = root
|
||||
root.title("Tetris (tkinter)")
|
||||
root.title("Tetris — Modern")
|
||||
root.configure(bg=BG_COLOR)
|
||||
self.board = Board()
|
||||
# layout
|
||||
@@ -322,23 +328,23 @@ class TetrisApp:
|
||||
self.canvas.pack()
|
||||
|
||||
self.info_lbl = tk.Label(right, text="Score: 0\nLines: 0\nLevel: 1",
|
||||
font=("Consolas", 14), fg=TEXT_COLOR, bg=BG_COLOR, justify="left")
|
||||
font=FONT_LG, fg=TEXT_COLOR, bg=BG_COLOR, justify="left")
|
||||
self.info_lbl.pack(anchor="w")
|
||||
|
||||
self.next_lbl = tk.Label(right, text="Next:", font=("Consolas", 12), fg=TEXT_COLOR, bg=BG_COLOR)
|
||||
self.next_lbl = tk.Label(right, text="Next:", font=FONT_MD, fg=TEXT_COLOR, bg=BG_COLOR)
|
||||
self.next_lbl.pack(anchor="w", pady=(10, 0))
|
||||
self.preview = tk.Canvas(right, width=side_w, height=PREVIEW_ROWS * CELL,
|
||||
bg=BG_COLOR, highlightthickness=0)
|
||||
self.preview.pack()
|
||||
|
||||
self.hold_lbl = tk.Label(right, text="Hold:", font=("Consolas", 12), fg=TEXT_COLOR, bg=BG_COLOR)
|
||||
self.hold_lbl = tk.Label(right, text="Hold:", font=FONT_MD, fg=TEXT_COLOR, bg=BG_COLOR)
|
||||
self.hold_lbl.pack(anchor="w", pady=(10, 0))
|
||||
self.hold_view = tk.Canvas(right, width=side_w, height=3 * CELL,
|
||||
bg=BG_COLOR, highlightthickness=0)
|
||||
self.hold_view.pack()
|
||||
|
||||
self.help_lbl = tk.Label(right, text="Arrows to move/rotate\nSpace: Hard drop\nShift/H: Hold\nP: Pause R: Restart",
|
||||
font=("Consolas", 10), fg=TEXT_COLOR, bg=BG_COLOR, justify="left")
|
||||
font=FONT_SM, fg=TEXT_COLOR, bg=BG_COLOR, justify="left")
|
||||
self.help_lbl.pack(anchor="w", pady=(10, 0))
|
||||
|
||||
# game state
|
||||
@@ -464,12 +470,12 @@ class TetrisApp:
|
||||
# overlays
|
||||
if self.paused:
|
||||
self.canvas.create_text(COLS * CELL // 2, ROWS * CELL // 2, text="PAUSED",
|
||||
fill=TEXT_COLOR, font=("Consolas", 24, "bold"))
|
||||
fill=TEXT_COLOR, font=(FONT_FAMILY, 24, "bold"))
|
||||
if self.game_over:
|
||||
self.canvas.create_text(COLS * CELL // 2, ROWS * CELL // 2 - 12, text="GAME OVER",
|
||||
fill=TEXT_COLOR, font=("Consolas", 24, "bold"))
|
||||
fill=TEXT_COLOR, font=(FONT_FAMILY, 24, "bold"))
|
||||
self.canvas.create_text(COLS * CELL // 2, ROWS * CELL // 2 + 16, text="Press R to restart",
|
||||
fill=TEXT_COLOR, font=("Consolas", 12))
|
||||
fill=TEXT_COLOR, font=FONT_MD)
|
||||
|
||||
# info
|
||||
self.info_lbl.config(text=f"Score: {self.board.score}\nLines: {self.board.lines_cleared}\nLevel: {self.board.level}")
|
||||
@@ -480,17 +486,20 @@ class TetrisApp:
|
||||
if self.board.hold_kind:
|
||||
self.draw_preview(self.hold_view, self.board.hold_kind)
|
||||
|
||||
def draw_cell(self, canvas: tk.Canvas, x: int, y: int, color: str, outline: str = "#0c0f12"):
|
||||
def draw_cell(self, canvas: tk.Canvas, x: int, y: int, color: str, outline: str = ""):
|
||||
x0 = x * CELL + 1
|
||||
y0 = y * CELL + 1
|
||||
x1 = x0 + CELL - 2
|
||||
y1 = y0 + CELL - 2
|
||||
# soft shadow
|
||||
canvas.create_rectangle(x0 + 2, y0 + 2, x1 + 2, y1 + 2, outline="", fill="#000000", stipple="gray25")
|
||||
# main body
|
||||
canvas.create_rectangle(x0, y0, x1, y1, outline=outline, fill=color)
|
||||
# simple 3D shading
|
||||
canvas.create_line(x0, y0, x1, y0, fill="#ffffff")
|
||||
canvas.create_line(x0, y0, x0, y1, fill="#ffffff")
|
||||
canvas.create_line(x0, y1, x1, y1, fill="#000000")
|
||||
canvas.create_line(x1, y0, x1, y1, fill="#000000")
|
||||
# subtle highlights
|
||||
canvas.create_line(x0, y0, x1, y0, fill="#ffffff", stipple="gray25")
|
||||
canvas.create_line(x0, y0, x0, y1, fill="#ffffff", stipple="gray25")
|
||||
canvas.create_line(x0, y1, x1, y1, fill="#000000", stipple="gray25")
|
||||
canvas.create_line(x1, y0, x1, y1, fill="#000000", stipple="gray25")
|
||||
|
||||
def draw_preview(self, canvas: tk.Canvas, kind: str):
|
||||
canvas.delete("all")
|
||||
@@ -507,11 +516,7 @@ class TetrisApp:
|
||||
for dx, dy in cells:
|
||||
x = dx - minx + offset_x
|
||||
y = dy - miny + offset_y
|
||||
x0 = x * CELL + 1
|
||||
y0 = y * CELL + 1
|
||||
x1 = x0 + CELL - 2
|
||||
y1 = y0 + CELL - 2
|
||||
canvas.create_rectangle(x0, y0, x1, y1, outline="#0c0f12", fill=PIECE_COLORS[kind])
|
||||
self.draw_cell(canvas, x, y, PIECE_COLORS[kind])
|
||||
|
||||
|
||||
def main() -> int:
|
||||
|
Reference in New Issue
Block a user