How to Use the Pygame Rect

19,706

Solution 1

From Making Games With Python and Pygame:

myRect.left The int value of the X-coordinate of the left side of the rectangle.

myRect.right The int value of the X-coordinate of the right side of the rectangle.

myRect.top The int value of the Y-coordinate of the top side of the rectangle.

myRect.bottom The int value of the Y-coordinate of the bottom side.

Because all of these attributes return integers, that's probably why your code isn't working.

Also, if your goal with ballSnapLeft() is to move the ball to a position away from the player, ballRect.right = playerRect.left - distance would only change the X coordinate of the rect. To make the ball also move in the Y coordinate you could do something like

def ballSnapTop():
    ballRect.top = playerRect.bottom - distance

Solution 2

Are you getting an error when you execute ballRect.right = playerRect.left - (0, 1)?

ballRect.right and ballRect.left, along with the related top, bottom, width, height values, are int types and can't have tuples added or subtracted from them.

You might want to take a look at the pygame.Rect documentation, and consider using pygame.Rect.move(x,y) which will shift the coordinates of the rectangle for you.

It's also worth noting that if you change, for example, myRect.topleft, then the corresponding top, left, bottom, etc... values will change as well so that the rect translates and preserves its size.

Share:
19,706
Pip
Author by

Pip

Hey! I'm a hobby game developer, currently working on an experimental C# game framework called Flare, which aims to automate the tasks that I and others commonly do for every project, such as opening a window or getting input in a platform-independent way. Flare provides OpenGL 3+ core bindings, but not compatibility bindings so supports OpenGL 3.0 and upwards, with the default context being 3.2 core. It also provides an optional XNA-like sprite rendering system as well as a 2D text rendering system. I have experience with programming languages including C#, C/C++, Ruby, Java, and Python, and a passing acquaintance with several others. I also have experience with technologies including XNA/MonoGame, Unity, Ruby on Rails, FRC/FTC robotics libraries (WPILib and the FTC SDK), as well as the afore-mentioned OpenGL and some mobile development. I currently attend high school in the United States. Links Github: WardBenjamin Twitter: TheProgramm3r Website: Benjamin Ward

Updated on June 04, 2022

Comments

  • Pip
    Pip almost 2 years

    I have been experimenting with Pygame, and have come across a problem that I could not find the answer to. In this paste, my basic game framework is exhibited. How can i complete my ballSnapLeft() definition correctly?

    Edit: I am not looking for my code to be completed, but I am looking for someone to explain how the 'Rect' class(?) works, and how it could be applied.

    Edit2: I have tried to use the x and y coordinates to do so, but I think there is a simpler way that can actually work, instead of using brute coordinates.