1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python画图小房子代码-python少儿编程-turtle 基本绘图

python画图小房子代码-python少儿编程-turtle 基本绘图

时间:2023-08-28 14:07:02

相关推荐

python画图小房子代码-python少儿编程-turtle 基本绘图

介绍了利用python中turtle模块画一些简单图形的例程。

1.画一个正方形

先画一个正方形,介绍forward命令和right命令

import turtle

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

image.png

练习:画一个三角形

import turtle

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

turtle.right(120)

turtle.forward(100)

2.用循环的方式来画刚刚的正方形

学习for的用法

import turtle

for x in range(0,3):

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

练习用for循环画三角形

import turtle

for x in range(0,3):

turtle.forward(100)

turtle.right(120)

3.给三角形上色

import turtle

turtle.begin_fill()

turtle.fillcolor('red')

for x in range(0,3):

turtle.forward(100)

turtle.right(120)

turtle.end_fill()

image.png

4.换个位置开始画

知识点:penup()、pendown()、goto(x,y)

import turtle as t

t.penup()#抬起画笔

t.goto(-100,-100)#移动到指定位置

t.pendown()#放下画笔

5.第一个项目:画一个小房子

import turtle as t

t.penup()

t.goto(-130,-100)

t.pendown()

# 房子的主体

t.forward(300)

t.left(90)

t.forward(150)

t.left(90)

t.forward(300)

t.left(90)

t.forward(150)

#门

t.fillcolor('blue')

t.begin_fill()

t.left(90)

t.forward(200)

t.left(90)

t.forward(100)

t.right(90)

t.forward(50)

t.right(90)

t.forward(100)

t.end_fill()

#屋顶

t.penup()

t.goto(170,50)

t.pendown()

t.right(180)

t.left(30)

t.forward(100)

t.left(60)

t.forward(200)

t.left(60)

t.forward(100)

#窗户

t.fillcolor('red')

t.begin_fill()

t.left(120)

t.forward(100)

for i in range(0,3):

t.right(90)

t.forward(50)

t.end_fill()

image.png

6.画圆圈

主要介绍circle命令的用法,包括steps

import turtle as t

t.circle(50)

#turtle.circle(radius, extent=None, steps=None)

7.任务,画太极图

import turtle

R=100

# 开始画出半边的轮廓

turtle.fillcolor('white')

turtle.begin_fill()

turtle.circle(R / 2, 180)

turtle.circle(R, 180)

turtle.circle(R / 2, -180)

turtle.end_fill() # 结束填充 上色完成

# 绘制该半边的鱼眼

turtle.penup() # 提起画笔,移动不留痕

turtle.goto(0, R/3) # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边

turtle.pendown() # 放下画笔,移动留痕

# 获取鱼眼填充色, 与该半边相反

turtle.fillcolor('black')

turtle.begin_fill()

turtle.circle(-R / 6, 360)

turtle.end_fill()

# 回到原点,为下半圆的开始做准备

turtle.penup()

turtle.goto(0, 0)

turtle.pendown()

# 开始画出右半边的轮廓

turtle.fillcolor('black')

turtle.begin_fill()

turtle.circle(R/2, 180)

turtle.circle(R, 180)

turtle.circle(R/2, -180)

turtle.end_fill()

# 绘制下半边的鱼眼

turtle.penup() # 提起画笔,移动不留痕

turtle.goto(0, -R/3) # 移动到该半边的鱼眼的圆上 R/3*c 表示移动到哪边

turtle.pendown() # 放下画笔,移动留痕

# 获取鱼眼填充色, 与该半边相反

turtle.fillcolor('white')

turtle.begin_fill()

turtle.circle(-R / 6, 360)

turtle.end_fill()

image.png

8.复习for循环:画五角星·

import turtle as t

t.shape('turtle')

t.fillcolor('red')

t.begin_fill()

for x in range(0,5):

t.forward(100)

t.left(144)

t.end_fill()

9.项目:用曲线和直线作画

import turtle as t

t.shape('turtle')

t.penup()

t.goto(-100,0)

t.pendown()

#画车身

t.fillcolor('blue')

t.begin_fill()

t.forward(250)

t.left(90)

t.forward(50)

t.left(90)

t.forward(50)

t.right(90)

t.forward(50)

t.left(90)

t.forward(150)

t.left(90)

t.forward(50)

t.right(90)

t.forward(50)

t.left(90)

t.forward(50)

t.end_fill()

#左轮子

t.fillcolor('yellow')

t.begin_fill()

t.penup()

t.goto(-80,0)

t.pendown()

t.circle(30)

t.end_fill()

#右轮子

t.fillcolor('yellow')

t.begin_fill()

t.penup()

t.goto(130,0)

t.pendown()

t.circle(-30)

t.end_fill()

image.png

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。