[Tutor] Livewires questions

Luke Paireepinart rabidpoobear at gmail.com
Thu Jul 26 05:14:45 CEST 2007


Tonu Mikk wrote:
> Thanks for offering to help!  I am following the Livewires exercise 
> (attached file "5-robots.pdf").  I have gotten as far as page 7.  
> Attached is also my code so far in robotsarecoming-teleport.py.
> Question 1.  I was checking for collision of a robot and player first 
> in this way:
>
> def check_collisions():
>    if player_x == robot_x+0.5 and player_y == robot_y+0.5:
>       print 'You have been caught'
>
> This was working fine.  I then tried to create a definition like this:
>
> def collided():
>    player_x == robot_x+0.5 and player_y == robot_y+0.5
I haven't looked at your code yet, but this doesn't seem like a very 
good way to check for collisions,
unless the player moves on a grid of 0.5 at a time, and you guarantee 
that you can check if the player collided with a robot on every move.
even so, doesn't this only collide with the robot if the player hits the 
bottom-right corner?
>
> and then check for collisions in this way (as in my code):
> def check_collisions():
>    if collided() == 1:
>        print 'You have been caught'
The reason this isn't working is because your function 'collided' 
doesn't return anything.
Consider this example:

def foo():
    "Hello"

What do you expect to happen when you call foo()?
1) "Hello" won't be printed, because there is no 'print' statement here.
2) "Hello" won't be returned, because you have no return statement.
So what does happen, then?
well, foo() creates a string in memory with "Hello" stored in it, but 
there are no variables referenced to it, so nothing happens.
Basically, the only thing foo() accomplishes is that it wastes memory 
until "Hello" is garbage collected and deleted.

Now consider this:
def foo():
    a == b and b == c

What do you expect to happen here?
It's similar to the above example.
a == b is evaluated.
if it's true, b == c is evaluated.
if it's true, then the value True is there, but it's not assigned to any 
variables, so it just disappears.
Can you see now why your code doesn't work?

Here's an example of a function you'd want to look at to give you an 
idea of what to do:

def foo():
  return a < b
> But this isn't printing out anything when the player and robot 
> collide.  I think I need to pass a variable of collided somehow, but I 
> am not sure how.  I also tried following:
> def check_collisions():
>    if collided()
>       print 'You have been caught'
> but this isn't working either.
This is because collided() is not returning anything.
Try this:
print collided()
you will get this output:
None
>
> Question 2.  I created a if statement to check if the "t" key is 
> pressed on a keyboard.  If it is, I want the player to be placed on 
> another location on the grid.  However nothing happens when I press 
> the "t" key.  I am not sure why.
Instead of changing the player's location, print "YOU PRESSED T" 
instead, and if you see that in the console, you know there's a problem 
with your repositioning code.  If you don't see that, you know it's a 
problem with your input code.
If you can't diagnose further than that, let us know.
>
> Question 3.  I think there is something strange about how I check that 
> my player is within the boundaries of the grid.  When it gets close to 
> the edges of the grid, it can sometimes disappear past it even though 
> I thought I had prevented this from happening.
It's probably similar to the thing I was mentioning above.
Imagine that you have this case:
you're checking if the player is hitting 0.5, 0.5
now if the player can move by acceleration, etc... and you can't 
guarantee that it moves exactly in .5 increments, the player may very 
well move to the position .51,.51 and from there, to .49, .49 which 
would negate your test.
Even if you check a range of values (for example, 0.0 - 0.5 in the x 
axis and 0.0 - 0.5 in the y axis)
the player could theoretically jump right over this boundary (unless you 
restrict his maximum movement speed.)
The best way to check would be:
Say you have the player's coordinates
playerx, playery
and your player-permitted space is 0,0 to 1024, 768
I would suggest something like this:
newx, newy = (playerx + movex, playery + movey)
if newx >= 0 and newx < 1024:
    playerx = newx
if newy >= 0 and newy < 768:
    playery = newy

This will only allow the player to move to wherever he's moving IFF he's 
not moving out of bounds.
Otherwise, he just stays where he is.
Alternatively, you could check the boundaries, and if he's outside, set 
him to the boundary.
That way you could have the player walk right up to the wall, which 
isn't allowed in my code (depending how fast the player moves)
For example, if the player moves at 1.5, and he's at the position 1.4, 
he won't be able to move any closer to the wall.

>
>
> Thank you again for looking at this.  The attached bit of code has 
> taken a long time to create.  I admire all who can program :-).
Hey, I'm going to send this e-mail now, so everyone on the list (you 
included) will be able to read it, but I'm going to send another one in 
a second with comments on your code (Unless you don't want ;)
> Tonu
-Luke


More information about the Tutor mailing list