Download presentation
Presentation is loading. Please wait.
Published byMorris Dorsey Modified over 6 years ago
1
turtle module Simple graphics programming How to use it ?
Import ‘turtle’ module by including import turtle Need to create a turtle, and NAME it to manipulate Say, you named a turtle ‘xxx.’ xxx = Turtle() or Pen() Opens up a turtle window Can have more than one turtles at a time – tell them apart by names
2
turtle module >>> import turtle
>>> myT = turtle.Turtle() What is “.” between turtle and Turtle() ? turtle => class (generic object referring to turtle module) Turtle() => method (action, function, constructor) Rough interpretation: get ‘turtle’ type (class) and do ‘Turtle()’ >>> myT.color(“red”) Get ‘myT’ turtle and color it red Attribute Position, heading (direction), color, tail position
3
Turtle in Python Actions References With myT
myT.forward(100), myT.backward(100) myT.right(90), myT.left(45) myT.goto(-200,90), myT.circle(50), myT.color(“red”) myT.up(), myT.down() myT.write(“Hello!”) References Official Python turtle page
4
Which methods to use ? Suppose ‘myT’ turtle is created
Draw a circle myT.circle(100) Draw a thicker circle – myT.width(5) Find out the location (x,y) of myT – myT.position() Move myT to (100,-50) – myT.goto(100,-50) Stop drawing – myT.up() Find out the screen sizes screen=myT.getscreen() screen.window_width() and screen.window_height()
5
Iteration – drunken turle
Random walk or Brownian motion A turtle From the current position Take an arbitrary direction and distance as the next position Goto the next position Next position becomes the current position next time around
6
Random package import random
random.randint(100) returns a random integer How to get a random number between (0,100)? A random number between (-50, 50) ?
7
Drunken Turtle With For loop
Create a turtle, name it on your own For 200 times, repeat Get the distance to the next random position from (curX, curY) Add the distance to (curX, curY) to compute the new random location Move the turtle to the new location
8
Drunken Turtle (curX, curY) = myT.position() for count in range(200):
For 200 times, repeat Get the distance to the next random position from (curX, curY) Add the distance to (curX, curY) to compute the new random location Move the turtle to the new location (curX, curY) = myT.position() for count in range(200): newX = curX + random.randint(-50,50) newY = curY + random.randint(-50,50) myT.goto(newX, newY) curX = newX curY = newY
9
Drunken Turtle Same variable can be replaced by a new updated value
(curX, curY) = myT.position() for count in range(200): newX = curX + random.randint(-50,50) newY = curY + random.randint(-50,50) myT.goto(newX, newY) curX = newX curY = newY curX curY rand_X rand_Y newX newY
10
Drunken Turtle Same variable can be replaced by a new updated value
(curX, curY) = myT.position() for count in range(200): newX = curX + random.randint(-50,50) newY = curY + random.randint(-50,50) myT.goto(newX, newY) curX = newX curY = newY (curX, curY) = myT.position() for count in range(200): curX = curX + random.randint(-50,50) curY = curY + random.randint(-50,50) myT.goto(curX, curY)
11
Keep Turtle in-bound Window bounds win = myT.getscreen()
wWid = win.window_width() wHgt = win.window_height() winRight = int(wWid/2) winLeft = -winRight winTop =int(wHgt/2) winBottom = -winTop
12
Keep Turtle in-bound Updated position at (curX, curY)
If curX > winRight If curX < winLeft If curY > winTop If curY < winBottom
13
Lab 0921 Write a Python program to simulate a drunken turtle for 500 random walks. Modify the drunken turtle program so that the turtle stays in bound your Python program to
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.