我正在做游戏,我想当我在车库里时,如果我购买更多速度并且已经达到最高速度,则会弹出一个文本,提示“达到最高速度!”。但是,当我单击该按钮并达到最大速度时,它不会停留,而是在1滴答声后消失。由于按钮的工作方式,我无法弄清楚如何使其停留更长时间。任何帮助表示赞赏。这是按钮的代码:
elif event.type == pygame.MOUSEBUTTONDOWN:
# 1 is the left mouse button, 2 is middle, 3 is right.
if event.button == 1:
# `event.pos` is the mouse position.
if button2.collidepoint(event.pos):
print('we been clicked')
current_time = time.strftime("%S")
# Increment the number
if player.vel < 20:
if player.coins >= 50:
player.vel += 5
player.coins -= 50
print('speed =' + str(player.vel))
player.vel1 = player.vel
player.grassvel = player.vel // 2
player.save()
else:
print("ur poor")
else:
MaxSpeedReached()
这是它调用的函数的代码:
def MaxSpeedReached():
display_crash_text = False
print("Max speed reached")
if display_crash_text == False:
start_time = time.strftime("%S")
display_crash_text = True
maxspeedtext = pygame.font.Font("freesansbold.ttf", 20)
maxspedd, maxspeedr = text_objects("Max Speed Reached!", maxspeedtext, BLACK)
maxspeedr.center = ((205, 270))
print(current_time, start_time)
if display_crash_text == True:
win.blit(maxspedd, maxspeedr)
if int(start_time) - int(current_time) < 3:
display_crash_text = False
print("yo we checking the time difference")
显然,您会看到此消息,因为MaxSpeedReached()
调用时会绘制一次消息,但是到达此函数的唯一可能路径是pygame.MOUSEBUTTONDOWN
事件。
每当出现最大速度的条件时,您都需要在主循环中绘制消息。
MAX_VELOCITY = 20
maxspeedtext = pygame.font.Font("freesansbold.ttf", 20)
maxspedd, maxspeedr = text_objects("Max Speed Reached!", maxspeedtext, BLACK)
maxspeedr.center = ((205, 270))
[ ... ]
# Main loop
while not finished:
[...]
if ( player.vel >= MAX_VELOCITY ):
win.blit( maxspedd, maxspeedr )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句