[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re: Question regarding a Rect()-object



That's interesting, I never really thought about how Rects work, but this does seem odd. I would have thought something like this would do the trick:

r = Rect(0, 0, 0, 0)
r.left = 10
r.top = 10
r.right = 50
r.bottom = 50
r.width
0

but obviously it does not.

But to do what you seem to be asking, couldn't you get the other surfaces' bounding box using surface.get_rect to get the bounds and then modify the new rect. It does look like once the width is set into a Rect object it behaves the way you would expect:

r = Rect(0, 0, 100, 100)
r.left = 10
r.top = 10
r
<rect(10, 10, 100, 100)>

Hope this helps in any way.

Cheers,
Taylor

----- Original Message ----- From: "Rikard Bosnjakovic" <rikard.bosnjakovic@xxxxxxxxx>
To: <pygame-users@xxxxxxxx>
Sent: Wednesday, June 28, 2006 10:49 PM
Subject: [personal] [pygame] Question regarding a Rect()-object



I've just learned how to use a Rect() and find them extremely useful.

However, I just stumbled into a problem that confused me a lot. It's
about defining a rect's bounds without using width and height.

Consider this:

a = Rect(0, 0, 0, 0)
a.x, a.y = 10, 10
a.height, a.width
(0, 0)

So far so good, a rect at (10,10) with no width, height. If the width
and height is set, the rect's right and bottom-property are also set
correctly:

a.width = a.height = 50
a
<rect(10, 10, 50, 50)>
a.right, a.bottom
(60, 60)

What I currently want to do is to ignore setting width/height, but set
x/y/right/bottom instead. However, doing that results in this:

a.x = a.y = 20
a.right, a.bottom = 100, 100
a.x, a.y, a.height, a.width
(100, 100, 0, 0)

Since width/height is not defined, x and y are moved to left and
bottom respectively. While thinking about it, it's surely logical in
one way, but not in the way I'm trying to do.

So, to conclude: is there a way to define a Rect() by using the
bounding points instead of filling in height/width (letting the
Rect-object do it by itself), or am I forced to calculate width/height
on my own?

The reason I'm asking is because in my game, a lot of surfaces relies
on other surfaces' (bounding box-)positions, and when creating a new
surface, it's bounding box is much more of importance than its
width/height.

--
Rikard.