酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

写一个俄罗斯方块游戏代码显示最高分和历史分数

以下是一个用 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() ``` 这只是一个简单的示例代码,你可以根据需要进行进一步的优化和扩展。 [2024-11-12 16:13:35 | AI写代码神器 | 1707点数解答]

相关提问