Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Staying on the Screen

In this lesson, you’ll add code to keep the player sprite from moving off the screen. After adding the code from the previous lesson, you may notice two small problems:

  1. The player rectangle can move very fast if a key is held down. You’ll work on that later.
  2. The player rectangle can move off the screen. Let’s solve that one now.

To keep the player on the screen, you need to add some logic to detect if the rect is going to move off screen. To do that, you check whether the rect coordinates have moved beyond the screen’s boundary. If so, then you instruct the program to move it back to the edge:

Python
29# Move the sprite based on user keypresses
30def update(self, pressed_keys):
31    if pressed_keys[K_UP]:
32        self.rect.move_ip(0, -5)
33    if pressed_keys[K_DOWN]:
34        self.rect.move_ip(0, 5)
35    if pressed_keys[K_LEFT]:
36        self.rect.move_ip(-5, 0)
37    if pressed_keys[K_RIGHT]:
38        self.rect.move_ip(5, 0)
39
40    # Keep player on the screen
41    if self.rect.left < 0:
42        self.rect.left = 0
43    if self.rect.right > SCREEN_WIDTH:
44        self.rect.right = SCREEN_WIDTH
45    if self.rect.top <= 0:
46        self.rect.top = 0
47    if self.rect.bottom >= SCREEN_HEIGHT:
48        self.rect.bottom = SCREEN_HEIGHT

00:00 In this lesson, we’ll learn how to fix the issue of staying on the screen. So, you may have noticed a couple small problems with your game at the moment, the first of which is the player rectangle can move off the screen.

00:15 The other isn’t a problem I’m having in demonstrating it on this Macintosh computer I have, which I just learned why. But on your machine, the player can move very fast, if a key is held down, and we’ll fix that problem a little bit later.

00:29 I guess the reason on the Mac that it does this is because of the Retina display doubling the pixels. I’ll try to show you some examples of the code running much faster on an external display on my machine, but that is an issue with pygame, I guess, updating on the Retina style displays on Macintosh OS.

00:45 So, let’s fix the first problem and make it so that the player rectangle can’t move off the screen. To fix this issue of staying on the screen, it has to do with the updating.

00:56 So, not when you’re calling it here with player.update(), but the actual method itself, back up here. So, as part of this updating and moving in place, you’re going to need to have it check that it isn’t moving it off screen.

01:11 So to do that…

01:17 if the left side of the rectangle is less than 0, set it to 0, keeping it on the screen. That won’t let it move off the left side.

01:27 Now you need to do that on each side. For the right side—the right edge—if it’s greater than SCREEN_WIDTH, set it equal to SCREEN_WIDTH.

01:36 I need a colon there. Again, that’s the maximum the right side would be, where left would be 0. And the same for up and down. In this case, it’s .top.

01:51 This time, do less than or equal to 0, set it to the .top, which is 0. And then—you guessed it—.bottom.

02:02 In this case, if the .bottom is greater than or equal to SCREEN_HEIGHT, reset the .bottom to SCREEN_HEIGHT. Great!

02:11 So, you added these lines here on line 40 to 48. Again, it’s all still part of the Player class, and in this case, it’s actually part of this updating method.

02:26 Let’s have a look at it. Go ahead and save.

02:33 Okay. Now, moving hard left stops. Bottom stops. Top. Yep. Looks like it’s keeping the player on the screen.

02:49 Good! All right, it’s time for some enemies. You’ll start setting them up in the next lesson.

Avatar image for KA

KA on May 17, 2020

love these videos.

Avatar image for David Alvarez

David Alvarez on July 25, 2020

I kind of compressed the conditionals this way.

        if self.rect.bottom < SCREEN_HEIGHT and keys_pressed[K_DOWN]:
            self.rect.move_ip(0, 4)

        if self.rect.top > 0 and keys_pressed[K_UP]:
            self.rect.move_ip(0, -4)

        if self.rect.left > 0 and keys_pressed[K_LEFT]:
            self.rect.move_ip(-4, 0)

        if self.rect.right < SCREEN_WIDTH and keys_pressed[K_RIGHT]:
            self.rect.move_ip(4, 0)

I don’t know if there is any caveat in this implementation but seems to be working fine. Left side in the if expression it’s evaluated first if I’m not wrong.

Become a Member to join the conversation.