import tkinter as tk import random class Tetris: def __init__(self, root): self.root = root self.root.title("俄羅斯方塊") self.canvas = tk.Canvas(root, width=300, height=600, bg="black") self.canvas.pack() self.cell_size = 30 self.cols = 10 self.rows = 20 self.board = [[0 for _ in range(self.cols)] for _ in range(self.rows)] self.shapes = [ [[1, 1, 1, 1]], # I [[1, 1], [1, 1]], # O [[0, 1, 0], [1, 1, 1]], # T [[1, 1, 0], [0, 1, 1]], # S [[0, 1, 1], [1, 1, 0]], # Z [[1, 1, 1], [1, 0, 0]], # L [[1, 1, 1], [0, 0, 1]] # J ] self.current_shape = self.new_shape() self.current_position = [0, self.cols // 2 - len(self.current_shape[0]) // 2] self.is_game_over = False self.score = 0 self.root.bind("", self.move_left) self.root.bind("", self.move_right) self.root.bind("", self.move_down) self.root.bind("", self.rotate_shape) self.update_game() def new_shape(self): return random.choice(self.shapes) def rotate_shape(self, event=None): self.current_shape = [list(row) for row in zip(*self.current_shape[::-1])] if self.check_collision(self.current_position): self.current_shape = [list(row) for row in zip(*self.current_shape)][::-1] def move_left(self, event=None): self.current_position[1] -= 1 if self.check_collision(self.current_position): self.current_position[1] += 1 def move_right(self, event=None): self.current_position[1] += 1 if self.check_collision(self.current_position): self.current_position[1] -= 1 def move_down(self, event=None): self.current_position[0] += 1 if self.check_collision(self.current_position): self.current_position[0] -= 1 self.place_shape() self.clear_lines() self.current_shape = self.new_shape() self.current_position = [0, self.cols // 2 - len(self.current_shape[0]) // 2] if self.check_collision(self.current_position): self.game_over() def check_collision(self, offset): off_x, off_y = offset for y, row in enumerate(self.current_shape): for x, cell in enumerate(row): if cell: if x + off_y < 0 or x + off_y >= self.cols or y + off_x >= self.rows or self.board[y + off_x][x + off_y]: return True return False def place_shape(self): for y, row in enumerate(self.current_shape): for x, cell in enumerate(row): if cell: self.board[self.current_position[0] + y][self.current_position[1] + x] = 1 def clear_lines(self): new_board = [row for row in self.board if not all(row)] cleared_lines = self.rows - len(new_board) self.score += cleared_lines self.root.title(f"俄羅斯方塊 - 分數: {self.score}") new_board = [[0 for _ in range(self.cols)] for _ in range(cleared_lines)] + new_board self.board = new_board def draw_board(self): self.canvas.delete("all") for y, row in enumerate(self.board): for x, cell in enumerate(row): if cell: self.draw_cell(x, y) for y, row in enumerate(self.current_shape): for x, cell in enumerate(row): if cell: self.draw_cell(self.current_position[1] + x, self.current_position[0] + y) def draw_cell(self, x, y): x0 = x * self.cell_size y0 = y * self.cell_size x1 = x0 + self.cell_size y1 = y0 + self.cell_size self.canvas.create_rectangle(x0, y0, x1, y1, fill="cyan", outline="white") def update_game(self): if not self.is_game_over: self.move_down() self.draw_board() self.root.after(500, self.update_game) def game_over(self): self.is_game_over = True self.canvas.create_text(150, 300, text="遊戲結束", fill="red", font=("Arial", 24)) if __name__ == "__main__": root = tk.Tk() game = Tetris(root) root.mainloop()