Joe Doe
Joe Doe

Reputation: 193

Drawing Sprites with button click - Pygame

Would it be good idea to create(draw on the screen) sprite when user click on the one of the buttons? The sprite would be created before and user would only "initialise" it by drawing it to the screen. Unfortunately the code I have got at the moment does not work, it prints "Start" but does not draw sprite. what could be the reason for that?

Code:

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

Upvotes: 0

Views: 655

Answers (2)

furas
furas

Reputation: 142641

Examples:

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

or - if you whan to add more sprites:

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

Upvotes: 1

skyress3000
skyress3000

Reputation: 59

It probably doesn't show up because you have a main loop updating the screen a certain number of times every second. If you want the sprite to show up, you'll have to blit it every time you update the screen.

Upvotes: 0

Related Questions