/ TrelliBird / bird.py
bird.py
 1  # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  FlappyBird type game for the NeoTrellisM4
 7  
 8  Adafruit invests time and resources providing this open source code.
 9  Please support Adafruit and open source hardware by purchasing
10  products from Adafruit!
11  
12  Written by Dave Astels for Adafruit Industries
13  Copyright (c) 2018 Adafruit Industries
14  Licensed under the MIT license.
15  
16  All text above must be included in any redistribution.
17  """
18  
19  # pylint: disable=wildcard-import,unused-wildcard-import,eval-used
20  
21  import time
22  from color_names import *
23  
24  class Bird(object):
25      """The 'bird': the user's piece."""
26  
27      def __init__(self, weight=0.5):
28          """Initialize a Bird instance.
29          weight -- the weight of the bird (default 0.5)
30          """
31          self._position = 0.75
32          self._weight = weight
33  
34      def _y_position(self):
35          """Get the verical pixel position."""
36          if self._position >= 0.75:
37              return 0
38          elif self._position >= 0.5:
39              return 1
40          elif self._position >= 0.25:
41              return 2
42          return 3
43  
44      def _move_up(self, amount):
45          """Move the bird up.
46          amount -- how much to move up, 0.0-1.0
47          """
48          self._position = min(1.0, self._position + amount)
49  
50      def _move_down(self, amount):
51          """Move the bird down.
52          amount -- how much to move down, 0.0-1.0
53          """
54          self._position = max(0.0, self._position - amount)
55  
56      def flap(self):
57          """Flap. This moves the bird up by a fixed amount."""
58          self._move_up(0.25)
59  
60      def update(self):
61          """Periodic update: add the effect of gravity."""
62          self._move_down(0.05 * self._weight)
63  
64      def did_hit_ground(self):
65          """Return whether this bird hit the ground."""
66          return self._position == 0.0
67  
68      def is_colliding_with(self, post):
69          """Check for a collision.
70          post -- the Post instance to check for a collicion with
71          """
72          return post.is_collision_at(3, self._y_position())
73  
74      def draw_on(self, trellis, color=YELLOW):
75          """Draw the bird.
76          trellis -- the TrellisM4Express instance to use as a screen
77          color   -- the color to display as (default YELLOW)
78          """
79          trellis.pixels[3, self._y_position()] = color
80  
81      def flash(self, trellis):
82          """Flash between RED and YELLOW to indicate a collision.
83          trellis -- the TrellisM4Express instance to use as a screen """
84          for _ in range(5):
85              time.sleep(0.1)
86              self.draw_on(trellis, RED)
87              trellis.pixels.show()
88              time.sleep(0.1)
89              self.draw_on(trellis, YELLOW)
90              trellis.pixels.show()