如何更新 pygame 模块中按钮单击时出现的文本
How to update text appearing on button click in pygame module
我编写了一段代码,点击按钮后,将从 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()
我怎样才能让文本在下一帧中不消失?
尝试使用另一个布尔变量
if event.type == pygame.MOUSEBUTTONDOWN:
if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
show_text = True
if show_text:
time_check = pygame.time.get_ticks ()
b_font = font.render (random.choice (my_list), True,(255,255,255))
screen.blit (b_font, (50, 100)
if time_check >= 2000: #2000 miliseconds
show_text = False
time_check = 0
这里会显示你的文字两秒
不要在主应用程序循环之前创建 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()
我编写了一段代码,点击按钮后,将从 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()
我怎样才能让文本在下一帧中不消失?
尝试使用另一个布尔变量
if event.type == pygame.MOUSEBUTTONDOWN:
if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
show_text = True
if show_text:
time_check = pygame.time.get_ticks ()
b_font = font.render (random.choice (my_list), True,(255,255,255))
screen.blit (b_font, (50, 100)
if time_check >= 2000: #2000 miliseconds
show_text = False
time_check = 0
这里会显示你的文字两秒
不要在主应用程序循环之前创建 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()