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

Re: [pygame] Pygame container for rectangles



If these rectangles are the same and are going to be constant, you
should use only one single pygame surface. You can wrap it in a sprite
class, esp. if you are going to be doing collisions, but that's not
necessary. Here's an example of using a surface to make a
tessellation, and then moving it, while using only one surface object:

BLOCK_WIDTH = 5    # width of tesseallation

image = pygame.image.load(image)
image_rect = image.get_rect()
image_list = [image] * 20
x = 0
y = 0
for image in image_list:
    screen.blit((x * image.rect.width),
                (y * image.rect.height))     # screen is the surface
you want to draw to
    x += 1
    if x > BLOCK_WIDTH - 1:
        y += 1
        x = 0

# To move, we can use the for loop method with different starting
values for x and y.  Setting x to 15 would move it by 15, for example.

Westley Martínez


On Wed, Sep 4, 2013 at 7:38 AM, Paul Vincent Craven
<paul@xxxxxxxxxxxxxxxx> wrote:
> If you are not using sprites, you can just iterate through a list like this
> example:
> http://programarcadegames.com/index.php?chapter=introduction_to_animation&lang=en#section_8.2
>
> If you are using sprites, you can create an update() method for that sprite,
> and then call update() on a sprite group. There's some talk about this in
> 14.2:
> http://programarcadegames.com/index.php?chapter=introduction_to_sprites&lang=en#section_14
>
> Or you can directly move them in a loop like this bullets example does:
> http://programarcadegames.com/python_examples/show_file.php?file=bullets.py
>
> Paul Vincent Craven
>
>
> On Wed, Sep 4, 2013 at 7:11 AM, hallur <halluratlason@xxxxxxxxx> wrote:
>>
>> I've drawn a couple of 20x20 rectangles on my screen, kind of like this:
>>
>> #####
>> #####
>> #####
>> #####
>>
>> and I want to move them all at once. I guess I could loop through all of
>> them and move them individually, but I figured there must be a container
>> of
>> some sort I could put all of the rectangles in and then just move the
>> container.
>> So, my question is, is there such a container, and if so, how do I
>> implement
>> it?
>>
>> Any help is greatly appreciated!
>>
>> Thanks in advance,
>> Hallur