首页 > 解决方案 > 如何更新pygame模块中按钮单击时出现的文本

问题描述

我正在编写一个代码,在单击按钮时,将从 myList 中随机选择一个字符串并显示出来。我正在使用 pygame 模块执行此操作。这里的问题是文本不会保留,它只是闪烁一帧。这是代码:

import pygame
import random

pygame.init()
size = (500, 400)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Project")
bg = pygame.image.load("bg.jpg")

# declaring variables and lists
white = (255, 255, 255)
black = (0, 0, 0)
light_grey = (224, 224, 224)
dark_grey = (200, 200, 200)

text = pygame.font.SysFont("Agency FB", 20)
myList = ["China", "Italy", "Russia", "India", "USA", "Canada", "France", "Japan", "Brazil", "Egypt"]
# text for button
button_text = text.render("Country", True, black)

rb = random.choice(myList)
font = pygame.font.SysFont("Agency FB", 50)
bFont = font.render(str(rb), True, white)

var = True
while var:
    screen.blit(bg, (0, 0))
    # store mouse position
    mouse = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            var = False
        # button click
        if event.type == pygame.MOUSEBUTTONDOWN:
            # pick one from list and blit
            if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
                screen.blit(bFont, (50, 100))
    # make button
    if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
        pygame.draw.rect(screen, dark_grey, (50, 350, 75, 35), 0)
    else:
        pygame.draw.rect(screen, light_grey, (50, 350, 75, 35), 0)
    screen.blit(button_text, (55, 355))

    pygame.display.update()

我怎样才能让文本在那里并且不会在下一帧中消失?

标签: pythonbuttontextpygamepygame-surface

解决方案


不要bFont在主应用程序循环之前创建,而是使用以下命令对其进行初始化None

bFont = None 

按下按钮时选择并渲染一个随机字符串:

while var:
    
    # [...]
    for event in pygame.event.get():
        # [...]

        # button click
        if event.type == pygame.MOUSEBUTTONDOWN:
            # pick one from list and blit
            button_rect = pygame.Rect(50, 350, 75, 35)
            if button_rect.collidepoint(event.pos):
                rb = random.choice(myList)
                bFont = font.render(str(rb), True, white)

bFont如果设置了,则在主应用程序循环中绘制文本:

var = True
while var:
    # [...]

    if bFont:
        screen.blit(bFont, (50, 100))
    pygame.display.update()

完整示例:

import pygame
import random

pygame.init()
size = (500, 400)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Project")
bg = pygame.image.load("bg.jpg")

# declaring variables and lists
white = (255, 255, 255)
black = (0, 0, 0)
light_grey = (224, 224, 224)
dark_grey = (200, 200, 200)

text = pygame.font.SysFont("Agency FB", 20)
myList = ["China", "Italy", "Russia", "India", "USA", "Canada", "France", "Japan", "Brazil", "Egypt"]
# text for button
button_text = text.render("Country", True, black)
button_rect = pygame.Rect(50, 350, 75, 35)

font = pygame.font.SysFont("Agency FB", 50)
bFont = None 

var = True
while var:
    # store mouse position
    mouse = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            var = False
        # button click
        if event.type == pygame.MOUSEBUTTONDOWN:
            # pick one from list and blit
            if button_rect.collidepoint(event.pos):
                rb = random.choice(myList)
                bFont = font.render(str(rb), True, white)
    
    screen.blit(bg, (0, 0))

    # make button
    if button_rect.collidepoint(mouse):
        pygame.draw.rect(screen, dark_grey, (50, 350, 75, 35), 0)
    else:
        pygame.draw.rect(screen, light_grey, (50, 350, 75, 35), 0)
    screen.blit(button_text, (55, 355))

    if bFont:
        screen.blit(bFont, (50, 100))
    pygame.display.update()

推荐阅读