Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Game Speed

In this lesson, you’ll adjust the speed of your game using the pygame module time. Normally, you want as high a frame rate as possible, but for this game, you may need to slow it down a bit for the game to be playable. Fortunately, the module time contains a Clock designed precisely for this purpose.

Using Clock to establish a playable frame rate requires just two lines of code. The first creates a new Clock before the game loop begins:

102# Setup the clock for a decent framerate
103clock = pygame.time.Clock()

The second calls .tick() to inform pygame that the program has reached the end of the frame:

179# Flip everything to the display
182# Ensure program maintains a rate of 30 frames per second

For more information about Clock and .tick(), check out the following resources from the pygame documentation:

00:00 Welcome to Section 4. The first lesson you’re going to cover here is a way to control the game speed inside of PyGame. I have to admit something to you. I’ve been having problems with my computer in the middle of this course, and in the middle of it, I ended up replacing the entire hard drive and putting in a new SSD.

00:18 So, you might have noticed some differences when it switches to show the gameplay, and that’s because the gameplay was playing really poorly on my previous version of my Mac and I could not figure out what the deal was.

00:30 But putting a new hard drive in it and then updating to the latest OS, and now pygame runs completely differently. But this video is from when it was operating again differently, and it had a problem with Retina display where on my display it would actually run much slower, and this actually, I think, makes the most sense to see it in action, the way I had recorded it before.

00:56 So just keep that in mind—that I’m not having that problem with the slower issues on my main display, but you might, and an update to the latest version of PyGame and the latest version of the SDL library solved all of that for me—after I fixed my machine.

01:13 Okay, so this will be from back in time, but I think it demonstrates game speed really well. The speed of games is typically measured as a frame rate. Currently, you’ve seen on my machine that it runs at different frame rates based upon which display I’m showing it on, and you might be seeing very different results on your hardware also.

01:33 So it might be a good thing to be able to control this, or at least set a maximum level to make the game a little more playable. Or you could adjust it depending on your own personal taste.

01:43 The time module that you’ve already used contains a Clock object, pygame.time.Clock, and this Clock object contains a method to limit the maximum frame rate.

01:56 The method is .tick() and it takes in an integer as an argument for the frames per second that you want to set. Let me show you in code how to set this up.

02:06 So in here, you need to set up two things. One, you need to create the Clock object, and the best place to do that is right after you initialize pygame.

02:18 So here, on line 102,

02:24 # Setup the clock for a decent framerate. Create an object named clock, and it’s going to be equal to pygame.time.Clock(), creating a Clock object from that class. Great! Now, again, this is after you’ve just initialized pygame, you’re creating this clock. Then inside of here, here’s your main loop.

02:49 Right after you set the display to .flip() is where you can then adjust how you would like the clock to work as far as the ticks in the clock.

03:00 So here, ensure the program maintains a maximum rate of—in this case, we’ll say 30 frames per second. Now, this is something you can adjust, which is nice. So, there’s the clock object, there’s the method named .tick(), and then here, you’re going to say 30.

03:18 And what it’s going to do is it’s going to calculate the number of milliseconds each frame should take based on the desired frame rate that we want—in this case, 30 frames per second.

03:28 So, it compares that number with the number of milliseconds that have passed so far since the last .tick() was called, and if it’s not enough time, then the .tick() is going to delay processing to ensure that it never exceeds that specified frame rate, which is pretty neat. Go ahead and save.

03:42 I don’t think you’re going to see much of a difference on this display, but I’ll show it. So again, python

03:53 Again, you can see how much slower it runs on this machine anyway, on this display. But what I’ll do is I’ll demonstrate it on the other display that was running much faster before. Okay. Recording on this separate screen now.

04:10 And you may remember how fast it was before. So now, this is at 30 frames per second, which is a decent speed. If I were to alter it, let’s try 60. And save.

04:25 There, you can see how much faster is. You could maybe use it as a difficulty level type of thing.

04:39 I’m going to go ahead and change it to 30, and save, and keep it at that rate.

04:51 Great!

04:56 Now that you have the ability to control the speed, let’s do some more enhancing with a lesson on adding music.

Become a Member to join the conversation.