From 0c99576eb555b5237d1e1c7f648e62abd9756c72 Mon Sep 17 00:00:00 2001 From: James Carr Date: Wed, 14 Jul 2021 20:58:12 +0100 Subject: [PATCH] Flying_Toasters + Scrolling_Clouds: Update for CP7 --- CircuitPython_Flying_Toasters/code.py | 33 +++++++++++++++----------- CircuitPython_Scrolling_Clouds/code.py | 23 +++++++++++------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/CircuitPython_Flying_Toasters/code.py b/CircuitPython_Flying_Toasters/code.py index ec65319b3..aec3d8318 100644 --- a/CircuitPython_Flying_Toasters/code.py +++ b/CircuitPython_Flying_Toasters/code.py @@ -11,8 +11,6 @@ Licensed under the MIT license. All text above must be included in any redistribution. - -Requires CircuitPython 5.0 or later. """ import time @@ -37,25 +35,20 @@ FIRST_CELL = CELL_1 LAST_CELL = CELL_4 - NUMBER_OF_CELLS = (LAST_CELL - FIRST_CELL) + 1 # A boolean array corresponding to the sprites, True if it's part of the animation sequence. -ANIMATED = [_sprite >= FIRST_CELL and _sprite <= LAST_CELL for _sprite in range(NUMBER_OF_SPRITES)] - +ANIMATED = [FIRST_CELL <= _sprite <= LAST_CELL for _sprite in range(NUMBER_OF_SPRITES)] # The chance (out of 10) that toast will enter CHANCE_OF_NEW_TOAST = 2 -# How many sprites to styart with +# How many sprites to start with INITIAL_NUMBER_OF_SPRITES = 4 -# Global variables -display = None -tilegrid = None - seed(int(time.monotonic())) + def make_display(): """Set up the display support. Return the Display object. @@ -63,16 +56,17 @@ def make_display(): spi = board.SPI() while not spi.try_lock(): pass - spi.configure(baudrate=24000000) # Configure SPI for 24MHz + spi.configure(baudrate=24000000) # Configure SPI for 24MHz spi.unlock() displayio.release_displays() display_bus = displayio.FourWire(spi, command=board.D7, chip_select=board.D10, reset=board.D9) return ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=True) + def make_tilegrid(): """Construct and return the tilegrid.""" - group = displayio.Group(max_size=10) + group = displayio.Group() sprite_sheet, palette = adafruit_imageload.load("/spritesheet-2x.bmp", bitmap=displayio.Bitmap, @@ -86,16 +80,19 @@ def make_tilegrid(): display.show(group) return grid + def random_cell(): return randint(FIRST_CELL, LAST_CELL) + def evaluate_position(row, col): - """Return whether how long of aa toaster is placable at the given location. + """Return whether how long of a toaster is placeable at the given location. :param row: the tile row (0-9) :param col: the tile column (0-9) """ return tilegrid[col, row] == EMPTY + def seed_toasters(number_of_toasters): """Create the initial toasters so it doesn't start empty""" for _ in range(number_of_toasters): @@ -106,21 +103,25 @@ def seed_toasters(number_of_toasters): break tilegrid[col, row] = random_cell() + def next_sprite(sprite): if ANIMATED[sprite]: return (((sprite - FIRST_CELL) + 1) % NUMBER_OF_CELLS) + FIRST_CELL return sprite + def advance_animation(): """Cycle through animation cells each time.""" for tile_number in range(25): tilegrid[tile_number] = next_sprite(tilegrid[tile_number]) + def slide_tiles(): """Move the tilegrid one pixel to the bottom-left.""" tilegrid.x -= 1 tilegrid.y += 1 + def shift_tiles(): """Move tiles one spot to the left, and reset the tilegrid's position""" for row in range(4, 0, -1): @@ -132,20 +133,23 @@ def shift_tiles(): tilegrid.x = 0 tilegrid.y = -64 + def get_entry_row(): while True: row = randint(0, 4) if tilegrid[4, row] == EMPTY and tilegrid[3, row] == EMPTY: return row + def get_entry_column(): while True: col = randint(0, 3) if tilegrid[col, 0] == EMPTY and tilegrid[col, 1] == EMPTY: return col + def add_toaster_or_toast(): - """Maybe add a new toaster or toast on the right and/or top at a randon open location""" + """Maybe add a new toaster or toast on the right and/or top at a random open location""" if randint(1, 10) <= CHANCE_OF_NEW_TOAST: tile = TOAST else: @@ -158,6 +162,7 @@ def add_toaster_or_toast(): tile = random_cell() tilegrid[get_entry_column(), 0] = tile + display = make_display() tilegrid = make_tilegrid() seed_toasters(INITIAL_NUMBER_OF_SPRITES) diff --git a/CircuitPython_Scrolling_Clouds/code.py b/CircuitPython_Scrolling_Clouds/code.py index e0085a076..56d17392a 100644 --- a/CircuitPython_Scrolling_Clouds/code.py +++ b/CircuitPython_Scrolling_Clouds/code.py @@ -1,6 +1,6 @@ """ Continuously scroll randomly generated Mario style clouds. -Designed fr an ItsyBitsy M4 Express and a 1.3" 240x240 TFT +Designed for an ItsyBitsy M4 Express and a 1.3" 240x240 TFT Adafruit invests time and resources providing this open source code. Please support Adafruit and open source hardware by purchasing @@ -20,6 +20,7 @@ from adafruit_st7789 import ST7789 import adafruit_imageload + # Sprite cell values EMPTY = 0 LEFT = 1 @@ -35,12 +36,9 @@ # The chance an existing cloud gets extended CHANCE_OF_EXTENDING_A_CLOUD = 5 -# Global variables -display = None -tilegrid = None - seed(int(time.monotonic())) + def make_display(): """Set up the display support. Return the Display object. @@ -48,7 +46,7 @@ def make_display(): spi = board.SPI() while not spi.try_lock(): pass - spi.configure(baudrate=24000000) # Configure SPI for 24MHz + spi.configure(baudrate=24000000) # Configure SPI for 24MHz spi.unlock() displayio.release_displays() @@ -56,9 +54,10 @@ def make_display(): return ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=True) + def make_tilegrid(): """Construct and return the tilegrid.""" - group = displayio.Group(max_size=10) + group = displayio.Group() sprite_sheet, palette = adafruit_imageload.load("/tilesheet-2x.bmp", bitmap=displayio.Bitmap, @@ -71,8 +70,9 @@ def make_tilegrid(): display.show(group) return grid + def evaluate_position(row, col): - """Return how long of a cloud is placable at the given location. + """Return how long of a cloud is placeable at the given location. :param row: the tile row (0-4) :param col: the tile column (0-8) """ @@ -83,6 +83,7 @@ def evaluate_position(row, col): end_col += 1 return min([4, end_col - col]) + def seed_clouds(number_of_clouds): """Create the initial clouds so it doesn't start empty""" for _ in range(number_of_clouds): @@ -99,6 +100,7 @@ def seed_clouds(number_of_clouds): tilegrid[col, row] = MIDDLE tilegrid[col + 1, row] = RIGHT + def slide_tiles(): """Move the tilegrid to the left, one pixel at a time, a full time width""" for _ in range(32): @@ -114,6 +116,7 @@ def shift_tiles(): tilegrid[8, row] = EMPTY tilegrid.x = 0 + def extend_clouds(): """Extend any clouds on the right edge, either finishing them with a right end or continuing them with a middle piece @@ -125,8 +128,9 @@ def extend_clouds(): else: tilegrid[8, row] = RIGHT + def add_cloud(): - """Maybe add a new cloud on the right at a randon open row""" + """Maybe add a new cloud on the right at a random open row""" if randint(1, 10) > CHANCE_OF_NEW_CLOUD: count = 0 while True: @@ -138,6 +142,7 @@ def add_cloud(): break tilegrid[8, row] = LEFT + display = make_display() tilegrid = make_tilegrid() seed_clouds(5)