0

How do I move a stamp in python turtle module?

Here's my code so far:

import turtle

def draw_start():
    turtle.pu()
    turtle.setpos(-350,300)
    turtle.pendown()
    turtle.right(90)
    turtle.forward(520)


def draw_finish():
    turtle.speed(15)
    turtle.pu()
    for i in range(18):
        turtle.setpos(200,(300-(i*30)))
        square()
    for j in range(18):
        turtle.setpos(215,(285-(j*30)))
        square()

def stamp_turtle(x,y,color):
    turtle.pu()
    turtle.setheading(0)
    turtle.shape("turtle")
    turtle.color(color)
    turtle.setpos(x,y)
    stamp_list.append(turtle.stamp())

def square():
    turtle.pu()
    turtle.fill(True)    
    for i in range(4):
        turtle.forward(15)
        turtle.right(90)
    turtle.fill(False)

print "Welcome to Turtle Racing : "
number_of_turtles = int(raw_input("How many turtles (between 3 and 6) : "))
bet_amount = int(raw_input("How much do you want to bet? $ "))
bet_turtle = raw_input("Which turtle (1 to 5)? ")

color_list=["red","blue","green","brown","yellow","purple"]
stamp_list=[]
draw_start()
draw_finish()
for i in range(number_of_turtles):
    stamp_turtle(-370,280-i*90,color_list[i])`
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
jnthndavis
  • 3
  • 1
  • 2
  • https://docs.python.org/2/library/turtle.html ? – Mike Bell Aug 01 '14 at 20:24
  • I already browsed and read almost the whole thing, and there was nothing that can help. @MikeBell – jnthndavis Aug 01 '14 at 20:26
  • To quickly remove all the stamped turtles, just call `turtle.clearstamp()` with each of the id's that were stored in the `stamp_list`. After that you can stamp them all again in slightly different positions. You'll need to keep track somehow of where each one is so you can modify that slightly before redrawing them all again in their update positions. – martineau Aug 01 '14 at 21:35
  • Using the methods here seemed to work fine for me, but maybe I'm not understanding the full context of the question. https://docs.python.org/2/library/turtle.html#turtle-motion – Mike Bell Aug 01 '14 at 21:55

2 Answers2

0

The answer is you don't move stamps, you move turtles! Stamps have to be removed and restamped whereas turtles can move without redrawing:

import random
import turtle

STAMP_SIZE = 20
SQUARE_SIZE = 15
FINISH_LINE = 200
COLOR_LIST = ['red', 'blue', 'green', 'brown', 'yellow', 'purple']

def draw_start():
    turtle.speed('fastest')
    turtle.penup()
    turtle.setpos(-350, 300)
    turtle.pendown()
    turtle.right(90)
    turtle.forward(520)

def draw_finish():
    turtle.shape('square')
    turtle.shapesize(SQUARE_SIZE / STAMP_SIZE)
    turtle.penup()

    for i in range(18):
        turtle.setpos(FINISH_LINE, (300 - (i * SQUARE_SIZE * 2)))
        turtle.stamp()

    for j in range(18):
        turtle.setpos(FINISH_LINE + SQUARE_SIZE, ((300 - SQUARE_SIZE) - (j * SQUARE_SIZE  * 2)))
        turtle.stamp()

    turtle.hideturtle()

def move_turtle(who):
    who.forward(random.randint(1, 10))
    if who.xcor() < FINISH_LINE:
        turtle.ontimer(lambda who=who: move_turtle(who), 50)

print('Welcome to Turtle Racing!')
number_of_turtles = int(input('How many turtles (between 3 and 6): '))

draw_start()
draw_finish()

turtle_list = []

for idx in range(number_of_turtles):
    racer = turtle.Turtle('turtle', visible=False)
    racer.speed('fastest')  # affects drawing speed, not forward motion
    racer.penup()
    racer.setpos(-350 - STAMP_SIZE, 280 - idx * 90)
    racer.color(COLOR_LIST[idx])
    racer.showturtle()

    turtle_list.append(racer)

for racer in turtle_list:
    turtle.ontimer(lambda who=racer: move_turtle(who), 100)

turtle.exitonclick()

The place where stamping speeds things up is in making your finish line which takes much longer if you try to draw it.

enter image description here

Note that although your original code is Python 2, my answer is Python 3 so you may need to tweak a couple of things if you're still using the older version.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Thanks! Yeah, I kinda figured that out around 2 years 10 months ago when I was still in my first year of CompSci. It's been a while.... – jnthndavis Jun 06 '17 at 22:35
-1

Move a turtle stamp: erase it, invoking penup() and re stamping it somewhere else:

import turtle
import time
a = turtle.Turtle()
a.penup()
a.goto(0, -200)
a.pendown()
a.stamp()
time.sleep(1)
a.clear()
a.penup()
a.goto(10,10)
a.stamp()

The stamp starts out at 0,200 then disappears and reappears at 10,10

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • You erased everything the turtle might have drawn. What you should do is capture the result of `a.stamp()` and ask that stamp ID to clear (just) itself. – cdlane Jun 05 '17 at 22:17