diff --git a/main.py b/main.py index 5d151022d0f3d476d922573317df65c422fb58c5..a51cac6f55545dcbbcf32d9da72203c60ae488c5 100644 --- a/main.py +++ b/main.py @@ -1 +1,223 @@ print('欢迎来到 InsCode') +import pygame +import random + +# 初始化 Pygame +pygame.init() + +# 定义游戏窗口大小 +screen_width = 800 +screen_height = 600 + +# 定义方块大小和间隔 +block_size = 30 +block_space = 2 + +# 定义颜色 +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +RED = (200, 0, 0) +GREEN = (0, 200, 0) +BLUE = (0, 0, 200) +YELLOW = (200, 200, 0) +PURPLE = (200, 0, 200) +CYAN = (0, 200, 200) + +# 定义七种方块的形状 +tetrominoes = [ + [[1, 1, 1, 1]], # I + [[1, 1, 0], [0, 1, 1]], # O + [[0, 1, 1], [1, 1, 0]], # Z + [[1, 1, 0], [0, 1, 1]], # S + [[1, 1, 1], [0, 1, 0]], # T + [[1, 1, 1], [0, 0, 1]], # L + [[1, 1, 1], [1, 0, 0]] # J +] + +# 定义方块的颜色 +tetromino_colors = [ + WHITE, + YELLOW, + RED, + GREEN, + BLUE, + PURPLE, + CYAN +] + +# 定义游戏区域的大小 +play_width = 10 * (block_size + block_space) +play_height = 20 * (block_size + block_space) +play_x = (screen_width - play_width) // 2 +play_y = screen_height - play_height - 50 + +# 初始化游戏窗口 +screen = pygame.display.set_mode((screen_width, screen_height)) +pygame.display.set_caption("Tetris") + +# 定义字体 +font = pygame.font.Font(None, 36) + +# 定义游戏结束标志 +game_over = False + +# 定义方块类 +class Block: + def __init__(self, x, y, shape): + self.x = x + self.y = y + self.shape = shape + self.color = tetromino_colors[tetrominoes.index(shape)] + self.rotation = 0 + + # 绘制方块 + def draw(self, x, y): + for i in range(len(self.shape[self.rotation])): + for j in range(len(self.shape[self.rotation][i])): + if self.shape[self.rotation][i][j] == 1: + pygame.draw.rect( + screen, + self.color, + [x + j * (block_size + block_space), + y + i * (block_size + block_space), + block_size, + block_size]) + + # 移动方块 + def move(self, direction, play_area): + if direction == "left": + if self.x > 0: + new_x = self.x - 1 + if not self.check_collision(new_x, self.y, self.rotation, play_area): + self.x = new_x + elif direction == "right": + if self.x < play_width - len(self.shape[self.rotation][0]): + new_x = self.x + 1 + if not self.check_collision(new_x, self.y, self.rotation, play_area): + self.x = new_x + elif direction == "down": + if self.y < play_height - len(self.shape[self.rotation]): + new_y = self.y + 1 + if not self.check_collision(self.x, new_y, self.rotation, play_area): + self.y = new_y + + # 旋转方块 + def rotate(self, play_area): + new_rotation = self.rotation + 1 + if new_rotation >= len(self.shape): + new_rotation = 0 + if not self.check_collision(self.x, self.y, new_rotation, play_area): + self.rotation = new_rotation + + # 检查方块是否与障碍物相撞 + def check_collision(self, x, y, rotation, play_area): + for i in range(len(self.shape[rotation])): + for j in range(len(self.shape[rotation][i])): + if self.shape[rotation][i][j] == 1: + position_x = x + j + position_y = y + i + if position_y < 0: + continue + if position_x < 0 or position_x >= play_width or position_y >= play_height: + return True + if play_area[position_y][position_x] != BLACK: + return True + return False + +# 定义游戏主函数 +def main(): + global game_over + + # 初始化游戏区域 + play_area = [[BLACK for _ in range(play_width)] for _ in range(play_height)] + + # 定义当前方块和下一个方块 + current_block = Block(3, 0, random.choice(tetrominoes)) + next_block = Block(3, 0, random.choice(tetrominoes)) + + # 定义计时器和计时器步长 + clock = pygame.time.Clock() + timer = 0 + timer_step = 1000 + + # 定义得分 + score = 0 + + # 游戏主循环 + while not game_over: + # 处理事件 + for event in pygame.event.get(): + if event.type == pygame.QUIT: + game_over = True + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + current_block.move("left", play_area) + if event.key == pygame.K_RIGHT: + current_block.move("right", play_area) + if event.key == pygame.K_DOWN: + current_block.move("down", play_area) + if event.key == pygame.K_UP: + current_block.rotate(play_area) + + # 计时器递增 + timer += clock.get_rawtime() + clock.tick() + + # 判断是否需要降落方块 + if timer >= timer_step: + timer = 0 + current_block.move("down", play_area) + + # 检查方块是否落地 + if current_block.check_collision(current_block.x, current_block.y + 1, current_block.rotation, play_area): + # 将方块加入游戏区域 + for i in range(len(current_block.shape[current_block.rotation])): + for j in range(len(current_block.shape[current_block.rotation][i])): + if current_block.shape[current_block.rotation][i][j] == 1: + position_x = current_block.x + j + position_y = current_block.y + i + play_area[position_y][position_x] = current_block.color + + # 计算得分 + rows_cleared = 0 + for i in range(len(play_area)): + if BLACK not in play_area[i]: + rows_cleared += 1 + for j in range(i, 0, -1): + play_area[j] = play_area[j - 1][:] + play_area[0] = [BLACK for _ in range(play_width)] + score += rows_cleared * 10 + + # 放置下一个方块 + current_block = next_block + next_block = Block(3, 0, random.choice(tetrominoes)) + + # 判断游戏是否结束 + if current_block.check_collision(current_block.x, current_block.y, current_block.rotation, play_area): + game_over = True + + # 绘制游戏区域 + screen.fill(BLACK) + for i in range(len(play_area)): + for j in range(len(play_area[i])): + pygame.draw.rect( + screen, + play_area[i][j], + [play_x + j * (block_size + block_space), + play_y + i * (block_size + block_space), + block_size, + block_size]) + current_block.draw(play_x, play_y) + next_block.draw(play_x + (play_width + 50), play_y) + text = font.render("Score: " + str(score), True, WHITE) + screen.blit(text, [play_x, play_y - 40]) + + # 更新屏幕 + pygame.display.update() + + # 退出 Pygame + pygame.quit() + +# 运行游戏主函数 +if __name__ == "__main__": + main()