Python Pygame石头剪刀布游戏:结果文本闪烁/不显示问题修复求助
修复Pygame石头剪刀布游戏结果闪烁/不显示问题
我正在开发首个Pygame石头剪刀布游戏,期望点击Rock、Paper、Scissors按钮后,屏幕稳定显示Bot Wins、Player Wins或Match Draw的结果,直到下一次按钮点击。但目前点击Rock按钮无任何显示,偶尔Bot Wins文本仅闪烁不到一秒就消失。
你的代码存在三个核心问题导致结果无法稳定显示:
- 结果仅在按钮点击的单帧绘制,后续循环不再渲染,所以一闪而过
- 电脑选择的逻辑完全错误,没有正确生成随机选择并进行判断
- 缺少全局变量存储当前结果,无法在每帧持续显示
修复后的完整代码
import pygame import random from pygame import mixer # 初始化Pygame pygame.init() # 创建屏幕 screen = pygame.display.set_mode((1000, 667)) # 标题和图标 pygame.display.set_caption("Rock, Paper, Scissors") icon = pygame.image.load("fist.png") pygame.display.set_icon(icon) # 背景图 bg = pygame.image.load("bg_paper.jpg") # 字体 font = pygame.font.SysFont("Sans", 50, bold=True) # 背景音乐 mixer.music.load("bg_song.mp3") mixer.music.play(-1) # 按钮类 class Button(): def __init__(self, x, y, image, scale): width = image.get_width() height = image.get_height() self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) self.rect = self.image.get_rect(topleft=(x, y)) self.clicked = False def draw(self): action = False screen.blit(self.image, (self.rect.x, self.rect.y)) pos = pygame.mouse.get_pos() if self.rect.collidepoint(pos): if pygame.mouse.get_pressed()[0] == 1 and not self.clicked: self.clicked = True action = True if pygame.mouse.get_pressed()[0] == 0: self.clicked = False return action # 按钮图片 button_img = pygame.image.load("button_img.png").convert_alpha() # 文本绘制函数 def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y)) # 创建按钮实例 rock_button = Button(10, 420, button_img, 0.92) paper_button = Button(340, 420, button_img, 0.92) scissors_button = Button(670, 420, button_img, 0.92) # 电脑选择选项 bot_choices = ["Rock", "Paper", "Scissors"] # 全局变量存储当前结果和分数 current_outcome = "" player_score = 0 bot_score = 0 # 判断胜负的函数 def determine_winner(player_choice): global current_outcome, player_score, bot_score bot_choice = random.choice(bot_choices) if player_choice == bot_choice: current_outcome = "Match Draw" elif (player_choice == "Rock" and bot_choice == "Scissors") or \ (player_choice == "Paper" and bot_choice == "Rock") or \ (player_choice == "Scissors" and bot_choice == "Paper"): current_outcome = "Player Wins" player_score += 1 else: current_outcome = "Bot Wins" bot_score += 1 # 游戏主循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((0, 0, 0)) screen.blit(bg, (0, 0)) # 绘制分数 draw_text(f"Score: {player_score}", font, (0, 0, 0), 40, 340) draw_text(f"Bot Score: {bot_score}", font, (0, 0, 0), 500, 340) # 绘制结果标题和当前结果 draw_text("Outcome: ", font, (0, 0, 0), 40, 70) if current_outcome: draw_text(current_outcome, font, (0, 0, 0), 300, 70) # 处理按钮点击 if rock_button.draw(): determine_winner("Rock") print("R") draw_text("ROCK", font, (0, 0, 0), 100, 490) if paper_button.draw(): determine_winner("Paper") print("P") draw_text("PAPER", font, (0, 0, 0), 420, 490) if scissors_button.draw(): determine_winner("Scissors") print("S") draw_text("SCISSORS", font, (0, 0, 0), 700, 490) pygame.display.update()
关键修改说明
- 添加全局结果变量:新增
current_outcome变量存储当前胜负结果,在主循环的每帧都绘制这个变量,确保结果持续显示直到下一次按钮点击。 - 修复电脑选择逻辑:将
bot_choices改为列表,用random.choice()直接获取随机选择,替换原来错误的字典比较逻辑。 - 抽离胜负判断函数:把胜负判断逻辑封装到
determine_winner()函数中,代码更清晰,同时处理分数累加。 - 优化结果绘制:在主循环中独立绘制结果,不再依赖按钮点击的帧,保证结果稳定显示。
- 完善分数系统:新增分数统计功能,让游戏逻辑更完整。
内容的提问来源于stack exchange,提问作者Squid
相关产品推荐
相关产品推荐

