

At this point, you should notice that your cursor changes to the Move cursor, but you still can’t move anything. That is, you should store the width and height of the window for later retrieval. Then right-click on the taskbar and choose Move. It is still better, however, to avoid calling this every time you need to know the boundaries of the player.

Note that a useful tool for you to get the size of the Pygame window is _surface().get_size() which will give you a tuple of the width and height. Self.y = max(0, min(newY, SCREEN_HEIGHT)) Self.x = max(0, min(newX, SCREEN_WIDTH)) # you handle where to store screen width Player.move(player.getSpeed(),0) # giving the x and y displacements # I assume you might be doing something like this
Print window off screen code#
If you know the size of the screen, and the displacement of all of the objects on the screen (only applicable if your game has a scrolling playfield or camera), then you can do something like this: # In the lines of code where you have the player move around When you used, you didn't actually create a "physical" boundary- you just set the colour of the pixels on the screen in a rectangular shape. Player.clamp_ip(screen_rect) # ensure player is inside screen Here's a simple example where you can't move the black rect outside the screen: import pygame Same as the Rect.clamp() method, but operates in place. Moves the rectangle inside another, in place If you simply want to stop the movement on the edges on the screen, an easy solution is to use clamp_ip(): Returns true when the argument is completely inside the Rect. If you want to check if a Rect is inside another one, use contains(): If not character.get_rect() in screen.get_rect():
