在Python中使用Turtle模块画一个小屋
Turtle是Python中的一个内置模块,它有许多功能,如forward()、backward()、right()和left()等。你可以通过使用Python中的Turtle模块在屏幕上画一个小屋。在这篇文章中,我们将使用Turtle模块创建一个小屋。
步骤:
1.import turtle
2.设置背景颜色。
3.定义一个函数来绘制小屋的前面部分,即矩形。
4.定义一个函数来绘制小屋的顶部部分,即等边三角形。
5.定义一个函数来绘制小屋的侧面部分,即平行四边形。
6.使用penup()和pendown()函数为小屋绘制窗户和门。
7.在小屋里填上适当的颜色。
以下实现。
import turtle
import math
# Set the background color
screen = turtle.Screen()
screen.bgcolor("lightpink")
# Create our turtle
t = turtle.Turtle()
t.color("black")
t.shape("turtle")
t.speed(5)
# Define a function to draw and
# fill a rectangle with the given
# dimensions and color
def drawRectangle(t, width, height, color):
t.fillcolor(color)
t.begin_fill()
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.end_fill()
# Define a function to draw and fill an equalateral right
# triangle with the given hypotenuse length and color. This
# is used to create a roof shape.
def drawTriangle(t, length, color):
t.fillcolor(color)
t.begin_fill()
t.forward(length)
t.left(135)
t.forward(length / math.sqrt(2))
t.left(90)
t.forward(length / math.sqrt(2))
t.left(135)
t.end_fill()
# Define a function to draw and fill a parallelogram, used to
# draw the side of the house
def drawParallelogram(t, width, height, color):
t.fillcolor(color)
t.begin_fill()
t.left(30)
t.forward(width)
t.left(60)
t.forward(height)
t.left(120)
t.forward(width)
t.left(60)
t.forward(height)
t.left(90)
t.end_fill()
# Draw and fill the front of the house
t.penup()
t.goto(-150, -120)
t.pendown()
drawRectangle(t, 100, 110, "blue")
# Draw and fill the front door
t.penup()
t.goto(-120, -120)
t.pendown()
drawRectangle(t, 40, 60, "lightgreen")
# Front roof
t.penup()
t.goto(-150, -10)
t.pendown()
drawTriangle(t, 100, "magenta")
# Side of the house
t.penup()
t.goto(-50, -120)
t.pendown()
drawParallelogram(t, 60, 110, "yellow")
# Window
t.penup()
t.goto(-30, -60)
t.pendown()
drawParallelogram(t, 20, 30, "brown")
# Side roof
t.penup()
t.goto(-50, -10)
t.pendown()
t.fillcolor("orange")
t.begin_fill()
t.left(30)
t.forward(60)
t.left(105)
t.forward(100 / math.sqrt(2))
t.left(75)
t.forward(60)
t.left(105)
t.forward(100 / math.sqrt(2))
t.left(45)
t.end_fill()
turtle.done()
输出: