User:Lidia.Pereira/PNM/Turtle: Difference between revisions
< User:Lidia.Pereira | PNM
No edit summary |
m (Lidia.Pereira moved page Turtle to User:Lidia.Pereira/PNM/Turtle) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 42: | Line 42: | ||
def makeTriple(): | def makeTriple(): | ||
turtle.speed(44) | |||
for i in range(3): | for i in range(3): | ||
i = (i+1)*5 | i = (i+1)*5 | ||
Line 50: | Line 51: | ||
xa = - 255 | xa = - 255 | ||
y = - 244 | y = - 244 | ||
numberColumns = 6 | |||
numberRows = 6 | |||
turtle.penup() | turtle.penup() | ||
turtle.setpos(xa,y) | turtle.setpos(xa,y) | ||
turtle.pendown() | turtle.pendown() | ||
def column(): | def column(): | ||
for i in range( | for i in range(numberColumns): | ||
i=(i+1)*100 | i=(i+1)*100 | ||
makeTriple() | makeTriple() | ||
Line 63: | Line 67: | ||
def finalGrid(): | def finalGrid(): | ||
for i in range( | for i in range(numberRows): | ||
i=(i+1)*100 | i=(i+1)*100 | ||
column() | column() | ||
turtle.penup() | turtle.penup() | ||
turtle. | turtle.setpos(xa+i,y) | ||
turtle.pendown() | turtle.pendown() | ||
finalGrid() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:TurtleGrid.png]] | [[File:TurtleGrid.png]] | ||
And the same, but with a nested loop: | |||
<syntaxhighlight lang="python"> | |||
import turtle | |||
import random | |||
def Poly(n,x): | |||
angle = 360/n | |||
for i in range(n): | |||
turtle.forward(x) | |||
turtle.left(angle) | |||
def makeFlower(p): | |||
for i in range(12): | |||
print Poly(9,p) | |||
turtle.left(30) | |||
def makeTriple(): | |||
turtle.speed(44) | |||
for i in range(3): | |||
i = (i+1)*5 | |||
turtle.color(random.random(),random.random(),random.random()) | |||
print makeFlower(i) | |||
xa = - 255 | |||
y = - 244 | |||
numberColumns = 5 | |||
numberRows = 5 | |||
turtle.penup() | |||
turtle.setpos(xa,y) | |||
turtle.pendown() | |||
def grid(): | |||
for i in range(numberRows): | |||
for c in range(numberColumns): | |||
c=(c+1)*100 | |||
makeTriple() | |||
turtle.penup() | |||
turtle.sety(y+c) | |||
turtle.pendown() | |||
i=(i+1)*100 | |||
turtle.penup() | |||
turtle.setpos(xa+i,y) | |||
turtle.pendown() | |||
grid() | |||
</syntaxhighlight> |
Latest revision as of 11:23, 30 September 2013
First experiences with the turtle! (Thank you very much Tamas and Lucia for technical support)
import turtle
import random
def Poly(n,x):
angle = 360/n
for i in range(n):
turtle.forward(x)
turtle.left(angle)
def loopieLoopie(p):
for i in range(12):
print Poly(9,p)
turtle.left(30)
while(1):
print loopieLoopie(random.randint(40,100))
turtle.color(random.randint(0,1),random.randint(0,1),random.randint(0,1))
First (successful) attempt to draw a grid with the turtle:
import turtle
import random
def Poly(n,x):
angle = 360/n
for i in range(n):
turtle.forward(x)
turtle.left(angle)
def makeFlower(p):
for i in range(12):
print Poly(9,p)
turtle.left(30)
def makeTriple():
turtle.speed(44)
for i in range(3):
i = (i+1)*5
turtle.color(random.random(),random.random(),random.random())
print makeFlower(i)
xa = - 255
y = - 244
numberColumns = 6
numberRows = 6
turtle.penup()
turtle.setpos(xa,y)
turtle.pendown()
def column():
for i in range(numberColumns):
i=(i+1)*100
makeTriple()
turtle.penup()
turtle.sety(y+i)
turtle.pendown()
def finalGrid():
for i in range(numberRows):
i=(i+1)*100
column()
turtle.penup()
turtle.setpos(xa+i,y)
turtle.pendown()
finalGrid()
And the same, but with a nested loop:
import turtle
import random
def Poly(n,x):
angle = 360/n
for i in range(n):
turtle.forward(x)
turtle.left(angle)
def makeFlower(p):
for i in range(12):
print Poly(9,p)
turtle.left(30)
def makeTriple():
turtle.speed(44)
for i in range(3):
i = (i+1)*5
turtle.color(random.random(),random.random(),random.random())
print makeFlower(i)
xa = - 255
y = - 244
numberColumns = 5
numberRows = 5
turtle.penup()
turtle.setpos(xa,y)
turtle.pendown()
def grid():
for i in range(numberRows):
for c in range(numberColumns):
c=(c+1)*100
makeTriple()
turtle.penup()
turtle.sety(y+c)
turtle.pendown()
i=(i+1)*100
turtle.penup()
turtle.setpos(xa+i,y)
turtle.pendown()
grid()