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

Re: [pygame] rect.collidedict() and rect.collidedictall() patch



Hi

sure, tuples are hashable and if it brakes the API thne forget my patch. But then the documentation must be more clear about what to put there in! (a small example perhaps?) Because the simples assumption is a Rect object (wich can not work as key). I though a bit about when you would use a dict to store rect/sprite (or whatever) pairs and then check for collision. I think it is not that useful (because if you use a rect/sprite pair, the rect has to be updated manually if it changes in the sprite).

Besides of the sugestions of Tim Ansell :

"My suggestions is,
Rect.collidedictkeys   <- Check against keys
Rect.collidedictvalues <- Check against values
and then map Rect.collidedict to the keys variety for backwards
compatibility?"

perhaps a function rect.collidelistattr([obj_list], 'attr_name') ->[colliding_obj_list] or [idx_list]

would be more useful. With this function you could do something like:

sprites = [pygame.sprite.Sprite()]
# every sprite need to have a rect attribute, sprite.rect
colliders = hero.rect.collidelistattrall(sprites, 'rect')
for c in colliders:
   #do what you need to do

or if the object have their 'rect' saved ad another attribute:

things = [thing]
# every thing need to have a world_rect attribute, thing.world_rect
other_thing.big_rect.collidelistattr(things, 'world_rect')->[colliding_things]

Unless you see no need or speed improvement compared to a straight forward python implementation:

data = []
for sprite in sprite:
   if hero.rect.colliderect(sprite.rect):
       data.append(sprite)


Since pygame 1.8 should be released soon I think this feature should be added for next release (pygame1.8.1 or how ever the numbering scheme works). How does it come that such useful things are not in pygame!? Is it because rects are mainly ment for screen rects?

~DR0ID



René Dudfield schrieb:
Hi,

I think tuples are hashable though?  Like (0,0,10,10) etc.

If we change it then it's breaking the API a bit... but maybe no one
used collidedictall anyway?


On 8/26/07, DR0ID <dr0id@xxxxxxxxxx> wrote:
Hi

since a dictionary with rectstyle keys is not possible (because rects
are not hashable), the source did not make sense to check if the keys
where rectstyle objects. It should be the values that must be a
rectstyle. Patch changes exactly that.

Documentation of collidedictall() updated to say that a empty list [] is
returned if no collision is found (not a empty dictioniary).


~DR0ID

Index: src/rect.c
===================================================================
--- src/rect.c  (revision 1047)
+++ src/rect.c  (working copy)
@@ -500,14 +500,14 @@
         return NULL;
     if (!PyDict_Check (dict))
         return RAISE (PyExc_TypeError,
-                      "Argument must be a dict with rectstyle keys.");
+                      "Argument must be a dict with rectstyle values (1).");

     while (PyDict_Next (dict, &loop, &key, &val))
     {
-        if (!(argrect = GameRect_FromObject (key, &temp)))
+        if (!(argrect = GameRect_FromObject (val, &temp)))
         {
             RAISE (PyExc_TypeError,
-                   "Argument must be a dict with rectstyle keys.");
+                   "Argument must be a dict with rectstyle values (2).");
             break;
         }
         if (DoRectsIntersect (&self->r, argrect))
@@ -535,7 +535,7 @@
         return NULL;
     if (!PyDict_Check (dict))
         return RAISE (PyExc_TypeError,
-                      "Argument must be a dict with rectstyle keys.");
+                      "Argument must be a dict with rectstyle values (1).");

     ret = PyList_New (0);
     if(!ret)
@@ -543,11 +543,11 @@

     while (PyDict_Next (dict, &loop, &key, &val))
     {
-        if (!(argrect = GameRect_FromObject (key, &temp)))
+        if (!(argrect = GameRect_FromObject (val, &temp)))
         {
             Py_DECREF (ret);
             return RAISE (PyExc_TypeError,
-                          "Argument must be a dict with rectstyle keys.");
+                          "Argument must be a dict with rectstyle values (2).");
         }

         if (DoRectsIntersect (&self->r, argrect))
Index: src/rect.doc
===================================================================
--- src/rect.doc        (revision 1047)
+++ src/rect.doc        (working copy)
@@ -264,7 +264,7 @@
 Rect.collidedictall(dict): return [(key, value), ...]

 Returns a list of all the key and value pairs that intersect with the
-Rect. If no collisions are found an empty dictionary is returned.
+Rect. If no collisions are found an empty list is returned.

 Rect objects are not hashable and cannot be used as keys in a dictionary,
 only as values.