General

Chat

python turtle question

I made a function for a random turtle race
when turtle1(Tom) is in front of turtle2(Amy) it becomes green and vise versa

My code is this:
import turtle
import random

tom = turtle.Turtle('turtle')
amy = turtle.Turtle('turtle')
myScreen = turtle.Screen()
def setup(tom, amy, myScreen):
myScreen.setworldcoordinates(0,0,50,50)
tom.up()
amy.up()
tom.setposition(1,25)
amy.setposition(1,25)

tom.color('red')
amy.color('red')
tom.down()
amy.down()
myScreen.exitonclick()

def newHeading(turtle, angleOfTipsiness):
angleOfTipsiness = 70
tom.setheading(random.randint(-angleOfTipsiness, angleOfTipsiness))
amy.setheading(random.randint(-angleOfTipsiness, angleOfTipsiness))

def newColor(tom, amy):
if tom.xcor() >= amy.xcor:
tom.color('green')
amy.color('red')
if amy.xcor() >= tom.xcor:
amy.color('green')
tom.color('red')
def tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps):
nSteps = 50
tom.forward(random.randint(1, nSteps))
amy.forward(random.randint(1, nSteps))

setup(tom, amy, myScreen)
newHeading(turtle, angleOfTipsiness)
newColor(tom, amy)
tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps)

However, when I run this code the turtles don't move forward from their starting positions
What is the error in my code? Any help would be greatly appreciated

April 26, 2015

7 Comments • Newest first

MarshMallows

[quote=kevinho00]yea I just think there is something crucial that I'm doing wrong lol
even with one turtle and clear values the turtle doesn't move[/quote]

Hmm...I was thinking something along the lines of:
setup(tom, amy, myScreen)
finishLine = 100 (if you're racing along the x-axis)
while ((amy.xcoord < finishLine) or (tom.xcoord < finishLine)):
newHeading(turtle, angleOfTipsiness)
newColor(tom, amy)
tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps)

Though, that may not be your problem..is turtle a python library?
I tried "pip install turtle", but for some reason it failed.

Have you tried simple lines of code like placing the turtle down and making it move manually?

Edit: Here's a semi-working version. Screen exists when the turtles pass x = 25. I think the exitonclick was the culprit.

import turtle
import random

def newHeading(tom, amy):
angleOfTipsiness = 70
tom.setheading(random.randint(-angleOfTipsiness, angleOfTipsiness))
amy.setheading(random.randint(-angleOfTipsiness, angleOfTipsiness))

def newColor(tom, amy):
if tom.xcor() >= amy.xcor():
tom.color('green')
amy.color('red')
if amy.xcor() >= tom.xcor():
amy.color('green')
tom.color('red')

def tipsyTurtleRace(tom, amy):
nSteps = 2
tom.forward(random.randint(1, nSteps))
amy.forward(random.randint(1, nSteps))

tom = turtle.Turtle('turtle')
amy = turtle.Turtle('turtle')
myScreen = turtle.Screen()
#setup(tom, amy, myScreen)
myScreen.setworldcoordinates(0,0,50,50)

tom.setposition(1,25)
amy.setposition(1,25)
tom.color('red')
amy.color('red')
#myScreen.exitonclick()
while ((tom.xcor() < 25) and (amy.xcor() < 25)):
newHeading(tom, amy)
newColor(tom, amy)
tipsyTurtleRace(tom, amy)

Reply April 27, 2015 - edited
kevinho00

[quote=achyif]I hope you're incrementing i so that the while loop will eventually quit.

Have you tried just having one turtle move forward 50 times a set distance x and see if that works? Then you can just throw another turtle in, replace x with the random integer, and add the adjust heading function before you move?[/quote]

yea I just think there is something crucial that I'm doing wrong lol
even with one turtle and clear values the turtle doesn't move

Reply April 26, 2015 - edited
achyif

I hope you're incrementing i so that the while loop will eventually quit.

Have you tried just having one turtle move forward 50 times a set distance x and see if that works? Then you can just throw another turtle in, replace x with the random integer, and add the adjust heading function before you move?

Reply April 26, 2015 - edited
kevinho00

@MarshMallows:
I went with option 2 and made them global
I also input
while i <= 70:
tom.setheading(random.randint(-angleOfTipsiness, angleOfTipsiness))
amy.setheading(random.randint(-angleOfTipsiness, angleOfTipsiness))

and
while i <= 50:
tom.forward(random.randint(1, nSteps))
amy.forward(random.randint(1, nSteps))

is this the incorrect way? I'm still not getting a response from the turtles

Reply April 26, 2015 - edited
MarshMallows

[quote=kevinho00]Ah okay
Thanks I think I forgot the while loop to actually make them race
Edit: also when you said to take out nSteps and angleOfTipsiness
did you mean to take them out of the function and place it outside or remove them completely?[/quote]

Uhh you have two options:

Option #1: Remove them from the arguments completely
Ex. def newHeading(tom, amy)

Option #2: Make both nSteps and angleOfTipsiness global variables
Define nSteps = 50 and angleOfTipsiness = 70 outside the functions to make them global.

Reply April 26, 2015 - edited
kevinho00

[quote=MarshMallows]Thanks for posting your code this time
I'll take a look into it - give me a few minutes

Edit: Just a few technicalities first
You should change your newColor to this way - no point in checking for coordinates twice!
def newColor(tom, amy):
if tom.xcor() > amy.xcor: ahead
tom.color('green')
amy.color('red')
elif amy.xcor() == tom.xcor:
amy.color('red')
tom.color('red')
else #Amy ahead
amy.color('green')
tom.color('red')

I think your error is in two functions:
def tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps): and angleOfTipsiness is undefined by the looks of it when you call it, so simply take it out

def newHeading(turtle, angleOfTipsiness): reasoning, angleOfTipsiness is undefined when you call it - take it out; not quite sure what you're doing, but the function should take in tom and amy

Also, when you call the functions, try calling in this order:
newHeading(turtle, angleOfTipsiness) #Set heading
tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps) turtles
newColor(tom, amy) who's ahead

Note: If you're actually trying to make them race, try setting a while loop with the condition being a certain x-cordinate. Hope I helped and it works[/quote]

Ah okay
Thanks I think I forgot the while loop to actually make them race
Edit: also when you said to take out nSteps and angleOfTipsiness
did you mean to take them out of the function and place it outside or remove them completely?

Reply April 26, 2015 - edited
MarshMallows

Thanks for posting your code this time
I'll take a look into it - give me a few minutes

Edit: Just a few technicalities first
You should change your newColor to this way - no point in checking for coordinates twice!
def newColor(tom, amy):
if tom.xcor() > amy.xcor: ahead
tom.color('green')
amy.color('red')
elif amy.xcor() == tom.xcor:
amy.color('red')
tom.color('red')
else #Amy ahead
amy.color('green')
tom.color('red')

I think your error is in two functions:
def tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps): and angleOfTipsiness is undefined by the looks of it when you call it, so simply take it out

def newHeading(turtle, angleOfTipsiness): reasoning, angleOfTipsiness is undefined when you call it - take it out; not quite sure what you're doing, but the function should take in tom and amy

Also, when you call the functions, try calling in this order:
newHeading(turtle, angleOfTipsiness) #Set heading
tipsyTurtleRace(tom, amy, angleOfTipsiness, nSteps) turtles
newColor(tom, amy) who's ahead

Note: If you're actually trying to make them race, try setting a while loop with the condition being a certain x-cordinate. Hope I helped and it works

Reply April 26, 2015 - edited