Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import pygame
  2.  
  3. class Actor(pygame.sprite.Sprite):
  4. def __init__(self, *grps):
  5. super().__init__(*grps)
  6. # create two images:
  7. # 1 - the no-fire-image
  8. # 2 - the with-fire-image
  9. self.original_image = pygame.Surface((100, 200))
  10. self.original_image.set_colorkey((1,2,3))
  11. self.original_image.fill((1,2,3))
  12. self.original_image.subsurface((0, 100, 100, 100)).fill((255, 255, 255))
  13. self.fire_image = self.original_image.copy()
  14. pygame.draw.rect(self.fire_image, (255, 0, 0), (20, 0, 60, 100))
  15.  
  16. # we'll start with the no-fire-image
  17. self.image = self.fire_image
  18. self.rect = self.image.get_rect(center=(300, 240))
  19.  
  20. # a field to keep track of our timeout
  21. self.timeout = 0
  22.  
  23. def update(self, events, dt):
  24.  
  25. # every frame, substract the amount of time that has passed
  26. # from your timeout. Should not be less than 0.
  27. if self.timeout > 0:
  28. self.timeout = max(self.timeout - dt, 0)
  29.  
  30. # if space is pressed, we make sure the timeout is set to 300ms
  31. pressed = pygame.key.get_pressed()
  32. if pressed[pygame.K_SPACE]:
  33. self.timeout = 300
  34.  
  35. # as long as 'timeout' is > 0, show the with-fire-image
  36. # instead of the default image
  37. self.image = self.original_image if self.timeout == 0 else self.fire_image
  38.  
  39. def main():
  40. pygame.init()
  41. screen = pygame.display.set_mode((600, 480))
  42. screen_rect = screen.get_rect()
  43. clock = pygame.time.Clock()
  44. dt = 0
  45. sprites_grp = pygame.sprite.Group()
  46.  
  47. # create out sprite
  48. Actor(sprites_grp)
  49.  
  50. # standard pygame mainloop
  51. while True:
  52. events = pygame.event.get()
  53. for e in events:
  54. if e.type == pygame.QUIT:
  55. return
  56.  
  57. sprites_grp.update(events, dt)
  58.  
  59. screen.fill((80, 80, 80))
  60. sprites_grp.draw(screen)
  61. pygame.display.flip()
  62. dt = clock.tick(60)
  63.  
  64. if __name__ == '__main__':
  65. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement