用按钮单击绘制精灵 - Pygame

时间:2014-07-07 18:34:32

标签: python pygame

当用户点击其中一个按钮时,创建(在屏幕上绘制)精灵是否是个好主意?精灵将在之前创建,用户只能通过将其绘制到屏幕来“初始化”它。 不幸的是,我目前获得的代码不起作用,它打印“开始”但不绘制精灵。可能是什么原因?

代码:

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")

2 个答案:

答案 0 :(得分:1)

示例:

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

答案 1 :(得分:0)

它可能没有显示,因为你有一个主循环每秒更新屏幕一定次数。如果你想要显示精灵,你每次更新屏幕时都必须进行blit。