Friday, December 4, 2020

circuitpython example with text

Now with some text as well (and I grant this python code to the public domain as well.)

import time

import board
import displayio
import digitalio

import vectorio
import gamepadshift
import terminalio

BUTTON_LEFT = 128
BUTTON_UP = 64
BUTTON_DOWN = 32
BUTTON_RIGHT = 16
BUTTON_SELECT = 8
BUTTON_START = 4
BUTTON_A = 2
BUTTON_B = 1

print('About to begin.')
for i in range(3,0,-1):
    print(i)
    time.sleep(0.2)


circle = vectorio.Circle(10)
circle2 = vectorio.Circle(6)

display = board.DISPLAY

palette = displayio.Palette(2)
palette[0] = 0x000000
palette[1] = 0xFFFFFF
palette.make_transparent(0)

def get_char_tile(c, x, y):
    g = terminalio.FONT.get_glyph(ord(c))
    tile = displayio.TileGrid(g.bitmap, pixel_shader=palette,  tile_width=g.width, tile_height=g.height, default_tile=g.tile_index, x=x, y=y)
    return tile

def get_string_group(s, x, y):
    text_w, text_h = terminalio.FONT.get_bounding_box()
    group = displayio.Group(max_size=len(s))
    for i, c in enumerate(s):
        group.append(get_char_tile(c, x+text_w*i, y))
    return group


palette2 = displayio.Palette(2)
palette2[0] = 0xFF0000
palette2[1] = 0x0000FF
palette2.make_transparent(0)

vect = vectorio.VectorShape(shape=circle, pixel_shader=palette)
vect2 = vectorio.VectorShape(shape=circle2, pixel_shader=palette2, x=5, y=-5)
s = get_string_group("Hey World!",15,0)
vect3 = vectorio.VectorShape(shape=vectorio.Rectangle(2, 10), pixel_shader=palette2)

move_group = displayio.Group()
move_group.append(vect)
move_group.append(vect2)
group = displayio.Group()
group.append(move_group)
group.append(s)
group.append(vect3)


display.show(group)
#display.show(group2)

group.x = 20
group.y = 20

pad = gamepadshift.GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
                                digitalio.DigitalInOut(board.BUTTON_OUT),
                                digitalio.DigitalInOut(board.BUTTON_LATCH))
button = 0
while button != BUTTON_START:
    button = pad.get_pressed()
    if button & BUTTON_UP == BUTTON_UP:
        move_group.y += -1
    if button & BUTTON_DOWN == BUTTON_DOWN:
        move_group.y += 1
    if button & BUTTON_LEFT == BUTTON_LEFT:
        move_group.x += -1
    if button & BUTTON_RIGHT == BUTTON_RIGHT:
        move_group.x += 1
    time.sleep(0.01)
    move_group.x %= display.width
    move_group.y %= display.height


No comments:

Post a Comment