samedi 5 avril 2014

Python 2.7 - adapter le code de détection de collision pour les autres côtés - Stack Overflow


I have developed a function that searches for a wall in the direction the sprite is moving in, but so far I have only got it to work for downwards movement and cannot get it to work for left and right. the function is as follows:


def CFW(DirectionMoving):
for a in WallList:
if DirectionMoving == 0: #0=down
if a[0] > (plyposx-30) and a[0] < (plyposx+30): #a[0] is x coord for the wall
return 1 #positive for wall
else:
return 0 #negative for wall

and this is the non-working code for left and right:


        elif DirectionMoving == 1: #1=left
if a[1] > (plyposy-30) and a[1] < (plyposy+30):
return 1
else:
return 0

if anyone could work out why this does not work for left and right I would appreciate the help.




elif DirectionMoving == 1: #1=left
if a[1] > (plyposy-30) and a[1] < (plyposy+30):
return 1
else:
return 0

Does this code ever get entered? Let's change it a bit so you can see what's happening.


elif DirectionMoving == 1: #1=left
print 'dbg: moving left'
if a[1] > (plyposy-30) and a[1] < (plyposy+30):
print 'dbg: moving left - condition met'
return 1
else:
print 'dbg: moving left - condition not met'
return 0

When you've found that out, change it again.


elif DirectionMoving == 1: #1=left
return a[1] > (plyposy-30) and a[1] < (plyposy+30)


I have developed a function that searches for a wall in the direction the sprite is moving in, but so far I have only got it to work for downwards movement and cannot get it to work for left and right. the function is as follows:


def CFW(DirectionMoving):
for a in WallList:
if DirectionMoving == 0: #0=down
if a[0] > (plyposx-30) and a[0] < (plyposx+30): #a[0] is x coord for the wall
return 1 #positive for wall
else:
return 0 #negative for wall

and this is the non-working code for left and right:


        elif DirectionMoving == 1: #1=left
if a[1] > (plyposy-30) and a[1] < (plyposy+30):
return 1
else:
return 0

if anyone could work out why this does not work for left and right I would appreciate the help.



elif DirectionMoving == 1: #1=left
if a[1] > (plyposy-30) and a[1] < (plyposy+30):
return 1
else:
return 0

Does this code ever get entered? Let's change it a bit so you can see what's happening.


elif DirectionMoving == 1: #1=left
print 'dbg: moving left'
if a[1] > (plyposy-30) and a[1] < (plyposy+30):
print 'dbg: moving left - condition met'
return 1
else:
print 'dbg: moving left - condition not met'
return 0

When you've found that out, change it again.


elif DirectionMoving == 1: #1=left
return a[1] > (plyposy-30) and a[1] < (plyposy+30)

0 commentaires:

Enregistrer un commentaire