Luke, thank you for your quick and complete response. Based on your
suggestions I have already made some progress! BTW, I am so glad that I
can ask this list my Python questions and get help. I began feeling
quite stuck and not knowing where to turn for help. So, thank you for
the great service!
Luke Paireepinart wrote:
> 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?
Yes, this indeed looks strange. I believe it is particular to this
Livewires exercise. The reason it is strange is that I am comparing a
position of two different shapes, a circle and a square. The position
for the circle is in the center of the circle and it is defined by
player_x and player_y coordinates. The position of the square is
defined by the first two coordinates that make up a square robot_x and
robot_y. The circle radius is 0.5. It took me a loooong time to figure
out how to write my code so that when the robot sits exactly on top of
the player, there is a message "you have been caught" :-) . When I run
the code, the robot will sit on top of the player and I can have the
message printed - yeaaah!
>
>>
>> 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
Based on your guidance, I figured it out. I need to use a return
statement, which I had not encountered before. Now I wrote my
definitions in this way:
def collided():
if player_x == robot_x+0.5 and player_y == robot_y+0.5:
return True
Then I use that value in another definition like this:
def check_collisions():
if collided() == 1:
print "You have been caught"
Which is displaying the message when the robot sits on top of the player.
>>
>> 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.
It is great suggestion to use a print statement for testing! I tried it
and I did not get a printed message either. I will need to think about
it some more. I believe your other email will give me some ideas here.
>>
>> 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.
>
Thanks for this suggestion. I will likely need to re-write how I check
for boundaries. Currently I was checking for boundaries after I moved
the player. I did this by not letting the player_x and player_y
coordinates to be changed if the player was going to go beyond
boundaries. It seems I need to put in a check for boundaries before I
move the player.
>>
>>
>> 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 ;)
Yes, I do want you to comment on the code, absolutely, which I see you
so generously did in another email.
Thank you,
Tonu
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor