In order to speed up the game, I ended up only redrawing the display where necessary. This meant keeping track of several screen locations. To do this, I wrote a special data buffer class. It works similar to a dictionary with a few changes. It keeps track of a default set of data and allows resetting either all of the data or selected keys to the default value, which makes updating certain area or resetting the level much easier.
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries # # SPDX-License-Identifier: MIT class DataBuffer: def __init__(self): self._dataset = {} self._default_data = {} def set_data_structure(self, data_structure): self._default_data = data_structure self.reset() def reset(self, field=None): # Copy the default data to the dataset if field is not None: if not isinstance(field, (tuple, list)): field = [field] for item in field: self._dataset[item] = self.deepcopy(self._default_data[item]) else: self._dataset = self.deepcopy(self._default_data) def deepcopy(self, data): # Iterate through the data and copy each element new_data = {} if isinstance(data, (dict)): for key, value in data.items(): if isinstance(value, (dict, list)): new_data[key] = self.deepcopy(value) else: new_data[key] = value elif isinstance(data, (list)): for idx, item in enumerate(data): if isinstance(item, (dict, list)): new_data[idx] = self.deepcopy(item) else: new_data[idx] = item return new_data @property def dataset(self): return self._dataset
One of the places that partial updating excels is when there are a lot of the same tiles on the screen. This is because as the viewport changes, the tile that needs to be displayed may not actually need to be redrawn such as with empty floor tiles. If the game were using displayio alone, the entire viewport would be redrawn because it only knows that a new values were written in a location and marks the areas as dirty.
However, when the game has a lot of different tiles, you may notice more lag because it has to redraw more tiles in the viewport as you move around. However, with the game as optimized as it is, the increased lag is barely perceptible.
Another strategy that was used was using displayio for the dialogs. By keeping all of the dialogs in a separate layer and everything else in either of the 2 main areas of the game (the tiles or the info box), the background is rarely redrawn for the entirety of the gameplay.
Page last edited April 09, 2025
Text editor powered by tinymce.