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

Re: [pygame] sprite collisions - looking for simple snippet



Traceback (most recent call last):
  File "col.py", line 52, in <module>
    collide=pygame.sprite.collide_rect(sprid[0],sprid[1])
  File "C:\Python25\lib\site-packages\pygame\sprite.py", line 1146, in collide_r
ect
    return left.rect.colliderect(right.rect)
AttributeError: 'pygame.Surface' object has no attribute 'rect'

This error happens because you are passing Surfaces to pygame.sprite.collide_rect, not Sprites.

On Mon, Jul 13, 2009 at 12:27, Zack Schilling <zack.schilling@xxxxxxxxx> wrote:
I tried to read and correct your code, but I have no idea at all what's going on. It's written like C++ transposed directly into python, which is confusing. It doesn't help that there are no comments at all and the variable names are cryptic. A big problem seems to be simply feeding wrong object types to all the functions. You're sending sprites to blitters and rects to the sprite collide.

I'll have a go at understanding what you want to do and making it work, but I make no promises. If I can get that far, I'll also rewrite it in a much more pythonic way. Then you can compare the two and learn much better practices when working in python.

-Zack


On Jul 13, 2009, at 8:03 AM, Paulo Silva wrote:

hi!

well, for me finding a good pygame snippet with sprite collision (and
simple to understand) is like finding a needle in a haystack...

but when i try do on my own, i get this:

Traceback (most recent call last):
File "_spritesheetexample15b4_difsiz_collision.py", line 47, in <module>
collide=pygame.sprite.collide_rect(spridr[0].rect,spridr[1].rect)
AttributeError: 'pygame.Rect' object has no attribute 'rect'

the example is: http://pastebin.com/f524a8cf2

all suggestions are welcome! (be welcome also fixing that pastebin,
and sending us the pastebin url with the fixes! ;) ) - sorry it's not
PEP8 yet... :/

thanks! ;)



On 7/13/09, René Dudfield <renesd@xxxxxxxxx> wrote:
On Mon, Jul 13, 2009 at 7:32 AM, Paulo Silva <nitrofurano@xxxxxxxxx> wrote:

this reference i know from a long time, and figured out easily there
were the exact methods to be used - the problem is i couldn't have a
snippet working only following that - on my oppinion on that
referenence, each pygame method should have a working snippet to be
tried out - for me is easier to work on tiny snippets, just like with
lego bricks




Hi,

pygame comes with examples.  Either use those, or use the code search button
next to each method, to search through all the projects on the internet that
use pygame.  There's 238 results for spritcollide for example.


cu,