Python 使用按钮单击绘制精灵-Pygame
当用户单击其中一个按钮时,创建(在屏幕上绘制)精灵是个好主意吗?精灵将在之前创建,用户只需通过将其绘制到屏幕来“初始化”它。 不幸的是,我现在得到的代码不起作用,它打印“开始”,但不绘制精灵。原因可能是什么 代码:Python 使用按钮单击绘制精灵-Pygame,python,pygame,Python,Pygame,当用户单击其中一个按钮时,创建(在屏幕上绘制)精灵是个好主意吗?精灵将在之前创建,用户只需通过将其绘制到屏幕来“初始化”它。 不幸的是,我现在得到的代码不起作用,它打印“开始”,但不绘制精灵。原因可能是什么 代码: if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: if button.collidepoint(pygame.mouse.get_pos()): scr
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if button.collidepoint(pygame.mouse.get_pos()):
screen.blit(player.image, player.rect.topleft)
print ("Start")
它可能不会出现,因为你有一个主循环,每秒更新屏幕一定次数。如果希望精灵显示,则必须在每次更新屏幕时将其闪烁。示例:
blit_player = False
while True:
# ....
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if button.collidepoint(pygame.mouse.get_pos()):
blit_player = True
print ("Start")
# ....
if blit_player:
screen.blit(player.image, player.rect.topleft)
pygame.display.update
或者-如果要添加更多精灵:
blited_sprites = []
while True:
# ....
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if button.collidepoint(pygame.mouse.get_pos()):
blited_sprites.append( player )
print ("Start")
# ....
for x in blited_sprites:
screen.blit(x.image, x.rect.topleft)
pygame.display.update
这其实很正常。我想你会发现当你想让一个精灵做一些像动画或移动的事情时,它会更有用。发布的另一个答案有一些更完整的代码,您可以查看。