Esempio n. 1
0
    def __init__(self):
        #path = os.path.abspath(os.path.dirname(__file__))
        path = __file__.rsplit('/', 1)[0] + '/' if '/' in __file__ else ''
        self.tiles, self.palette = adafruit_imageload.load(
            path + "tiles.gif",
            bitmap=displayio.Bitmap,
            palette=displayio.Palette)
        self.palette.make_transparent(15)
        self.pew_sound = open(path + "pew.wav", 'rb')
        self.boom_sound = open(path + "boom.wav", 'rb')

        self.root = displayio.Group(max_size=3)
        self.sprites = displayio.Group(max_size=8)
        space_palette = displayio.Palette(16)
        for i in range(16):
            space_palette[i] = self.palette[i]
        space_palette[15] = space_palette[0]
        self.space = displayio.TileGrid(self.tiles,
                                        pixel_shader=space_palette,
                                        width=16,
                                        height=16,
                                        tile_width=16,
                                        tile_height=16,
                                        default_tile=0)
        for i in range(8):
            self.space[random.randint(0, 7),
                       random.randint(0, 7)] = random.randint(1, 2)
        self.ship = Ship(self)
        self.aliens = Aliens(self)
        self.saucer = Saucer(self)
        self.bomb = Bomb(self)
        self.missiles = [Missile(self, 0), Missile(self, 1), Missile(self, 2)]
        self.mobs = [self.ship, self.aliens, self.saucer, self.bomb
                     ] + self.missiles
        self.root.append(self.space)
        self.root.append(self.sprites)

        w, h = terminalio.FONT.get_bounding_box()
        text_palette = displayio.Palette(2)
        text_palette[0] = 0x000000
        text_palette[1] = 0xFFFFFF
        text_palette.make_transparent(0)
        text_grid = displayio.TileGrid(terminalio.FONT.bitmap,
                                       pixel_shader=text_palette,
                                       tile_width=w,
                                       tile_height=h,
                                       width=9,
                                       height=1)
        text_grid.x = (controls.display.width - 9 * w) // 2
        text_grid.y = (controls.display.height - h) // 2
        self.text = terminalio.Terminal(text_grid, terminalio.FONT)
        self.root.append(text_grid)

        self.last_tick = time.monotonic()

        controls.display.show(self.root)
        controls.audio.mute(False)
Esempio n. 2
0
def menu_choice(seq, button_ok, button_cancel=0, *, sel_idx=0, text_font=font):
    """Display a menu and allow a choice from it"""
    gc.collect()
    board.DISPLAY.auto_refresh = True
    scroll_idx = 0
    glyph_width, glyph_height = text_font.get_bounding_box()[:2]
    num_rows = min(len(seq), board.DISPLAY.height // glyph_height)
    max_glyphs = board.DISPLAY.width // glyph_width
    palette = displayio.Palette(2)
    palette[0] = 0
    palette[1] = 0xffffff
    labels = [
        displayio.TileGrid(text_font.bitmap,
                           pixel_shader=palette,
                           width=max_glyphs + 1,
                           height=1,
                           tile_width=glyph_width,
                           tile_height=glyph_height) for i in range(num_rows)
    ]
    terminals = [terminalio.Terminal(li, text_font) for li in labels]
    cursor = adafruit_display_text.label.Label(text_font,
                                               max_glyphs=1,
                                               color=0xddddff)
    base_y = 0
    caret_offset = glyph_height // 2 - 1
    scene = displayio.Group(max_size=len(labels) + 1)
    for i, label in enumerate(labels):
        label.x = round(glyph_width * 1.5)
        label.y = base_y + glyph_height * i
        scene.append(label)
    cursor.x = 0
    cursor.y = caret_offset
    cursor.text = ">"
    scene.append(cursor)

    last_scroll_idx = max(0, len(seq) - num_rows)

    board.DISPLAY.show(scene)
    buttons.get_pressed()  # Clear out anything from before now
    i = 0
    old_scroll_idx = None

    while True:
        enable.value = speaker.playing
        pressed = buttons.get_pressed()
        if button_cancel and (pressed & button_cancel):
            return -1
        if pressed & button_ok:
            return sel_idx

        joystick.poll()
        if up_key.value:
            sel_idx -= 1
        if down_key.value:
            sel_idx += 1

        sel_idx = min(len(seq) - 1, max(0, sel_idx))

        if scroll_idx > sel_idx or scroll_idx + num_rows <= sel_idx:
            scroll_idx = sel_idx - num_rows // 2
        scroll_idx = min(last_scroll_idx, max(0, scroll_idx))

        board.DISPLAY.auto_refresh = False
        if old_scroll_idx != scroll_idx:
            for i in range(scroll_idx, scroll_idx + num_rows):
                j = i - scroll_idx
                new_text = ''
                if i < len(seq):
                    new_text = seq[i][:max_glyphs]
                terminals[j].write('\r\033[K')
                terminals[j].write(new_text)
        cursor.y = caret_offset + base_y + glyph_height * (sel_idx -
                                                           scroll_idx)
        board.DISPLAY.auto_refresh = True
        old_scroll_idx = scroll_idx

        time.sleep(1 / 20)
Esempio n. 3
0
# output text
p = displayio.Palette(2)
p.make_transparent(0)
p[1] = 0x000000

w, h = terminalio.FONT.get_bounding_box()
tilegrid = displayio.TileGrid(terminalio.FONT.bitmap,
                              pixel_shader=p,
                              x=margin * 2 + border,
                              y=int(margin + border + header + margin / 2),
                              width=48,
                              height=10,
                              tile_width=w,
                              tile_height=h)
term = terminalio.Terminal(tilegrid, terminalio.FONT)
splash.append(tilegrid)

# input textarea
input_rect = Rect(margin,
                  display.height - margin - header,
                  display.width - margin * 2,
                  header,
                  fill=0xFFFFFF,
                  outline=0x666666)
splash.append(input_rect)

# input text
input_text = Label(terminalio.FONT,
                   text='',
                   x=margin * 2 + border,
palette[3] = 0xee00bb
palette[4] = 0xbbee00
palette[5] = 0xbb00ee
text_palette = displayio.Palette(2)
text_palette[0] = 0x111111
text_palette[1] = 0xffeedd
w, h = terminalio.FONT.get_bounding_box()
text_grid = displayio.TileGrid(terminalio.FONT.bitmap,
                               tile_width=w,
                               tile_height=h,
                               pixel_shader=text_palette,
                               width=8,
                               height=1)
text_grid.x = 96
text_grid.y = 48
text = terminalio.Terminal(text_grid, terminalio.FONT)
screen = displayio.Bitmap(10, 20, 6)
preview = displayio.Bitmap(4, 4, 6)
bricks = displayio.Group(scale=8)
bricks.append(displayio.TileGrid(screen, pixel_shader=palette, x=0, y=-4))
bricks.append(displayio.TileGrid(preview, pixel_shader=palette, x=12, y=0))
root = displayio.Group()
root.append(bricks)
root.append(text_grid)
board.DISPLAY.show(root)
buttons = gamepad.GamePad(
    digitalio.DigitalInOut(board.BUTTON_O),
    digitalio.DigitalInOut(board.BUTTON_X),
    digitalio.DigitalInOut(board.BUTTON_Z),
    digitalio.DigitalInOut(board.BUTTON_DOWN),
    digitalio.DigitalInOut(board.BUTTON_LEFT),