Ohhh, that makes quite a bit of sense. I didn't realize that the log wouldn't necessarily report the full floating number.
Well, I changed it, and it worked:
[spoiler]
Code:
import turtle
import random
wn = turtle.Screen()
jim = turtle.Turtle()
jim.shape('turtle')
joe = turtle.Turtle()
joe.shape('turtle')
def isOnScreen(window, someturtle):
leftbound = -window.window_width()/2
rightbound = window.window_width()/2
topbound = window.window_height()/2
bottombound = -window.window_height()/2
turtleX = someturtle.xcor()
turtleY = someturtle.ycor()
if turtleX > rightbound or turtleX < leftbound or turtleY > topbound or turtleY < bottombound:
someturtle.lt(180)
someturtle.fd(50)
def collisionCheck(turtle1, turtle2):
crash = True
turtle1X = turtle1.xcor()
turtle1Y = turtle1.ycor()
turtle2X = turtle2.xcor()
turtle2Y = turtle2.ycor()
turtle1Pos = (int(turtle1X), int(turtle1Y))
turtle2Pos = (int(turtle2X), int(turtle2Y))
if turtle1Pos != turtle2Pos:
crash = False
return crash
def startPos(window, turtle1, turtle2):
leftbound = -window.window_width()/2
rightbound = window.window_width()/2
bottombound = -window.window_height()/2
topbound = window.window_height()/2
for turtle in [turtle1, turtle2]:
turtle.up()
randomX = random.randrange(int(leftbound), int(rightbound), 50)
randomY = random.randrange(int(bottombound), int(topbound), 50)
turtle.goto(int(randomX), int(randomY))
turtle.down()
def randomMove(turtle):
coinflip = random.randrange(0, 2)
if coinflip == 0:
turtle.lt(90) #heads
else:
turtle.rt(90) #tails
turtle.fd(50)
startPos(wn, jim, joe)
infLoop = 1
while infLoop != 0:
for turtle in [jim, joe]:
randomMove(turtle)
if collisionCheck(jim, joe) == True:
infLoop = 0
break
isOnScreen(wn, turtle)
wn.exitonclick()
[/spoiler]
However, when I tried to clean the code up, the turtles would initialize, move to random starting locations, and then not move; if I click on the window, it'll close, suggesting the program is skipping my while loop and going right to wn.exitonclick(). Here's the modified code:
Code:
import turtle
import random
wn = turtle.Screen()
jim = turtle.Turtle()
jim.shape('turtle')
joe = turtle.Turtle()
joe.shape('turtle')
def isOnScreen(window, someturtle):
leftbound = -window.window_width()/2
rightbound = window.window_width()/2
topbound = window.window_height()/2
bottombound = -window.window_height()/2
turtleX = someturtle.xcor()
turtleY = someturtle.ycor()
if turtleX > rightbound or turtleX < leftbound or turtleY > topbound or turtleY < bottombound:
someturtle.lt(180)
someturtle.fd(50)
def collisionCheck(turtle1, turtle2):
crash = False
turtle1X = turtle1.xcor()
turtle1Y = turtle1.ycor()
turtle2X = turtle2.xcor()
turtle2Y = turtle2.ycor()
turtle1Pos = (int(turtle1X), int(turtle1Y))
turtle2Pos = (int(turtle2X), int(turtle2Y))
if turtle1Pos == turtle2Pos:
crash = True
return crash
def startPos(window, turtle1, turtle2):
leftbound = -window.window_width()/2
rightbound = window.window_width()/2
bottombound = -window.window_height()/2
topbound = window.window_height()/2
for turtle in [turtle1, turtle2]:
turtle.up()
randomX = random.randrange(int(leftbound), int(rightbound), 50)
randomY = random.randrange(int(bottombound), int(topbound), 50)
turtle.goto(int(randomX), int(randomY))
turtle.down()
def randomMove(turtle):
coinflip = random.randrange(0, 2)
if coinflip == 0:
turtle.lt(90) #heads
else:
turtle.rt(90) #tails
turtle.fd(50)
startPos(wn, jim, joe)
while collisionCheck == False:
for turtle in [jim, joe]:
randomMove(turtle)
isOnScreen(wn, turtle)
wn.exitonclick()
It seems to me that this should work fine, but for some reason it isn't. Do you know what might be causing this?
(As a side irritation, do you know why we can't use spoilers?)
Bookmarks