Skip to content

Tilepalettemapper api update #3034

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 4 commits into from
May 20, 2025
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
10 changes: 9 additions & 1 deletion Metro/Metro_RP2350_CircuitPython_Matrix/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import supervisor
from displayio import Group, TileGrid
from tilepalettemapper import TilePaletteMapper
from adafruit_fruitjam.peripherals import request_display_config
import adafruit_imageload


# use the built-in HSTX display
request_display_config(320, 240)
display = supervisor.runtime.display

# screen size in tiles, tiles are 16x16
Expand Down Expand Up @@ -64,7 +67,12 @@
shader_palette[i + 1] = COLORS[i]

# mapper to change colors of tiles within the grid
grid_color_shader = TilePaletteMapper(shader_palette, 2, SCREEN_WIDTH, SCREEN_HEIGHT)
if sys.implementation.version[0] == 9:
grid_color_shader = TilePaletteMapper(
shader_palette, 2, SCREEN_WIDTH, SCREEN_HEIGHT
)
elif sys.implementation.version[0] >= 10:
grid_color_shader = TilePaletteMapper(shader_palette, 2)

# load the spritesheet
katakana_bmp, katakana_pixelshader = adafruit_imageload.load("matrix_characters.bmp")
Expand Down
12 changes: 9 additions & 3 deletions Metro/Metro_RP2350_Match3/match3_game/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import atexit
import io
import os
import sys
import time

import board
Expand Down Expand Up @@ -217,9 +218,11 @@

# create tile palette mappers
for i in range(2):
palette_mapper = TilePaletteMapper(remap_palette, 3, 1, 1)
# remap index 2 to each of the colors in mouse colors list
palette_mapper[0] = [0, 1, i + 3]
if sys.implementation.version[0] == 9:
palette_mapper = TilePaletteMapper(remap_palette, 3, 1, 1)
elif sys.implementation.version[0] >= 10:
palette_mapper = TilePaletteMapper(remap_palette, 3)

palette_mappers.append(palette_mapper)

# create tilegrid for each mouse
Expand All @@ -228,6 +231,9 @@
mouse_tg.y = display.height // scale_factor // 2
mouse_tgs.append(mouse_tg)

# remap index 2 to each of the colors in mouse colors list
palette_mapper[0] = [0, 1, i + 3]

# USB info lists
mouse_interface_indexes = []
mouse_endpoint_addresses = []
Expand Down
19 changes: 13 additions & 6 deletions Metro/Metro_RP2350_Match3/match3_game/match3_game_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT
import os
import random
import sys
import time
from io import BytesIO

Expand Down Expand Up @@ -133,7 +134,10 @@ class Match3Card(Group):

def __init__(self, card_tuple, **kwargs):
# tile palette mapper to color the card
self._mapper = TilePaletteMapper(kwargs["pixel_shader"], 5, 1, 1)
if sys.implementation.version[0] == 9:
self._mapper = TilePaletteMapper(kwargs["pixel_shader"], 5, 1, 1)
elif sys.implementation.version[0] >= 10:
self._mapper = TilePaletteMapper(kwargs["pixel_shader"], 5)
kwargs["pixel_shader"] = self._mapper
# tile grid to for the visible sprite
self._tilegrid = TileGrid(**kwargs)
Expand Down Expand Up @@ -580,9 +584,11 @@ def handle_left_click(self, player_index, coords):
# if 3 cards have been clicked
if len(self.clicked_cards) == 3:
# check if the 3 cards make a valid set
valid_set = validate_set(self.clicked_cards[0],
self.clicked_cards[1],
self.clicked_cards[2])
valid_set = validate_set(
self.clicked_cards[0],
self.clicked_cards[1],
self.clicked_cards[2],
)

# if they are a valid set
if valid_set:
Expand Down Expand Up @@ -660,7 +666,7 @@ def handle_left_click(self, player_index, coords):
# load the game from the given game state
self.load_from_game_state(self.game_state)
# hide the title screen
self.title_screen.hidden = True # pylint: disable=attribute-defined-outside-init
self.title_screen.hidden = True
# set the current state to open play
self.cur_state = STATE_PLAYING_OPEN

Expand All @@ -676,7 +682,7 @@ def handle_left_click(self, player_index, coords):
# initialize a new game
self.init_new_game()
# hide the title screen
self.title_screen.hidden = True # pylint: disable=attribute-defined-outside-init
self.title_screen.hidden = True
# set the current state to open play
self.cur_state = STATE_PLAYING_OPEN

Expand Down Expand Up @@ -727,6 +733,7 @@ class Match3TitleScreen(Group):

def __init__(self, display_size):
super().__init__()
self.hidden = False
self.display_size = display_size
# background bitmap color
bg_bmp = Bitmap(display_size[0] // 10, display_size[1] // 10, 1)
Expand Down
29 changes: 21 additions & 8 deletions Metro/Metro_RP2350_Match3/tilepalettemapper_demo/code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import sys

import supervisor
from displayio import Group, OnDiskBitmap, TileGrid
Expand All @@ -20,16 +21,28 @@
spritesheet_bmp = OnDiskBitmap("match3_cards_spritesheet.bmp")

# create a TilePaletteMapper
tile_palette_mapper = TilePaletteMapper(
spritesheet_bmp.pixel_shader, # input pixel_shader
5, # input color count
3, # grid width
1 # grid height
)
if sys.implementation.version[0] == 9:
tile_palette_mapper = TilePaletteMapper(
spritesheet_bmp.pixel_shader, # input pixel_shader
5, # input color count
3, # grid width
1, # grid height
)
elif sys.implementation.version[0] >= 10:
tile_palette_mapper = TilePaletteMapper(
spritesheet_bmp.pixel_shader, # input pixel_shader
5, # input color count
)

# create a TileGrid to show some cards
cards_tilegrid = TileGrid(spritesheet_bmp, pixel_shader=tile_palette_mapper,
width=3, height=1, tile_width=24, tile_height=32)
cards_tilegrid = TileGrid(
spritesheet_bmp,
pixel_shader=tile_palette_mapper,
width=3,
height=1,
tile_width=24,
tile_height=32,
)

# set each tile in the grid to a different sprite index
cards_tilegrid[0, 0] = 10
Expand Down