📜  在Python中使用 Turtle 绘制点图案

📅  最后修改于: 2022-05-13 01:55:10.708000             🧑  作者: Mango

在Python中使用 Turtle 绘制点图案

先决条件:海龟编程基础

Turtle 是Python中的一个内置模块。它使用屏幕(纸板)和海龟(笔)提供绘图。要在屏幕上绘制一些东西,我们需要移动海龟(笔)。要移动海龟,有一些函数,即 forward()、backward() 等。

1) 画点正方形

使用以下步骤:

  • 进口龟
  • 做乌龟
  • 定义一个函数来绘制一个带点的正方形
  • 调用那个函数
  • 把乌龟藏起来。

下面是实现:

Python3
# import package and making object
import turtle 
  
  
pen = turtle.Turtle()
  
# method to draw square with dots
# space --> distance between dots
# x     --> side of square
def draw(space,x):
  for i in range(x):
    for j in range(x):
        
        # dot
        pen.dot()
          
        # distance for another dot
        pen.forward(space)
    pen.backward(space*x)
      
    # direction
    pen.right(90)
    pen.forward(space)
    pen.left(90)
  
# Main Section
pen.penup()
draw(10,8)
  
# hide the turtle
pen.hideturtle()


Python3
# import package and making object
import turtle 
  
  
pen = turtle.Turtle()
  
# method to draw rectangle with dots
# space --> distance between dots
# x     --> height of rectangle
# y     --> width of rectangle
def draw(space,x,y):
  for i in range(x):
    for j in range(y):
        
        # dot
        pen.dot()
          
        # distance for another dot
        pen.forward(space)
    pen.backward(space*y)
      
    # direction
    pen.right(90)
    pen.forward(space)
    pen.left(90)
  
# Main Section
pen.penup()
draw(10,5,12)
  
# hide the turtle
pen.hideturtle()


Python3
# import package and making object
import turtle 
  
  
pen = turtle.Turtle()
  
# method to draw diamond with dots
# space --> distance between dots
# x     --> side of diamond
def draw(space,x):
  for i in range(x):
    for j in range(x):
          
        # dot
        pen.dot()
          
        # distance for another dot
        pen.forward(space)
    pen.backward(space*x)
      
    # direction
    pen.right(90)
    pen.forward(space)
    pen.left(90)
  
# Main Section
pen.penup()
  
# direction to form diamond
pen.left(45)
draw(10,8)
  
# hide the turtle
pen.hideturtle()


输出 :

2) 画点矩形

使用以下步骤:

  • 进口龟
  • 做乌龟
  • 定义一个函数来绘制一个带点的矩形
  • 调用那个函数
  • 把乌龟藏起来。

下面是实现:

Python3

# import package and making object
import turtle 
  
  
pen = turtle.Turtle()
  
# method to draw rectangle with dots
# space --> distance between dots
# x     --> height of rectangle
# y     --> width of rectangle
def draw(space,x,y):
  for i in range(x):
    for j in range(y):
        
        # dot
        pen.dot()
          
        # distance for another dot
        pen.forward(space)
    pen.backward(space*y)
      
    # direction
    pen.right(90)
    pen.forward(space)
    pen.left(90)
  
# Main Section
pen.penup()
draw(10,5,12)
  
# hide the turtle
pen.hideturtle()

输出 :

3)画点钻石:

使用以下步骤:

  • 进口龟
  • 做乌龟
  • 定义一个用点绘制菱形的函数
  • 调用那个函数
  • 把乌龟藏起来。

下面是实现:

Python3

# import package and making object
import turtle 
  
  
pen = turtle.Turtle()
  
# method to draw diamond with dots
# space --> distance between dots
# x     --> side of diamond
def draw(space,x):
  for i in range(x):
    for j in range(x):
          
        # dot
        pen.dot()
          
        # distance for another dot
        pen.forward(space)
    pen.backward(space*x)
      
    # direction
    pen.right(90)
    pen.forward(space)
    pen.left(90)
  
# Main Section
pen.penup()
  
# direction to form diamond
pen.left(45)
draw(10,8)
  
# hide the turtle
pen.hideturtle()

输出 :