Skip to content
Sign upLog in
← Back to Community

turtle in Python - .undo() does not work?

Profile icon
scottdurkin

import turtle
import random
import math

#functions

def writeScore(score, spen):
spen.undo() #does not work!
spen.penup()
spen.ht()
spen.setposition(-290, 310)
spen.color('white')
scoreStr = 'Score: %s' % score
spen.write(scoreStr, False, align='left', font=('Arial', 24, 'normal'))

def isCollision(t1, t2):
dx = t2.xcor()-t1.xcor() #run or side A
dy = t2.ycor()-t1.ycor() #rise or side B
csq = dx*dx + dy**2
c = math.sqrt(csq)
if c < 20:
return True
else:
return False

def makeBorder():
#border
pen = turtle.Turtle()
pen.ht()
pen.penup()
pen.speed(0)
pen.pensize(9)
pen.setposition(-300, -300)
pen.pendown()
pen.color('#fda500')
for i in range(4):
pen.forward(600)
pen.left(90)

def turnLeft():
player.left(30)

def turnRight():
player.right(30)

def increaseSpeed():
global speed
speed += 1
if speed > 15:
speed = 15

def decreaseSpeed():
global speed
speed -= 1
if speed < 1:
speed = 0

#main
#screen
wn = turtle.Screen()
wn.bgcolor('#0000ff')

makeBorder()

#player
player = turtle.Turtle()
player.color('red')
player.shape('arrow')
player.penup()

#variables
global speed
speed = 0
score = 0
wn.mainloop()
wn.tracer(7)

#keyboard binding
wn.listen()
wn.onkey(turnLeft, 'Left')
wn.onkey(turnRight, 'Right')
wn.onkey(increaseSpeed, 'Up')
wn.onkey(decreaseSpeed, 'Down')

#make goals
maxGoals = 6
goals = []
for i in range(maxGoals):
goals.append(turtle.Turtle())
goals[i].shape('circle')
goals[i].color('pink')
goals[i].speed(3)
goals[i].penup()
goals[i].ht()
goals[i].setposition(random.randrange(-290, 290), random.randrange(-290, 290))
goals[i].left(random.randrange(0, 360))
goals[i].st()

spen = turtle.Turtle() #score pen

#main
while True:
player.forward(speed)
#player boundary checking
if player.xcor() > 290 or player.xcor() < -290:
player.left(180)
if player.ycor() > 290 or player.ycor() < -290:
player.left(180)
#move goals
for i in range(maxGoals):
goals[i].forward(3)
if goals[i].xcor() > 290 or goals[i].xcor() < -290:
goals[i].left(180)
if goals[i].ycor() > 290 or goals[i].ycor() < -290:
goals[i].left(180)
#collision detection
if isCollision(player, goals[i]):
score += 1
goals[i].ht()
goals[i].penup()
goals[i].setposition(random.randrange(-290, 290), random.randrange(-290, 290))
goals[i].left(random.randrange(0, 360))
goals[i].st()
writeScore(score, spen)

Voters
Profile icon
scottdurkin
Comments
hotnewtop
Profile icon
AlexanderRosen

oh god.....indent it

Profile icon
scottdurkin

Finally tried spen.reset() and that did the trick!

Profile icon
scottdurkin

import turtle

score = 0
spen = turtle.Turtle()
while 1:
score += 1
spen.undo() #does not work!
spen.penup()
spen.ht()
spen.setposition(0, 0)
spen.color('red')
scoreStr = 'Score: %s' % score
spen.write(scoreStr, False, align='left', font=('Arial', 24, 'normal'))

Profile icon
scottdurkin

Nice. The editor here removed all the leading whitespaces, which is how Python keeps things straight. : (