/ TrelliBird / post.py
post.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 from color_names import * 22 23 24 class Post(object): 25 """Obstacles the user must avoice colliding with.""" 26 27 def __init__(self, from_bottom=0, from_top=0): 28 """Initialize a Post instance. 29 from_bottom -- how far the post extends from the bottom of the screen (default 0) 30 from_top -- how far the post extends from the top of the screen (default 0) 31 """ 32 self._x = 7 33 self._top = from_top 34 self._bottom = from_bottom 35 36 def update(self): 37 """Periodic update: move one step to the left.""" 38 self._x -= 1 39 40 def _on_post(self, x, y): 41 """Determine whether the supplied coordinate is occupied by part of this post. 42 x -- the horizontal pixel coordinate 43 y -- the vertical pixel coordinate 44 """ 45 return x == self._x and (y < self._top or y > (3 - self._bottom)) 46 47 def draw_on(self, trellis): 48 """Draw this post on the screen. 49 trellis -- the TrellisM4Express instance to use as a screen 50 """ 51 for i in range(4): 52 if self._on_post(self._x, i): 53 trellis.pixels[self._x, i] = GREEN 54 55 def is_collision_at(self, x, y): 56 """Determine whether something at the supplied coordinate is colliding with this post. 57 x -- the horizontal pixel coordinate 58 y -- the vertical pixel coordinate 59 """ 60 return self._on_post(x, y) 61 62 @property 63 def off_screen(self): 64 """Return whether this post has moved off the left edge of the screen.""" 65 return self._x < 0