如何让Pygame程序首次生成随机数字后停止持续刷新更新
问题原因
你的随机数生成逻辑写在了start_the_game()函数内部,而这个函数在主while循环里每帧都会被调用一次,所以每次循环都会重新生成新的x、y值,导致数字持续刷新。
解决方法
你猜想的用if语句控制是可行的,这里提供两种修改方案:
方案1:调整变量作用域(更推荐)
把只需要生成一次的随机数、题目文本放到主循环外初始化,主循环内只做界面渲染和事件处理即可:
import pygame import random pygame.init() clock = pygame.time.Clock() surface = pygame.display.set_mode((600, 400)) pygame.display.set_caption("Projecte MatZanfe") font = pygame.font.SysFont('comicsans', 50) base_font = pygame.font.Font(None, 32) user_text = '' color_active = pygame.Color('lightskyblue3') # 把只需要生成一次的内容移到循环外 is_correct = False points = 0 x = random.randint(0,10) y = random.randint(0,10) z = x + y # 题目文本也只需要渲染一次 question_text = font.render (str(x) + "+" + str(y), True, (255,255,255)) input_rect = pygame.Rect(200,200,180,50) def render_game(): surface.fill((255,70,90)) pygame.draw.rect(surface,color_active,input_rect) text_surface = base_font.render(user_text,True,(255,255,255)) # 修正了原代码文字贴输入框边框的问题 surface.blit(text_surface, (input_rect.x+5, input_rect.y+5)) surface.blit(question_text,(260,120)) input_rect.w = max(100,text_surface.get_width()+10) running = True while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 补充原代码缺失的键盘输入处理逻辑 if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: user_text = user_text[:-1] elif event.key == pygame.K_RETURN: # 可在此处添加答案校验、切换下一题的逻辑 if user_text.isdigit() and int(user_text) == z: print("回答正确") user_text = '' else: if event.unicode.isdigit(): user_text += event.unicode render_game() pygame.display.update() pygame.quit()
方案2:用if状态标记控制(符合你预期的if语句实现思路)
如果不想调整原有代码结构,可以加一个标记位控制随机数是否已经生成:
import pygame import random pygame.init() clock = pygame.time.Clock() surface = pygame.display.set_mode((600, 400)) pygame.display.set_caption("Projecte MatZanfe") font = pygame.font.SysFont('comicsans', 50) base_font = pygame.font.Font(None, 32) user_text = '' color_active = pygame.Color('lightskyblue3') # 新增标记位,判断是否已经生成过随机数 num_generated = False x = 0 y = 0 z = 0 question_text = None def start_the_game(): global num_generated, x, y, z, question_text is_correct = False points = 0 # 仅未生成过随机数时执行生成逻辑 if not num_generated: x = random.randint(0,10) y = random.randint(0,10) z = x + y question_text = font.render (str(x) + "+" + str(y), True, (255,255,255)) num_generated = True surface.fill((255,70,90)) input_rect = pygame.Rect(200,200,180,50) pygame.draw.rect(surface,color_active,input_rect) text_surface = base_font.render(user_text,True,(255,255,255)) surface.blit(text_surface, (input_rect.x+5, input_rect.y+5)) surface.blit(question_text,(260,120)) input_rect.w = max(100,text_surface.get_width()+10) running = True while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 补充键盘输入逻辑 if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: user_text = user_text[:-1] elif event.unicode.isdigit(): user_text += event.unicode start_the_game() pygame.display.update() pygame.quit()
内容的提问来源于stack exchange,提问作者Torchllama
相关产品推荐
相关产品推荐

