Created
July 30, 2024 20:00
-
-
Save todbot/95a75a8c226746761a63eba40bb37abd to your computer and use it in GitHub Desktop.
Bouncy John Park heads in CircuitPython
This file contains 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
# bouncy_jpheads.py - use displayio to make simple bounch balls | |
# orignally from: https://gist.github.com/todbot/6f082f452806fad9cd4702c2f1b0aed4 | |
# 30 Jul 2024 - @todbot | |
import time, random | |
import board, displayio, terminalio | |
from adafruit_display_text import label | |
import adafruit_imageload | |
display = board.DISPLAY # CP already sets up display for us, 240x135 for LILYGO T8 ESP32-S2 | |
screen = displayio.Group() # a main group that holds everything | |
display.root_group = screen # add main group to display | |
jp_bitmap,jp_pal = adafruit_imageload.load("jpbitmapsq.bmp") | |
jp_pal.make_transparent(0) | |
jp_tile_grid = displayio.TileGrid(jp_bitmap, pixel_shader=jp_pal) | |
background = Rect(0,0, display.width, display.height, fill=0x000000 ) # background color | |
screen.append(background) | |
num_balls = 10 | |
balls = displayio.Group() # group of balls | |
vees = [] # array of velocities for each ball | |
for i in range(num_balls): | |
fill = random.randint(0,0xffffff) # random color | |
b = displayio.TileGrid(jp_bitmap, pixel_shader=jp_pal, x=display.width//2, y=display.height//2) | |
v = [random.randint(-3,3), random.randint(-3,3)] # random initial velocity | |
vees.append(v) | |
balls.append(b) | |
screen.append(balls) # add ball group to screen | |
hello_lbl = label.Label(terminalio.FONT, text="John Park's\nWorkshop!", color=0xffffff, | |
anchored_position=(display.width//2, display.height//2), | |
anchor_point=(0.5,0.5) ) | |
screen.append(hello_lbl) | |
while True: | |
for i in range(len(balls)): | |
b = balls[i] # get a ball | |
v = vees[i] # get its velocity | |
b.x = int(b.x + v[0]) # update ball position | |
b.y = int(b.y + v[1]) # update ball position | |
# if ball hits edge, bounce it off by reflecting velocity | |
if b.x <= 0 or b.x > display.width: v[0] = -v[0] + random.random()-0.5 | |
if b.y <= 0 or b.y > display.height: v[1] = -v[1] + random.random()-0.5 | |
time.sleep(0.01) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
video demo:
IMG_4112.mp4