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

Re: [pygame] question about Rect objects



Hi, Alex. Welcome to a fun world.

1. Resizing. See the docs for Transform. You will need to get the new re-sized rect from the image afterward.

2. Keyboard. The docs for Event describe how to handle events. Pygame makes it extremely easy. The docs don't seem to adequately explain how to check modifiers (shift, alt, etc.); there are some great comments at the top of the page that dig deeper, and list the modifiers. To test modifiers you'll do something like "e.mod & KMOD_SHIFT". You can catch multiple modifiers by using logical-or on the KMOD_*.

3. Clicking on squares. By default Pygame queues mouse-click events, which include attributes for button and coordinates. See the docs for Event. If you're using Sprites you can test for collisions quite easily.

4. Once you climb the very mild learning curve of sprites, you will find that using them is a lot easier than doing without. Handling sprites in groups is also very convenient. Sprites must have an image attribute; and must have a rect attribute in order to be drawn by its containing Group, for a number of collision-detection routines, and for other potential reasons.

5. Rects. Rects are a geometric abstraction not to be confused with an image. A rect defines merely the geometry of an image or other "thing". Sprites and rects work hand in hand, and are used even for non-drawing purposes, because Rect is a very slick and wieldy Pygame feature. Examples of frequent use: defining the screen dimensions; positioning an image or sprite on the screen; rendering a small part of a large image; detecting collisions; virtual spaces in game models. Rects: learn em, love em.

Note: Changing a sprite's image. In quick summary and just to be clear: my_sprite.image = new_image; my_sprite.rect = self.rect.get_rect(); my_sprite.rect.center = my_pos.

Please read the very nice tutorials on pygame.org. They will lead you through all this in an orderly fashion, save you the struggle of learning the basics from an API reference, and likely answer any questions that you may still have. While you are doing this, and any time afterward, please feel free to ask questions of the mailing list. :)

Gumm

On Tue, May 18, 2010 at 6:13 AM, Alex Hall <mehgcap@xxxxxxxxx> wrote:
Interesting idea. Is there a way of resizing an image? For example, if
the user has a 10x10 grid, each square will be bigger than on a 15x15
grid. Also, I will need to catch keyboard keys (mostly ctrl-m, space,
enter, and space/enter with modifiers) as well as mouse clicks
(differentiating between left and right) for each square. So maybe you
left click a square, or move to it and press ctrl-m. Can Pygame detect
where on the grid that is with some sort of onImageClick type method,
or do I have to track coordinates?
Looks like I have to do a lot more reading on sprites. I heard the
concept and coding is more difficult than basic Pygame. Oh, you
mentioned using rects even with sprites; I am not sure why, then, I
would not just use colored rects in the first place, maybe define a
blue, red, and white rect and then just overwrite the current rect
with the appropriate color by changing the new rect's left/top
arguments? Again, I am quite new to this; I have not even done it in
another language as I have done with other things, so I am coming at
it with no reference. Thanks!