# For loop indices

Page 1 of 1

## 4 Replies - 1653 Views - Last Post: 11 January 2011 - 05:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=209286&amp;s=9d7925fdf03ea8f9fe78ac339c959533&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Ntwiles

Reputation: 148
• Posts: 831
• Joined: 26-May 10

# For loop indices

Posted 10 January 2011 - 08:16 PM

Hey guys. I'm trying to wing 2d collision detection and besides the hell I'm having figuring out an efficient way to do it, I'm having trouble with the for loop indices.

Here's what I've got:

```def checkhit():
global bullets
for i in bullets:
xhit = intersectx(i,enemies)
if xhit > 0:
yhit = intersecty(i,enemies)
if xhit == yhit:
print "HIT"

def intersectx(bullet,obj):
for i in range(bullet.xboundmin,bullet.xboundmax):
for ii in obj:
if i >= ii.xboundmin and i <= ii.xboundmax:
return ii
return 0

def intersecty(bullet,obj):
for i in range(bullet.yboundmin,bullet.yboundmax):
for ii in obj:
if i >= ii.yboundmin and i <= ii.yboundmax:
return ii
return 0
```

My problem are the return ii lines. What I need is to return the index of the object, but ii is the object itself. Is there a way to get that number at all?

This post has been edited by Ntwiles: 10 January 2011 - 08:17 PM

Is This A Good Question/Topic? 0

## Replies To: For loop indices

### #2 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: For loop indices

Posted 10 January 2011 - 11:42 PM

There are two ways when you need indexes in for loops. Loop explicitly over the indexes or use enumerate.
```>>> obs = ["o1", "o2", "o3"]

>>> for index, ob in enumerate(obs):
...     print index, ob

0 o1
1 o2
2 o3

>>> for index in range(0, len(obs)):
...     print index, obs[i]

0 o1
1 o2
2 o3

```

### #3 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: For loop indices

Posted 11 January 2011 - 07:33 AM

Nallo's hit the nail on the head. The only thing I would point out is that
```for index in range(0, len(obs)):
```
could be written as
```for index in range(len(obs)):
```
because indices start as 0 by default.

I actually wrote a short article about looping through lists here here (shameless plug). Someone pointed out that I did not discuss enumerate, but I personally don't tend to use it because I feel that for loops require less thinking to translate into other languages. That being said, there is a minor (but measurable) speed increase in using enumerate as oppose range(len).

This post has been edited by atraub: 11 January 2011 - 07:39 AM

### #4 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: For loop indices

Posted 11 January 2011 - 09:30 AM

Had some more time to look at the code and there is something I don't like:

line 11:
```for i in range(bullet.xboundmin,bullet.xboundmax) ....

```

What if bullet is a small object (say 0.2 in both directions) and sits on position 3.5/3.5
then range(xboundmin, xboundmax) = range(3.3, 3.7) = [] and nothing is checked.

I would prefer to write a more general function that checks if bounding boxes of 2 objects intersect and works without relying on integer positions:
```def intersects(obj1, obj2):
"""returns True if bounding boxes intersect, False otherwise"""
#slightly cumbersomly written to drive my point home
above = obj1.xboundmin > obj2.xboundmax
below = obj1.xboundmax < obj2.xboundmin
right = obj1.yboundmax < obj2.yboundmin
left = obj1.yboundmin > obj2.yboundmax
intersects = not above and not below and not right and not left
return intersects

```

side note: Even if your version works for your problem ... this is faster than running a loop over all points in the bounding box.

This post has been edited by Nallo: 11 January 2011 - 09:36 AM

### #5 Ntwiles

Reputation: 148
• Posts: 831
• Joined: 26-May 10

## Re: For loop indices

Posted 11 January 2011 - 05:26 PM

Thanks for the great info guys. And thank you very much for that function Nallo. I couldn't for the life of me figure out a way to do check the bounding boxes without loops.