Pygame Sprite Tutorial

Pygame Sprite Tutorial 4,0/5 9264 reviews
  1. Python Sprites
  2. How To Use Pygame
  3. Pygame Sprite Sheet Tutorial

Several of the pygame examples (like 'chimp' and 'aliens') have been updated to use the sprite module. You may want to look into those first to see what this sprite module is all about. The chimp module even has it's own line-by-line tutorial, which may help get more an understanding of programming with python and pygame. Jul 09, 2015  In this second tutorial on how to create a retro arcade game using PyGame we are looking at creating our first sprite. Consider a sprite as an object. An object can have different properties (e.g. Width, height, colour, etc.) and methods (e.g. Jump, hide, moveForward, etc.). Like in the industry an object is built from a mould. After you have read this document, you should understand the core concepts of Pygame. There are a multitude of tutorials available that will teach you Pygame in a step-by-step manner. I’ve hand-picked three of my favorites: PyGame Tutorials - Simple Introduction to Game Programming: Covers the basic features of Pygame. You may wish to be familiar with the basics of the Python 3 programming language, or at least the basics of Programming in general That said, this course begins at a slowish pace, and I do my best to explain everything at least the first time it shows up. PyGame can actually be a wonderful starting.

Active2 years, 3 months ago

I've been searching for some good tutorial about making simple sprite animation from few images in Python using Pygame. I still haven't found what I'm looking for.

My question is simple: how to make an animated sprite from few images (for an example: making few images of explosion with dimensions 20x20px to be as one but animated)

Mar 21, 2019  Download here for PC; Download here for Mac; Click Download on the Origin download page or click one of the links above to start downloading Origin. When it’s done downloading, follow the instructions in the installation wizard to install the latest version of Origin. If you don’t have an EA Account yet, you can find out how to create one here. Star wars battlefront ea pc download. Explore video games for PC Download from Electronic Arts, a leading publisher of games for the PC, consoles and mobile. Explore video games for PC Download from Electronic Arts, a leading publisher of games for the PC, consoles and mobile. News Madden NFL. Looks like your computer is running a operating system we no longer support, but you can still download and play your games using an older version of Origin. For Windows XP or Vista, click here to download. For Mac OSX 10.7 or 10.8, click here to download.

Any good ideas?

Ruslan Osipov
4,5112 gold badges22 silver badges41 bronze badges
lbartoliclbartolic
5681 gold badge7 silver badges20 bronze badges

3 Answers

You could try modifying your sprite so that it swaps out its image for a different one inside update. That way, when the sprite is rendered, it'll look animated.

Edit:

Here's a quick example I drew up:

It assumes that you have two images called image1.png and image2.png inside the same folder the code is in.

Michael0x2aMichael0x2a
26.8k17 gold badges81 silver badges134 bronze badges

Python Sprites

There are two types of animation: frame-dependent and time-dependent. Both work in similar fashion.

Before the main loop

  1. Load all images into a list.
  2. Create three variable:
    1. index, that keeps track on the current index of the image list.
    2. current_time or current_frame that keeps track on the current time or current frame since last the index switched.
    3. animation_time or animation_frames that define how many seconds or frames should pass before switching image.

During the main loop

  1. Increment current_time by the amount of seconds that has passed since we last incremented it, or increment current_frame by 1.
  2. Check if current_time >= animation_time or current_frame >= animation_frame. If true continue with 3-5.
  3. Reset the current_time = 0 or current_frame = 0.
  4. Increment the index, unless if it'll be equal or greater than the amount of images. In that case, reset index = 0.
  5. Change the sprite's image accordingly.

A full working example

When to chose which

Time-dependent animation allows you to play the animation at the same speed, no matter how slow/fast the frame-rate is or slow/fast your computer is. This allows your program to freely change the framerate without affecting the animation and it'll also be consistent even if the computer cannot keep up with the framerate. If the program lags the animation will catch up to the state it should've been as if no lag had happened.

Although, it might happen that the animation cycle don't synch up with the framerate, making the animation cycle seem irregular. For example, say that we have the frames updating every 0.05 second and the animation switch image every 0.075 second, then the cycle would be:

  1. Frame 1; 0.00 seconds; image 1
  2. Frame 2; 0.05 seconds; image 1
  3. Frame 3; 0.10 seconds; image 2
  4. Frame 4; 0.15 seconds; image 1
  5. Frame 5; 0.20 seconds; image 1
  6. Frame 6; 0.25 seconds; image 2

And so on..

Frame-dependent can look smoother if your computer can handle the framerate consistently. If lag happens it'll pause in its current state and restart when the lag stops, which makes the lag more noticeable. This alternative is slightly easier to implement since you just need to increment current_frame with 1 on each call, instead of dealing with the delta time (dt) and passing it to every object.

Sprites

Result

Ted Klein BergmanTed Klein Bergman
3,9942 gold badges16 silver badges29 bronze badges

You should have all your sprite animations on one big 'canvas', so for 3 20x20 explosion sprite frames you will have 60x20 image. Now you can get right frames by loading an area of the image.

Inside your sprite class, most likely in update method you should have something like this (hardcoded for simplicity, I prefer to have separate class to be responsible for picking the right animation frame). self.f = 0 on __init__.

Ruslan OsipovRuslan Osipov
4,5112 gold badges22 silver badges41 bronze badges

Not the answer you're looking for? Browse other questions tagged pythonanimationspritepygame or ask your own question.

Mon, Aug 15, 2016
Tags: pythontutorialgamedevpygame

This is part 3 of our tutorial series, “Game Development with Pygame”. It is intended for beginner/intermediate programmers who are interested in game development and improving their Python coding skills. You should start with Part 1: Getting Started

You can watch a video version of this lesson here:

Moving to graphical sprites

Colored rectangles are fine - they’re a great way to start and make sure you have the basics of your game working, but sooner or later you’re going to want to use a cool spaceship image or character for your sprite. This leads us to the first issue: where do you get your game graphics.

Where to find art

When you need art for your game, you have 3 choices:

  1. Draw it yourself
  2. Find an artist to draw it for you
  3. Use pre-existing art from the Internet

1 and 2 are fine if you or your friends are artistically inclined, but for most programmers (yours truly included), creating nice-looking art is not in our skill set. So that leaves the Internet, and it’s very important to remember that you shouldn’t use art that you do not have the right to use. While it’s easy enough to search and find a picture of Mario or your favorite Pokemon, that doesn’t mean that it’s OK for you to use it in your game, especially if you plan to put it online and let other people see it.

Fortunately, there’s a good solution: OpenGameArt.org. This website is loaded with tons of art, sound, music, and more - and it’s all generously licensed by the artists for you to use in your games. One of the best artists you can find there is named Kenney (just put his name in the search box).

You can also visit Kenney’s website.

The reason I love to use Kenney’s art (besides the fact that it’s very high quality) is that he likes to release it in packs. This means that you can get sets of art that will all match in style, instead of trying to mix and match images from multiple artists.

For this lesson, we’re going to use Kenney’s Platformer Art Complete Pack, which has lots of graphics for making a platformer-style game. Go ahead and download it and unzip. We’re going to use the image “p1_jump.png”:

You can also just click on the image above to download the player image.

How To Use Pygame

Pygame Sprite Tutorial

Organizing your game assets

First we need a folder to hold our assets, which is a term game developers use to refer to things like art and sound. I called the folder “img” for short, and I put the player image into it.

To use this image in our game, we need to tell Pygame to load the picture file, which means we need our program to know where the file is located. Depending on what kind of computer you are using, this can be different, and we want to be able to run our program on any computer, so we need to load a Python library called os, and then specify where our game is located:

The special Python variable __file__ refers to whatever folder your game code is saved in, and the command os.path.dirname figures out the path to that folder. For example, on my computer, the path to my code is this:

/Users/chris/Documents/gamedev/tutorials/1-3 sprite example.py

But I’m using a computer running OS X. If you’re using Windows, your path might look like this:

C:UserschrisDocumentspythongame.py

As you can see, different operating systems use different ways of describing where things are located on the computer. By using the os.path command, we can let the computer figure out what the right path is (whether to use “/” or “” for example.)

Now, we can specify our “img” folder:

Now we’ve loaded our image by using pygame.image.load() and we’ve made sure to use convert(), which will speed up Pygame’s drawing by converting the image into a format that will be faster to draw on the screen. Now we’re ready to replace the plain green square in our sprite with our fancy player image:

Notice we’ve deleted the self.image.fill(GREEN) commmand - we don’t need it to be filled with a solid color anymore. get_rect() will still work just fine, because it looks at whatever self.image is to figure out what the bounding rectangle should be.

Now if you run the program, you should see a nice little cartoon alien running across the screen. But we have a problem - one we can’t see because the background is currently black. Change the screen.fill() command at the bottom to something else - I decided to use BLUE. Now you can see the issue:

When you have an image file on the computer, that file is always a rectangular grid of pixels. No matter what shape you’ve drawn, there’s still a border of pixels filling the “background” of your image. What we need to do is tell Pygame to ignore the pixels in the image that we don’t care about. In this image, those pixels happen to be black, so we can add the following:

Get ongoing updates to the Windows 7 platform. Easily deploy cumulative updates at a single time. Keep your PCs supported and up-to-date. Windows 7 sp1 64 bit download. This enables organizations to deploy a single set of updates.Windows 7 and Windows Server 2008 R2 SP1 will help you:.

set_colorkey() just tells Pygame that when we draw the image we want to ignore any pixels of the specified color. Now our image looks much better:

Pygame Sprite Sheet Tutorial

Congratulations, you have now learned the basics of working with Pygame! Now it’s time to start making a real game. We have created a few tutorials showing the process of creating a full game from start to finish. They get more complex as we go, so it’s recommended that you follow them in order.

Click here for the full list of tutorials.

Helpful Links:

Comments

Please enable JavaScript to view the comments powered by Disqus.