Python实现带最高分与历史分数显示的俄罗斯方块游戏代码
以下是一个用 Python 编写的简单俄罗斯方块游戏代码示例,其中包含显示最高分和历史分数的功能: ```python import pygame import random # 游戏窗口大小 WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 # 小方格大小 GRID_SIZE = 30 # 颜色定义 WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) # 初始化 pygame pygame.init() # 创建游戏窗口 window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("俄罗斯方块游戏") # 字体设置 font = pygame.font.SysFont(None, 36) # 方块形状和对应的颜色 SHAPES = { 'I': [[1, 1, 1, 1]], 'J': [[1, 0, 0], [1, 1, 1]], 'L': [[0, 0, 1], [1, 1, 1]], 'O': [[1, 1], [1, 1]], 'S': [[0, 1, 1], [1, 1, 0]], 'T': [[0, 1, 0], [1, 1, 1]], 'Z': [[1, 1, 0], [0, 1, 1]] } SHAPE_COLORS = { 'I': GREEN, 'J': (0, 0, 255), 'L': (255, 165, 0), 'O': (255, 255, 0), 'S': (0, 255, 0), 'T': (128, 0, 128), 'Z': (255, 0, 0) } # 游戏区域的坐标范围 GRID_X = 0 GRID_Y = 0 GRID_WIDTH = 10 GRID_HEIGHT = 20 # 方块的下落速度(毫秒) DROP_SPEED = 500 # 存储分数和历史分数 current_score = 0 max_score = 0 history_scores = [] # 初始化游戏区域 grid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)] # 随机生成一个新的方块 def generate_new_shape(): shape_name = random.choice(list(SHAPES.keys())) shape = SHAPES[shape_name] color = SHAPE_COLORS[shape_name] x = GRID_WIDTH // 2 - len(shape[0]) // 2 y = 0 return shape, color, x, y # 检查是否可以放置方块 def is_valid_position(shape, x, y): for row in range(len(shape)): for col in range(len(shape[row])): if shape[row][col] and (x + col < 0 or x + col >= GRID_WIDTH or y + row >= GRID_HEIGHT or grid[y + row][x + col]): return False return True # 合并方块到游戏区域 def merge_shape(shape, x, y): for row in range(len(shape)): for col in range(len(shape[row])): if shape[row][col]: grid[y + row][x + col] = 1 # 消除满行 def clear_full_rows(): global current_score full_rows = [] for row in range(GRID_HEIGHT): if all(grid[row]): full_rows.append(row) for row in full_rows: del grid[row] grid.insert(0, [0] * GRID_WIDTH) current_score += len(full_rows) * 10 # 游戏主循环 running = True clock = pygame.time.Clock() current_shape, current_color, current_x, current_y = generate_new_shape() next_shape, next_color, next_x, next_y = generate_new_shape() time_since_last_drop = 0 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_LEFT] and is_valid_position(current_shape, current_x - 1, current_y): current_x -= 1 elif keys_pressed[pygame.K_RIGHT] and is_valid_position(current_shape, current_x + 1, current_y): current_x += 1 elif keys_pressed[pygame.K_DOWN] and is_valid_position(current_shape, current_x, current_y + 1): current_y += 1 elif keys_pressed[pygame.K_UP]: rotated_shape = list(zip(*reversed(current_shape))) if is_valid_position(rotated_shape, current_x, current_y): current_shape = rotated_shape time_since_last_drop += clock.tick() if time_since_last_drop >= DROP_SPEED and is_valid_position(current_shape, current_x, current_y + 1): current_y += 1 time_since_last_drop = 0 elif not is_valid_position(current_shape, current_x, current_y + 1): merge_shape(current_shape, current_x, current_y) clear_full_rows() if current_y == 0: running = False current_shape, current_color, current_x, current_y = next_shape, next_color, next_x, next_y next_shape, next_color, next_x, next_y = generate_new_shape() # 绘制游戏界面 window.fill(BLACK) for row in range(GRID_HEIGHT): for col in range(GRID_WIDTH): if grid[row][col]: pygame.draw.rect(window, WHITE, (GRID_X + col * GRID_SIZE, GRID_Y + row * GRID_SIZE, GRID_SIZE, GRID_SIZE)) for row in range(len(current_shape)): for col in range(len(current_shape[row])): if current_shape[row][col]: pygame.draw.rect(window, current_color, (GRID_X + (current_x + col) * GRID_SIZE, GRID_Y + (current_y + row) * GRID_SIZE, GRID_SIZE, GRID_SIZE)) # 显示分数 score_text = font.render("Score: " + str(current_score), True, WHITE) window.blit(score_text, (10, 10)) # 显示最高分 max_score_text = font.render("Max Score: " + str(max_score), True, WHITE) window.blit(max_score_text, (10, 50)) # 显示历史分数 history_text = font.render("History Scores: " + ", ".join(str(score) for score in history_scores), True, WHITE) window.blit(history_text, (10, 90)) pygame.display.flip() # 更新最高分 if current_score > max_score: max_score = current_score # 保存历史分数 history_scores.append(current_score) pygame.quit() ``` 这只是一个简单的示例代码,你可以根据需要进行进一步的优化和扩展。 ######[AI写代码神器 | 1707点数解答 | 2024-11-12 16:13:35]
- 超燃年会来袭!诗舞歌戏轮番上阵,共赴欢乐盛宴! (字节豆包 | 206点数解答 | 2025-01-08 10:59:43)366
- Java 继承实现:重温经典剪刀石头布游戏(字节豆包 | 451点数解答 | 2024-10-20 19:57:58)485
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)498
- C++ 实现学生成绩统计:最高分、最低分、不及格与优秀人数计算(字节豆包 | 489点数解答 | 2026-04-12 10:09:58)14
- Python实现期末成绩单数据统计:一键算出及格不及格人数及高低分、平均分!(字节豆包 | 478点数解答 | 2025-12-06 20:50:12)84
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)384
- 选科组合最高分求解:C++实现及注意要点(字节豆包 | 258点数解答 | 2025-08-28 12:51:41)162
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)451
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)443
- 探秘俄罗斯方块:经典玩法背后的游戏开发逻辑与要点(字节豆包 | 130点数解答 | 2025-04-24 19:06:56)120
- 探秘经典游戏俄罗斯方块:玩法、技术、影响与冷知识大揭秘(DeepSeek | 503点数解答 | 2025-04-24 19:07:57)116
- 超复杂 C++ 代码打造集象棋、将棋、方块、国象于一体的游戏盒子!(字节豆包 | 1233点数解答 | 2025-11-23 08:32:14)31