导语
哈喽,大家不知道是上午还是下午好!
今天给大家带来我多年玩pthon时写下的海龟画图代码。话不多说,直接走起。
人工亲测,保证有效!
病毒螺旋线
# ViralSpiral.py - a spiral of spirals!
import turtle
t = turtle.Pen()
t.penup()
turtle.bgcolor("black")
# Ask the user for the number of sides, default to 4, min 2, max 6
sides = int(turtle.numinput("Number of sides",
"How many sides in your spiral of spirals? (2-6)", 4,2,6))
colors = ["red", "yellow", "blue", "green", "purple", "orange"]
# Our outer spiral loop
for m in range(100):
t.forward(m*4)
position = t.position() # Remember this corner of the spiral
heading = t.heading() # Remember the direction we were heading
print(position, heading)
# Our "inner" spiral loop
# Draws a little spiral at each corner of the big spiral
for n in range(int(m/2)):
t.pendown()
t.pencolor(colors[n%sides])
t.forward(2*n)
t.right(360/sides - 2)
t.penup()
t.setx(position[0]) # Go back to the big spiral's x location
t.sety(position[1]) # Go back to the big spiral's y location
t.setheading(heading) # Point in the big spiral's heading
t.left(360/sides + 2) # Aim at the next point on the big spiral
家庭成员螺旋线
# SpiralFamily.py - prints a colorful spiral of names
import turtle # Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange",
"purple", "white", "brown", "gray", "pink" ]
family = [] # Set up an empty list for family names
# Ask for the first name
name = turtle.textinput("My family",
"Enter a name, or just hit [ENTER] to end:")
# Keep asking for names
while name != "":
# Add their name to the family list
family.append(name)
# Ask for another name, or end
name = turtle.textinput("My family",
"Enter a name, or just hit [ENTER] to end:")
# Draw a spiral of the names on the screen
for x in range(100):
t.pencolor(colors[x%len(family)]) # Rotate through the colors
t.penup() # Don't draw the regular spiral lines
t.forward(x*4) # Just move the turtle on the screen
t.pendown() # Draw the next family member's name
t.write(family[x%len(family)], font = ("Arial", int((x+4)/4), "bold") )
t.left(360/len(family) + 2) # Turn left for our spiral
玫瑰花螺旋线
# ColorfulRosettes.py
import turtle
t=turtle.Pen()
turtle.bgcolor('black')
t.speed(0)
t.width(3)
# Ask the user for the number of circles in their rosette, default to 6
number_of_circles = int(turtle.numinput("Number of circles",
"How many circles in your rosette?", 6))
for x in range(number_of_circles):
t.pencolor('red')
t.circle(100)
t.pencolor('yellow')
t.circle(50)
t.left(360/number_of_circles)
玫瑰花病毒螺旋线
# SpiralRosettes.py - challenge problem
import turtle
t=turtle.Pen()
t.penup()
t.speed(0)
turtle.bgcolor('black')
# Ask the user for the number of sides, default to 4, min 2, max 6
sides = int(turtle.numinput("Number of sides",
"How many sides in your spiral of rosettes?", 4,2,6))
colors=['red', 'yellow', 'blue', 'green', 'purple', 'orange']
# Our outer spiral loop
for m in range(100):
t.forward(m*4)
position = t.position() # remember this corner of the spiral
heading = t.heading() # remember the direction we were heading
# Our 'inner' spiral loop draws a rosette
for n in range(sides):
t.pendown()
t.pencolor(colors[n%sides])
t.circle(m/5)
t.right(360/sides - 2)
t.width(m/20)
t.penup()
t.setx(position[0]) # go back to the big spiral's x location
t.sety(position[1]) # go back to the big spiral's y location
t.setheading(heading) # point in the big spirals direction/heading
t.left(360/sides + 2) # move to the next point on the big spiral
多重玫瑰花螺旋线
from turtle import Turtle, mainloop
from time import perf_counter as clock
# wrapper for any additional drawing routines
# that need to know about each other
class Designer(Turtle):
def design(self, homePos, scale):
self.up()
for i in range(5):
self.forward(64.65 * scale)
self.down()
self.wheel(self.position(), scale)
self.up()
self.backward(64.65 * scale)
self.right(72)
self.up()
self.goto(homePos)
self.right(36)
self.forward(24.5 * scale)
self.right(198)
self.down()
self.centerpiece(46 * scale, 143.4, scale)
self.getscreen().tracer(True)
def wheel(self, initpos, scale):
self.right(54)
for i in range(4):
self.pentpiece(initpos, scale)
self.down()
self.left(36)
for i in range(5):
self.tripiece(initpos, scale)
self.left(36)
for i in range(5):
self.down()
self.right(72)
self.forward(28 * scale)
self.up()
self.backward(28 * scale)
self.left(54)
self.getscreen().update()
def tripiece(self, initpos, scale):
oldh = self.heading()
self.down()
self.backward(2.5 * scale)
self.tripolyr(31.5 * scale, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.down()
self.backward(2.5 *