这个问题在这里已经有了答案:
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
(1 个回答)
去年关闭。
当单击一行中的按钮时,我试图在屏幕上闪烁图像
screen.blit(buttonPlusImage,
[random.randrange(560, 680),
random.randrange(330, 450)])
但是由于我获得的屏幕刷新,图像不久后消失了(我使用这 3 行来更新显示:
pygame.display.flip()
、 FPS.tick(144)
和 screen.fill(white)
)。我怎样才能使图像保留在屏幕上并且不会随着 screen.fill(white) 消失? (我认为这就是导致它的原因)。我将代码粘贴在 https://dpaste.org/qGdi
import pygame
import random
pygame.init()
# variables
mainLoop = True
font = pygame.font.SysFont('comicsansms', 25)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [140, 140, 140]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
screen = pygame.display.set_mode((1300, 700))
# functions
def switchButton01():
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
screen.blit(buttonPlusImage, [random.randrange(560, 680), random.randrange(330, 450)])
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
# window itself and icon
pygame.display.set_caption("incremental adventures")
pygame.display.set_icon(icon)
while mainLoop:
pygame.display.flip()
FPS.tick(144)
screen.fill(white)
# actual content in the game
button01 = pygame.transform.scale(button01, (100, 100))
screen.blit(button01, [580, 350])
click_counter_text = font.render("Click counter: ", True, black, white)
screen.blit(click_counter_text, [525, 50])
button01rect = button01.get_rect(center=(630, 400))
button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
click_counter = font.render((str(clickNum)), True, black, white)
screen.blit(click_counter, [700, 50])
if button01collidepoint:
switchButton01()
# quits
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
最佳答案
我没有改变一切。
避免在应用程序循环中出现多个事件循环。注意, pygame.event.get()
从队列中删除所有事件。因此,第一个或第二个事件循环将获取事件。但不是两个循环。您可以通过在循环中获取一次事件并多次使用事件列表来摆脱这种情况:
while mainLoop:
events = pygame.event.get()
# [...]
if button01collidepoint:
switchButton01(events)
# [...]
for event in events:
# [...]
如果您希望图像留在屏幕上,那么您必须在每一帧中绘制它。添加存储按钮位置的一个 buttonPlusPos
并在主应用程序循环中绘制位置。函数switchButton01
必须返回位置:buttonPlusPos = None
while mainLoop:
# [...]
if button01collidepoint:
buttonPlusPos = switchButton01(events, buttonPlusPos)
if buttonPlusPos:
screen.blit(buttonPlusImage, buttonPlusPos)
函数switchButton01
没有 blit
按钮,它只是返回位置。此外,事件列表被传递给函数,函数处理列表中的事件,而不是检索它的“自己的”列表。在任何情况下,都会返回图像的位置。新位置或当前位置(允许为 None
):def switchButton01(events, buttonPlusPos):
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
return (random.randrange(560, 680), random.randrange(330, 450))
return buttonPlusPos
完整代码:import pygame
import random
pygame.init()
# variables
mainLoop = True
font = pygame.font.SysFont('comicsansms', 25)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [140, 140, 140]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
screen = pygame.display.set_mode((1300, 700))
# functions
def switchButton01(events, buttonPlusPos):
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
return (random.randrange(560, 680), random.randrange(330, 450))
return buttonPlusPos
# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
# window itself and icon
pygame.display.set_caption("incremental adventures")
pygame.display.set_icon(icon)
buttonPlusPos = None
while mainLoop:
pygame.display.flip()
FPS.tick(144)
screen.fill(white)
events = pygame.event.get()
# actual content in the game
button01 = pygame.transform.scale(button01, (100, 100))
screen.blit(button01, [580, 350])
click_counter_text = font.render("Click counter: ", True, black, white)
screen.blit(click_counter_text, [525, 50])
button01rect = button01.get_rect(center=(630, 400))
button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
click_counter = font.render((str(clickNum)), True, black, white)
screen.blit(click_counter, [700, 50])
if button01collidepoint:
buttonPlusPos = switchButton01(events, buttonPlusPos)
if buttonPlusPos:
screen.blit(buttonPlusImage, buttonPlusPos)
# quits
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
关于python - 我正在流血图像,但它在一毫秒左右后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60330787/