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

Re: [pygame] question about Rect objects



Hi, Alex.

On Tue, May 18, 2010 at 6:12 PM, Alex Hall <mehgcap@xxxxxxxxx> wrote:
In the meantime,
do you have a place where I can find an example of drawing a grid of
Rects?

I searched for a little bit and could not find any tutorials, or projects or demos that highlighted a grid as a feature. While there are several small games on pygame.org that do tic-tac-toe like grids, I did not find one that appeared simple enough for a first study.

I whipped up a *very* basic demo that does the does the absolute minimum you ask. I didn't want to give too much away to an aspiring student. :) The demo lacks elegance and is not a good Python or Pygame example, but it may provide the small "hello world" boost you need. Hope so. Find it here...
http://code.google.com/p/trollsouttaluckland/downloads/detail?name=grid0.py&can=2&q=

Also, I would still like to put a rect into a boardSquare
object (an object I have defined) so I can call something like
board[i][j].Rect.changeColor(red) where changeColor is a method I
implement. Is this not possible / a huge waste of resources?

What you describe is a common practice, and is generally encouraged. And yes, if you have many, many squares, each with a copy of the same image, it would likely be considered wasteful. As Jason suggests, you might economize images by making a set of unique ones and sharing them amongst the squares. On the other hand, a small board game's memory requirements is insignificant. Still, I am sure that a demonstration of smart resource management would be good for your grade.

To pursue this, you will want to define a class that implements the changeColor() method; either your own class or by subclassing the Sprite class. Next you will want to store your objects (or sprites) in a list or dict for easy lookup. Using a dict will allow the index style board[i,j] versus board[i][j], but you may find good reasons to stick with a list. Lastly, if you go with Sprites I would recommend also creating a sprite group (as Jason suggests) if only because its update() and draw() methods can make your tasks much easier.

I still suggest you read a sprite tutorial on pygame.org. I predict that demonstrating this acquired knowledge would be very good for your grade. :)

Gumm