Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.

lines in CircuitPython
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

lines in CircuitPython

by fid on Tue May 19, 2020 5:08 pm

I wanted to create some animated lines on the screen of the PyGamer that look like falling rain. If I started the raindrop off of the screen it would stop the program because of writing outside the boundaries of the screen. Likewise drawing the raindrops past the bottom of the screen caused the program to stop (fail or crash, whatever you want to call it.) I decided to write a function that would draw the parts of the lines that are only on the screen.

The example will have a set of lines that are radiating from one point well above the screen and terminating below the screen. A second color will have a second set of lines radiating from below the screen and terminating above the screen.
PyGamerLines.png (511 KiB) Viewed 49 times

(I had to design and print a new cap for the joystick.)
Code: Select all | TOGGLE FULL SIZE
# screen size is 160x128

import board
import displayio

display = board.DISPLAY

bitmap = displayio.Bitmap(display.width, display.height, 3)
palette = displayio.Palette(3)
palette[0] = 0x000000  # This is the background of the display
palette[1] = 0x8A2BE2  # This is the foreground, Purple
palette[2] = 0xFF0000

# TileGrid for the bitmap
bitmap_grid = displayio.TileGrid(bitmap, pixel_shader=palette)

group = displayio.Group()

x1 = 80
y1 = -40
x2 = 0
y2 = 180
x = 0
y = 0

def lineIt(x1, y1, y2, color):
    for x2 in range(-240, 400, 20):
        for y in range(128):
            x = int((((x2 - x1)/(y2 - y1))*(y - y1)) + x1)
            if x > -1 and x < 160:
                bitmap[x, y] = color

lineIt(x1, y1, y2, 1)
lineIt(x1, 180, -40, 2)

while True:

It won't take much to modify the code to create the animation I was looking for with respect to the raindrops. For a different view of the moire patterns I changed the step in the line that reads for x2 in range(-240, 400, 20) to read for x2 in range(-240, 400, 10). You can also change the colors in the lines beginning with lineIt to be the same to show the moire patterns a little better.

I bumped into Anne Barela on Twitter and she said this is a good place to share code. Thanks Anne.

Posts: 55
Joined: Wed Sep 25, 2013 3:00 pm

Re: lines in CircuitPython

by siddacious on Mon May 25, 2020 8:51 pm

Thanks for sharing :)

Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Please be positive and constructive with your questions and comments.