杰瑞科技汇

turtle.shape()怎么改自定义形状?

Of course! Drawing shapes with Python's turtle module is a fantastic way to learn the basics of programming, including loops, angles, and functions.

turtle.shape()怎么改自定义形状?-图1
(图片来源网络,侵删)

Let's break down how to draw common shapes, from simple to more complex.

First, A Quick Setup

Before you can draw, you need to import the turtle module and set up your screen and "turtle" (the pen).

import turtle
# Set up the screen
screen = turtle.Screen()
screen.title("Python Turtle Shapes")
screen.bgcolor("white")
# Create a turtle object
pen = turtle.Turtle()
pen.speed(5)  # Set the drawing speed (1=slowest, 10=fastest, 0=fastest)
pen.pensize(2) # Set the pen thickness
pen.color("blue") # Set the pen color

Drawing a Square

A square has four equal sides and four 90-degree angles.

Logic:

turtle.shape()怎么改自定义形状?-图2
(图片来源网络,侵删)
  1. Move forward a certain distance.
  2. Turn right 90 degrees.
  3. Repeat steps 1 and 2 four times.

Code:

import turtle
pen = turtle.Turtle()
pen.color("blue")
# Loop 4 times for the 4 sides of the square
for _ in range(4):
    pen.forward(100)  # Move forward 100 units
    pen.right(90)    # Turn right 90 degrees
# Hide the turtle when done
pen.hideturtle()
# Keep the window open until you click on it
turtle.done()

Explanation:

  • for _ in range(4): creates a loop that runs exactly 4 times. We use _ as the variable name because we don't need to use the loop counter for anything.
  • pen.forward(100) tells the turtle to move 100 pixels in the direction it's facing.
  • pen.right(90) tells the turtle to turn right by 90 degrees.

Drawing a Triangle (Equilateral)

An equilateral triangle has three equal sides and three 60-degree angles (the angles in any triangle add up to 180 degrees).

Logic:

turtle.shape()怎么改自定义形状?-图3
(图片来源网络,侵删)
  1. Move forward.
  2. Turn left 120 degrees. (180 - 60 = 120)
  3. Repeat three times.

Code:

import turtle
pen = turtle.Turtle()
pen.color("green")
# Loop 3 times for the 3 sides of the triangle
for _ in range(3):
    pen.forward(100)   # Move forward 100 units
    pen.left(120)     # Turn left 120 degrees
pen.hideturtle()
turtle.done()

Drawing a Pentagon

A regular pentagon has five equal sides and five 72-degree angles (360 / 5 = 72).

Logic:

  1. Move forward.
  2. Turn right 72 degrees.
  3. Repeat five times.

Code:

import turtle
pen = turtle.Turtle()
pen.color("red")
# Loop 5 times for the 5 sides of the pentagon
for _ in range(5):
    pen.forward(100)  # Move forward 100 units
    pen.right(72)    # Turn right 72 degrees
pen.hideturtle()
turtle.done()

Drawing a Star

This is a fun one! A 5-pointed star has 5 sides. The key is the angle: you turn 144 degrees at each point.

Logic:

  1. Move forward.
  2. Turn right 144 degrees.
  3. Repeat five times.

Code:

import turtle
pen = turtle.Turtle()
pen.color("purple")
# Loop 5 times for the 5 points of the star
for _ in range(5):
    pen.forward(200)  # Move forward 200 units
    pen.right(144)   # Turn right 144 degrees
pen.hideturtle()
turtle.done()

Drawing a Circle (and an Oval)

A circle is a special shape. The turtle module has a built-in circle() function, which is much easier than calculating tiny sides.

Logic:

  • Use pen.radius().

Code for a Circle:

import turtle
pen = turtle.Turtle()
pen.color("orange")
# The circle function takes the radius as an argument
pen.circle(100) 
pen.hideturtle()
turtle.done()

How to draw an Oval? An oval is just a "squished" circle. You can draw it by drawing two halves of circles with different radii.

import turtle
pen = turtle.Turtle()
pen.color("pink")
# Draw the first half of the oval (left side)
pen.circle(100, 180) # radius=100, extent=180 degrees
# Draw the second half of the oval (right side) with a smaller radius
pen.circle(50, 180)  # radius=50, extent=180 degrees
pen.hideturtle()
turtle.done()

Putting It All Together: A Shape-Drawing Function

You can make your code reusable by creating a function that draws any shape. This function will take the number of sides and the side length as arguments.

The Math:

  • Angle: The turtle must turn by the exterior angle. For any regular polygon, this is 360 / number_of_sides.

Code:

import turtle
def draw_shape(num_sides, side_length):
    """Draws a regular polygon."""
    pen = turtle.Turtle()
    pen.color("black")
    pen.pensize(2)
    # Calculate the exterior angle
    angle = 360 / num_sides
    # Draw the shape
    for _ in range(num_sides):
        pen.forward(side_length)
        pen.right(angle)
    pen.hideturtle()
# --- Let's use the function! ---
# Set up the screen
screen = turtle.Screen()
screen.title("Drawing Shapes with a Function")
# Draw a hexagon (6 sides)
draw_shape(num_sides=6, side_length=100)
# Wait for a click before drawing the next shape
screen.exitonclick() # This closes the window after one click
# To draw multiple shapes, you need to reset the turtle's position
# or use multiple turtles. Here's an example for a new window:
# screen.clear()
# pen.goto(-150, 0) # Move the turtle to a new spot
# draw_shape(num_sides=8, side_length=80) # Draw an octagon
# screen.exitonclick()
turtle.done()

Summary of Common Turtle Commands

Command Description Example
turtle.forward(distance) or turtle.fd(distance) Moves the turtle forward. pen.forward(100)
turtle.backward(distance) or turtle.bk(distance) Moves the turtle backward. pen.backward(50)
turtle.right(angle) or turtle.rt(angle) Turns the turtle right (clockwise). pen.right(90)
turtle.left(angle) or turtle.lt(angle) Turns the turtle left (counter-clockwise). pen.left(45)
turtle.pensize(width) or turtle.width(width) Sets the thickness of the pen. pen.pensize(5)
turtle.pencolor(color) Sets the color of the pen. pen.pencolor("red") or pen.pencolor("#FF0000")
turtle.circle(radius) Draws a circle. pen.circle(75)
turtle.penup() or turtle.pu() Lifts the pen up, so no drawing occurs. pen.penup()
turtle.pendown() or turtle.pd() Puts the pen down, so drawing occurs. pen.pendown()
turtle.goto(x, y) Moves the turtle to a specific coordinate. pen.goto(100, 200)
turtle.speed(speed) Sets the animation speed (0 is fastest). pen.speed(10)
turtle.hideturtle() Makes the turtle invisible. pen.hideturtle()
turtle.done() or turtle.exitonclick() Keeps the window open until you close it. turtle.done()
分享:
扫描分享到社交APP
上一篇
下一篇