Skip to content

Flying_Toasters + Scrolling_Clouds: Update for CP7 #1614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions CircuitPython_Flying_Toasters/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,42 +35,38 @@

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.
"""
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,
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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)
Expand Down
23 changes: 14 additions & 9 deletions CircuitPython_Scrolling_Clouds/code.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +20,7 @@
from adafruit_st7789 import ST7789
import adafruit_imageload


# Sprite cell values
EMPTY = 0
LEFT = 1
Expand All @@ -35,30 +36,28 @@
# 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.
"""
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("/tilesheet-2x.bmp",
bitmap=displayio.Bitmap,
Expand All @@ -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)
"""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -138,6 +142,7 @@ def add_cloud():
break
tilegrid[8, row] = LEFT


display = make_display()
tilegrid = make_tilegrid()
seed_clouds(5)
Expand Down