Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- class Actor(pygame.sprite.Sprite):
- def __init__(self, *grps):
- super().__init__(*grps)
- # create two images:
- # 1 - the no-fire-image
- # 2 - the with-fire-image
- self.original_image = pygame.Surface((100, 200))
- self.original_image.set_colorkey((1,2,3))
- self.original_image.fill((1,2,3))
- self.original_image.subsurface((0, 100, 100, 100)).fill((255, 255, 255))
- self.fire_image = self.original_image.copy()
- pygame.draw.rect(self.fire_image, (255, 0, 0), (20, 0, 60, 100))
- # we'll start with the no-fire-image
- self.image = self.fire_image
- self.rect = self.image.get_rect(center=(300, 240))
- # a field to keep track of our timeout
- self.timeout = 0
- def update(self, events, dt):
- # every frame, substract the amount of time that has passed
- # from your timeout. Should not be less than 0.
- if self.timeout > 0:
- self.timeout = max(self.timeout - dt, 0)
- # if space is pressed, we make sure the timeout is set to 300ms
- pressed = pygame.key.get_pressed()
- if pressed[pygame.K_SPACE]:
- self.timeout = 300
- # as long as 'timeout' is > 0, show the with-fire-image
- # instead of the default image
- self.image = self.original_image if self.timeout == 0 else self.fire_image
- def main():
- pygame.init()
- screen = pygame.display.set_mode((600, 480))
- screen_rect = screen.get_rect()
- clock = pygame.time.Clock()
- dt = 0
- sprites_grp = pygame.sprite.Group()
- # create out sprite
- Actor(sprites_grp)
- # standard pygame mainloop
- while True:
- events = pygame.event.get()
- for e in events:
- if e.type == pygame.QUIT:
- return
- sprites_grp.update(events, dt)
- screen.fill((80, 80, 80))
- sprites_grp.draw(screen)
- pygame.display.flip()
- dt = clock.tick(60)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement