Creating Characters with Classes
Characters that appear in a game are commonly referred to as sprites.
In this chapter, we will use classes to prepare a common base class for sprites.
You might feel that “classes sound difficult…” at first,
but for now, the goal is simply to treat a character as a single reusable component.
If you want to review classes beforehand, checking
Chapter 12: Let’s Use Classes
will make things smoother.
The complete code is shown at the end of this article.
1. Prepare the Sprite Module
Create a new file named sprite.py and place it in your working folder.
The folder structure should look like this:
# Folder structure
working_folder/
├ main.py
├ sprite.py # sprite module
└ shooter.pyxres
2. Create a Common Sprite Base Class
In the sprite.py file, define a class named BaseSprite.
This class will serve as the common base class for all sprites.
# sprite.py (excerpt)
import pyxel
import math
import random
class BaseSprite:
def __init__(self, x, y, w=8, h=8):
""" Constructor """
self.x = x # X coordinate
self.y = y # Y coordinate
self.w = w # Image width
self.h = h # Image height
def update(self):
""" Update logic """
pass
def draw(self):
""" Drawing logic (implemented in subclasses) """
pass
3. Create the Player Class
Next, add a ShipSprite class to sprite.py.
This class inherits from BaseSprite and will be used to create the player character.
# sprite.py (excerpt)
# omitted
class ShipSprite(BaseSprite):
def __init__(self, x, y):
""" Constructor """
super().__init__(x, y)
def draw(self):
""" Drawing logic """
pyxel.blt(
self.x, self.y, 0,
0, 0,
self.w, self.h, 0
) # Ship
4. Use the Player Class
Now let’s use the player class inside the Game class in main.py.
We will do this in the following three steps.
4-1. Initialize the Player
Create a player object in the constructor of the Game class.
# main.py (excerpt)
# Initialize the player (placed near the bottom center of the screen)
self.ship = sprite.ShipSprite(W / 2, H - 40)
4-2. Update the Player
Update the player object in the update() method of the Game class.
(In this chapter, the goal is to build the structure, so the player will not move yet.)
# main.py (excerpt)
# Update the player
self.ship.update()
4-3. Draw the Player
Draw the player object in the draw() method of the Game class.
# main.py (excerpt)
# Draw the player
self.ship.draw()
Complete Code
Below is the complete code with all features implemented so far.
# sprite.py
import pyxel
import math
import random
class BaseSprite:
def __init__(self, x, y, w=8, h=8):
""" Constructor """
self.x = x # X coordinate
self.y = y # Y coordinate
self.w = w # Image width
self.h = h # Image height
self.vx = 0 # Velocity (X direction)
self.vy = 0 # Velocity (Y direction)
def update(self):
""" Update logic """
self.x += self.vx # Move in X direction
self.y += self.vy # Move in Y direction
def draw(self):
""" Drawing logic (implemented in subclasses) """
pass
class ShipSprite(BaseSprite):
def __init__(self, x, y):
""" Constructor """
super().__init__(x, y)
def draw(self):
""" Drawing logic """
pyxel.blt(
self.x, self.y, 0,
0, 0,
self.w, self.h, 0
) # Ship
# main.py
import pyxel
import math
import random
import sprite # Import the sprite module
W, H = 160, 120
# Game
class Game:
def __init__(self):
""" Constructor """
# Initialize score
self.score = 0
# Initialize player
self.ship = sprite.ShipSprite(W / 2, H - 40)
# Start Pyxel
pyxel.init(W, H, title="Hello, Pyxel!!")
pyxel.load("shooter.pyxres")
pyxel.run(self.update, self.draw)
def update(self):
""" Update logic """
# Update player
self.ship.update()
def draw(self):
""" Drawing logic """
pyxel.cls(0)
# Draw score
pyxel.text(
10, 10,
"SCORE:{:04}".format(self.score),
12
)
# Draw player
self.ship.draw()
def main():
""" Main entry point """
Game()
if __name__ == "__main__":
main()
When you run this program, the result will look like this:
Coming Up Next...
Thank you for reading this chapter.
The next title is “Let’s Move the Character.”
See you next time!!

Top comments (0)