Created
May 7, 2023 17:36
-
-
Save WhiteHusky/e382d576b0e1a69061b4ad916506a8a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://docs.python.org/3/tutorial/classes.html | |
| # Define a new class that inherits `pygame.sprite.Sprite`, thus giving it all | |
| # the methods of `pygame.sprite.Sprite` to `Bird`. You can override these | |
| # methods with your own implementation if needed | |
| # https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite | |
| class Bird(pygame.sprite.Sprite): | |
| # Use __init__, a special method that allows Bird to be constructed by | |
| # simply doing `Bird(x, y)` | |
| def __init__(self, x, y): | |
| # Use `super()`` to allow changing `pygame.sprite.Sprite` to something | |
| # else and to save writing code. | |
| # This is the same as `pygame.sprite.Sprite.init(self)` | |
| # Per the docs, you want to initialize the super class against this one | |
| # so whatever logic in `pygame.sprite.Sprite` can run. | |
| super().__init__(self) | |
| # self is a reference to _this_ instance of the class. | |
| # This case we are adding our own fields to store data important to us | |
| # here. | |
| self.images = [ | |
| pygame.image.load('bird1.png'), | |
| pygame.image.load('bird2.png'), | |
| pygame.image.load('bird3.png') | |
| ] | |
| self.index = 0 | |
| self.image = self.images[self.index] | |
| self.rect = self.image.get_rect() | |
| self.rect.center = [x, y] | |
| self.counter = 0 | |
| # This is a overridden method that `Sprite` provides. Per the docs, it does | |
| # nothing if left as-is, but is used to update the state of `Bird` per tick. | |
| # Judging from the code, it's cycling between it's images whenever | |
| # it's internal counter, incremented on each tick, reaches above | |
| # `bird_cooldown`. This probably is probably to do a simple animation with | |
| # the three images. | |
| def update(self): | |
| self.counter += 1 | |
| bird_cooldown = 5 | |
| if self.counter > bird_cooldown: | |
| self.counter = 0 | |
| self.index += 1 | |
| if self.index >= len(self.images): | |
| self.index = 0 | |
| self.image = self.images[self.index] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment