ByteSprites Package#

Submodules#

ByteSprite Class#

class visualizer.bytesprites.bytesprite.ByteSprite(screen: ~pygame.surface.Surface, filename: str, num_of_states: int, object_type: int, update_function: ~typing.Callable[[dict, int, ~game.utils.vector.Vector, list[list[pygame.surface.Surface]]], list[pygame.surface.Surface]], colorkey: ~pygame.color.Color | None = None, top_left: ~game.utils.vector.Vector = <game.utils.vector.Vector object>)[source]#

Bases: Sprite

ByteSprite Class Notes:

PyGame Notes#

Here are listed definitions of the PyGame objects that are used in this file:
PyGame.Rect:

“An object for storing rectangular coordinates.” This is used to help position things on the screen.

PyGame.Surface:

“An object for representing images.” This is used mostly for getting the screen and individual images in a spritesheet.

Class Variables#

Active Sheet:

The active_sheet is the list of images (sprites) that is currently being used. In other words, it’s a strip of sprites that will be used.

Spritesheets:

This is a 2D array of sprites. For example, refer to the ExampleSpritesheet.png file. The entirety of the 4x4 would be a spritesheet. One row of it would be used as an active sheet.

Object Type:

This is an int that represents the enum value of the Object the sprite represents. For example, the ExampleSpritesheet.png shows the Avatar. The Avatar object_type’s enum value is found in the JSON logs and is the number 4. This would change if the order of the ObjectType enum changes, so be mindful of that and refer to the JSON logs for the exact values.

Rect:

The rect is an object used for rectangular objects. You can offset the top left corner of the Rect by passing parameters.

Example:

On the left, the Rect’s offset is depicted as being at (0, 0), meaning there is no offset. A Rect object has parameters that will determine the offset by passing in (x, y). The ‘x’ is the offset from the left side, and the ‘y’ is the offset from the top. Therefore, passing in an (x, y) of (3, 2) in a Rect object will move the object 3 units to the right, and 2 units down.

In the visual below, the left side shows a Rect at (0, 0) (i.e., no offset). The image on the right depicts the Rect object further to the right, showing its offset from the left corner of the screen.

Rect Example:

-----------------------                 -----------------------
|------               |                 |     ------          |
||    |               |                 |     |    |          |
|______               |    -------->    |     ______          |
|                     |                 |                     |
|                     |                 |                     |
|                     |                 |                     |
-----------------------                 -----------------------
Screen:

The screen is also a PyGame.Screen object, so it simply represents an image of the screen itself.

Image:

The image is an individual sprite in a spritesheet.

Frame Index:

The frame index is an int that is used to determine which sprite to use from the active_sheet. For example, say the active_sheet is the first row in the ExampleSpritesheet.png. If the frame_index is 1, the first image will be used where the head is centered. If the frame_index is 3, the sprite will now have the head of the Avatar in a different position than in frame_index 1.

Config:

This is an object reference to the config.py file. It’s used to access the fixed values that are only accessed in the configurations of the file.

Update Function:

The update function is a method that is assigned during instantiation of the ByteSprite. That function is used to update what the active_sheet is depending on what is implemented in ByteSprite classes.

Examine the exampleBS.py file. In that implementation of the update method, it selects the active_sheet based on a chain of if statements. Next, in the create_bytesprite method, the implemented update method is passed into the returned ByteSprite object.

Now, in the ByteSprite object’s update method, it will set the active_sheet to be based on what is returned from the BytespriteFactory’s method.

To recap, first, a ByteSprite’s update function depends on the BytespriteFactory’s implementation. Then, the BytespriteFactory’s implementation will return which sprite_sheet is supposed to be used. Finally, the ByteSprite’s update function will take what is returned from the BytespriteFactory’s method and assign the active_sheet to be what is returned. The two work in tandem.

property active_sheet: list[pygame.surface.Surface]#
image: Surface#
property object_type: int#
property rect: Rect#
property screen: Surface#
set_image_and_render()[source]#

This method will take a single image from the current active_sheet and then display it on the screen. :return:

property spritesheets: list[list[pygame.surface.Surface]]#
update(data: dict, layer: int, pos: Vector) None[source]#

This method will start an animation based on the currently set active_sheet. Then, it will reassign the active_sheet based on what the BytespriteFactory’s update method will return. Lastly, the set_image_and_render method is then called to display the new sprites in the active_sheet. :param data: :param layer: :param pos: :return: None

property update_function: Callable[[dict, int, Vector, list[list[pygame.surface.Surface]]], list[pygame.surface.Surface]]#

ByteSprite Factory#

class visualizer.bytesprites.bytesprite_factory.ByteSpriteFactory[source]#

Bases: object

This is a class that every ByteSpriteFactory subclass must inherit. The factory structure allows for these classes to create the ByteSprite objects that will be used for visualization. With the given structure, it forces those classes to have the following methods: update() and create_bytesprite(). These methods can be implemented in many different ways depending on how the sprites should behave during the game. Examples are provided in the example files.

static create_bytesprite(screen: Surface) ByteSprite[source]#

This is a method that must be implemented in every ByteSpriteFactory class. Look at the example files to see how this can be implemented. :param screen: :return:

static update(data: dict, layer: int, pos: Vector, spritesheets: list[list[pygame.surface.Surface]]) list[pygame.surface.Surface][source]#

This is a method that must be implemented in every ByteSpriteFactory class. Look at the example files to see how this could be implemented. Implementation may vary. :param data: :param layer: :param pos: :param spritesheets: :return: list[pyg.Surface]

Example BS Class#

class visualizer.bytesprites.exampleBS.AvatarBytespriteFactoryExample[source]#

Bases: ByteSpriteFactory

Avatar Bytesprite Factory Example Notes:

This is a factory class that will produce Bytesprite objects of the Avatar.

static create_bytesprite(screen: Surface) ByteSprite[source]#

This file will return a new ByteSprite object that is to be displayed on the screen. :param screen: ByteSprite :return:

static update(data: dict, layer: int, pos: Vector, spritesheets: list[list[pygame.surface.Surface]]) list[pygame.surface.Surface][source]#

This method will select which spritesheet to select from the ExampleSpritesheet.png file. For example, the first if statement will return the second row of sprites in the image if conditions are met. :param data: :param layer: :param pos: :param spritesheets: :return: list[pyg.Surface]

Example Tile BS Class#

class visualizer.bytesprites.exampleTileBS.TileBytespriteFactoryExample[source]#

Bases: ByteSpriteFactory

This class is used to demonstrate an example of the Tile Bytesprite. It demonstrates how any class inheriting from ByteSpriteFactory must implement the update() and create_bytesprite() static methods. These methods may have unique implementations based on how the sprites are meant to look and interact with other objects in the game.

static create_bytesprite(screen: Surface) ByteSprite[source]#

This method takes a screen from Pygame.Surface. That screen is then passed in as a parameter into the returned Bytesprite object. :param screen: :return:

static update(data: dict, layer: int, pos: Vector, spritesheets: list[list[pygame.surface.Surface]]) list[pygame.surface.Surface][source]#

This implementation of the update method is different from the exampleWallBS.py file. In this method, the data dictionary is used. The data is a dict representing a Tile object in JSON notation.

For this unique implementation, an if statement is used to check if something is occupying the Tile object. If true, the second spritesheet is used. If false, the first spritesheet is used.

Examining the ExampleTileSS.png, it is apparent that the first spritesheet shows a Tile with an animation with only the pink color. However, the second spritesheet (the one used if something occupies that tile) has a unique animation that is blue instead. :param data: :param layer: :param pos: :param spritesheets: :return:

Example Wall BS Class#

class visualizer.bytesprites.exampleWallBS.WallBytespriteFactoryExample[source]#

Bases: ByteSpriteFactory

This class is used to demonstrate an example of the Wall Bytesprite. It demonstrates how any class inheriting from ByteSpriteFactory must implement the update() and create_bytesprite() static methods. These methods may have unique implementations based on how the sprites are meant to look and interact with other objects in the game.

static create_bytesprite(screen: Surface) ByteSprite[source]#

This method takes a screen from Pygame.Surface. That screen is then passed in as a parameter into the returned Bytesprite object. :param screen: :return: a ByteSprite object

static update(data: dict, layer: int, pos: Vector, spritesheets: list[list[pygame.surface.Surface]]) list[pygame.surface.Surface][source]#

This method implementation simply returns the first spritesheet in the list of given spritesheets. Examining the ExampleWallSS.png file, it is clear that there is only one spritesheet, so that is all this method needs to do. :param data: :param layer: :param pos: :param spritesheets: :return:

Module contents#