如何在Pygame中更新显示文本?变量修改后界面无变化问题
Pygame文本不更新问题解决
我已经成功显示了文本矩形,显示内容赋值给变量msg。但在脚本中延迟几秒修改msg的值后,变量本身已经改变,界面上的文本却没有更新。即使调用了pygame.display.update(),问题仍然存在。
HORL.py
import pygame, sys, time, random, math from functions import horl, number, starting, msg from pygame.locals import QUIT pygame.init() clock = pygame.time.Clock() screenWidth = 800 screenHeight = 600 screen = pygame.display.set_mode((screenWidth , screenHeight)) pygame.display.set_caption('Higher or Lower') pygame.display.set_icon(pygame.image.load('sprites/icons/icon.ico')) # fonts pixel_serif = pygame.font.Font('fonts/pixelserif.ttf', 32) # name name = pixel_serif.render('Higher or Lower', True, 0) namerect = name.get_rect() namerect.center = (400, 75) # backgrounds eight_to_six = pygame.image.load('sprites/background/800x600.png') # buttons higher = pygame.image.load('sprites/icons/highButton.png') lower = pygame.image.load('sprites/icons/lowButton.png') # text message = pixel_serif.render(msg, True, 0) msgrect = message.get_rect() msgrect.center = (400, 300) num = pixel_serif.render(str(starting), True, 0) numrect = num.get_rect() numrect.center = (400, 350) hpressed = False lpressed = False running = True while running: clock.tick(60) mouse_pos = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and hButton.collidepoint(mouse_pos) and hpressed == False: hpressed = True higher = pygame.image.load('sprites/icons/highPressed.png') horl('H') elif event.button == 1 and lButton.collidepoint(mouse_pos) and lpressed == False: lpressed = True lower = pygame.image.load('sprites/icons/lowPressed.png') horl('L') if event.type == pygame.MOUSEBUTTONUP: time.sleep(1) higher = pygame.image.load('sprites/icons/highButton.png') lower = pygame.image.load('sprites/icons/lowButton.png') hpressed = False lpressed = False screen.blit((eight_to_six), (0, 0)) screen.blit(name, namerect) screen.blit(message, msgrect) screen.blit(num, numrect) hButton = screen.blit(higher,(200,400)) lButton = screen.blit(lower,(500,400)) pygame.display.update()
functions.py
import pygame, time, random from random import randint, choice class color: white = '255, 255, 255' black = '0, 0, 0' red = '255, 0, 0' green = '0, 255, 0' wins = 0 loss = 0 rounds = 0 starting = randint(0,13) number = choice([i for i in range(0,13) if i not in [starting]]) msg = 'The starting number is' def correct(yn): if yn == 'y': starting = number print('msg y') msg = 'Correct! The number is',number elif yn == 'n': starting = number print('msg n') msg = 'Incorrect! The Number is',number def horl(opt): global number global wins global loss global msg if opt == 'H': if number > int(starting): correct('y') wins = wins + 1 elif number < int(starting): correct('n') loss = loss + 1 elif opt == 'L': if number < int(starting): correct('y') wins = wins + 1 elif number > int(starting): correct('n') loss = loss + 1 else: print(color.error + '\nSYNTAX ERROR' + color.normal)
问题根源及修复步骤
未重新渲染文本Surface
HORL.py中,message和num仅在初始化时渲染一次,后续msg或starting变化后,没有重新生成新的渲染Surface,导致界面始终显示旧内容。
修复:将文本渲染代码移入主循环内部,每次循环都重新生成Surface:# 移除初始化时的message和num赋值,在while running循环的screen.blit之前添加: message = pixel_serif.render(msg, True, 0) num = pixel_serif.render(str(starting), True, 0)函数内未声明全局变量
functions.py的correct函数中,修改starting和msg时未声明它们为全局变量,实际修改的是函数局部变量,全局变量并未更新。
修复:在correct函数开头添加全局声明:def correct(yn): global starting, msg # 原有代码...错误的字符串赋值方式
msg = 'Correct! The number is',number会创建元组而非字符串,pygame无法正确渲染元组。
修复:改用f-string或字符串拼接:msg = f'Correct! The number is {number}' # 或 msg = 'Correct! The number is ' + str(number)阻塞式延迟导致程序卡顿
time.sleep(1)会阻塞整个事件循环,期间无法处理事件和更新界面,应改用pygame时间事件实现延迟。
修复:- 初始化部分添加自定义事件:
RESET_BUTTON_EVENT = pygame.USEREVENT + 1 - 修改鼠标抬起事件逻辑:
if event.type == pygame.MOUSEBUTTONUP: pygame.time.set_timer(RESET_BUTTON_EVENT, 1000) # 1秒后触发事件 - 在事件循环中添加自定义事件处理:
elif event.type == RESET_BUTTON_EVENT: higher = pygame.image.load('sprites/icons/highButton.png') lower = pygame.image.load('sprites/icons/lowButton.png') hpressed = False lpressed = False pygame.time.set_timer(RESET_BUTTON_EVENT, 0) # 关闭定时器
- 初始化部分添加自定义事件:
内容的提问来源于stack exchange,提问作者Bennett Kohler
相关产品推荐
相关产品推荐

