A quick video of our final program is below, and at the bottom of the page is a video explanation of the program.
. A good 2D game can involve a lot of graphics. Rather than create and manage a file for each image, games will use a large image made up of several smaller images. For example, this sprite sheet has multiple frames of a player character walking all in one image:

The sprite sheet was downloaded from the ``Platformer Art Deluxe'' package at OpenGameArt.org and was created by author Kenney. Here is another sprite sheet that our game uses to build the world:
Pygame Sprite Animation Tutorial
In addition, I've created a couple backdrops for my game. You can't interact with the backdrops, but I think it is more interesting to have a backdrop than just have a solid color. The backdrop was created using the Tiled Map Editor and Kenney's Buildings sprite sheet.
Finally, the code! This code builds off the other platformer examples on this website. I've divided the code up into several files to make it easier to navigate.
The first file just has some variables that represent constant values. It makes the code easier to read by using variables like
Using Pygame To Move Your Game Character Around
This class pulls smaller images out of the large sprite sheet. Create an instance of the class and pass in the file name as a parameter to the constructor. Then call
With the x, y location of the upper left corner of yorur sprite along with its height and width. You can use a drawing program to get the location of the sprite images you are intersted in.
This next file defines platforms we can jump on or run into. At the beginning of the file we create lists for each image we want to use. The lists contain the x, y location of the sprite, along with the width and height. Next is the
Sprites That Follow The Player 2
Class that defines a non-moving platform. There isn't much to it. The class passes along the image x, y, width, and height and uses the SpriteSheet class to grab the image.
. The class keeps track of its boundaries that it stays in, along with the velocity. The update is complex because a moving platform can 'bump' the user and move him/her. It also needs to keep track of how far the world has scrolled to the left or right.
Class that all levels should inherit from. Things that all levels have (like a list of platforms) are defined in this parent
Pygame — How To Think Like A Computer Scientist: Learning With Python 3
The player class. This class could be simple, but we will make this player have an animation as he/she moves left and right.When we built Alien Invasion, we only needed two images: one for the ship, and one for the alien. In some games, however, you’ll need lots of different images.
Game developers realized a long time ago that loading many images from separate files causes a game to run really slowly, so people came up with the idea of sprite sheets. A

Is a single file that contains many smaller images, all on a plain or transparent background. To use a sprite sheet, you load the sprite sheet as a single large image, and then you load the individual images from the sprite sheet image. This turns out to be much more efficient than loading a bunch of separate image files.
Pygame: How To Control Your Sprite?
For this example we’ll look at how you can load a full set of chess pieces from one sprite sheet. To build a chess game, you need 12 pieces: a black and white king, queen, rook, bishop, knight, and pawn. Instead of loading 12 separate images, we’ll load one image that contains an icon for each of these pieces. We’ll then create 12 separate objects representing each of these pieces. What you learn in this guide will be useful any time you want to load a number of icons from a single image file.
I downloaded this image from Public Domain Clip Art, and converted it to a .bmp file. You can right click and save the image file from this page, or you can find it in the beyond_pcc folder when you download the resources for the book.
We’re not going to make a chess game in this tutorial, but we’ll set up this project so you could continue to expand on it and start building a game if you want to. So let’s start with a simple Pygame file, just like we did for Alien Invasion. Make a folder called
How To Create Walking Character Using Multiple Images From Sprite Sheet Using Pygame?
This gives us an empty game window, and we can press ‘q’ to quit the game at any time. I like to have this option because sometimes I run games in fullscreen mode, and you can’t click the close button in fullscreen mode.
A chess set is made up of a number of pieces. Let’s think about the pieces first. Each piece needs a name and a color, and an image. We need to be able to draw each piece to the screen. In a fully implemented game we might add attributes such as
![]()
, which will contain a class for representing pieces and a class for representing the set as a whole. Here’s the start of that file, with the
Pygame Sprite Animation Update
Class allows us to assign each piece an image, a name, and a color. Each piece will start off at the top left corner, but we can move it wherever it needs to go. The only argument needed to create a piece initially is a reference to the overall game object. When we’re ready to draw a piece to the screen, we can do so by calling
Now we can start to model the set as a whole. The set will handle the task of creating all the pieces. In a fuller implementation, it might also track attributes such as the overall strength of the player’s remaining pieces, which can be useful in developing playing strategies.
Method that accepts the overall game object, and we want to call a helper method that builds the pieces that make up the set. Add this code to
Modular Sprite Sheets: Pygame
Method which accepts a reference to the overall game object, and we have an attribute for storing the pieces in the set. We also call
When loading images from a sprite sheet, it’s helpful to start with a library if possible. If you search something like “pygame sprite sheet”, one of the top results is from the Pygame wiki. Here’s a cleaned-up version of the code featured there:

Class, passing it the location of our sprite sheet file. We’ll figure out which rectangular portion of the sprite sheet we want to load - the coordinates of its top left corner, and the width and height of the region we want to load. We’ll call
D Platform Game With Pygame
, you can see that the left edge of the black king is about 68 pixels from the edge of the image, and the top of the king is about 70 pixels from the top edge of the image. These will be the first two values we pass to
There are many ways we could have done this. You can create the piece first, and then load the image, or you can load the image and assign it to the piece in one line. I’m doing it the way you see here because in a little bit I’m going to show you how to load all the images at once, and then write a loop that creates the pieces all at once as well.
We’ve pretty much got the image we want. We might want to go back and refine the rectangle we used for pulling this image, to even out the amount of background on each of the margins.
These Are All Information I Have Right Now. Here
Don’t be surprised if you see a much different area of the sprite sheet than you were expecting when you run your own code. It can take a bit of practice to understand how to choose the right rectangle coordinates, and even with practice it’s easy to make a mistake that grabs the wrong part of the sprite sheet. If you see a black rectangle, it’s possible you asked for a portion of the sprite sheet that doesn’t exist.
As mentioned earlier, it’s possible to load all of the images we need from the spritesheet at once, and then assign each one to the appropriate piece. This can be much easier than figuring out the coordinates by hand for each individual piece, especially if you’re working with multiple sprite sheets.
![]()
. The dark green bar shows the margin above the first row, and the light green bar shows the vertical padding between each row.
Sprite Sheet Tutorial
These spacings allow us to work out a pattern for where each image should be grabbed. For example the left position of the black king is equal to the width of the horizontal margin. The left position of the black queen is equal to the width of the horizontal margin, plus the width of a piece, plus the width of one strip of padding. For the third icon in the first row, the horizontal position is one margin width, plus two padding widths, plus two icon widths.
0 Comments
Posting Komentar