python好玩的案例-Python有趣的小案例-美国队长盾牌

# 导入 turtle 模块

# 模块是 python 自带的工具箱,这里将工具箱导入就能使用了

# turtle 模块是 python 用来画图的工具箱

import turtle

# 将 turtle 里的工具拿出来,赋给 t 变量

# 照猫画虎用就是了,这些东西要到很后面才能理解

t = turtle.Turtle()

# 这一行用来加快画笔速度,从 1~9 依次变快,但 0 是最快

t.speed(0)

# 这是向前走,单位是像素

t.forward(100)

# 这是转弯,单位是角度

t.right(120)

t.forward(100)

t.right(120)

t.forward(100)

t.right(120)

# 复制三次,就画了一个三角形

# 正方形

# 长方形

# 如果我们需要改变三角形的边长怎么办?

# 这就要用到变量了,到时候只需改变变量就能改变长度

# 如果有相同的变量,后面定义的会覆盖前面的

l = 200

t.forward(l)

t.right(120)

t.forward(l)

t.right(120)

t.forward(l)

t.right(120)

# for 循环

# 循环还有 while 循环,考虑到用不着就不讲了

# 循环用来处理重复的事情

# range() 是一个区间

# range(3) 相当于 0 1 2

# range(5) 相当于 0 1 2 3 4

# i 取的是 range() 里的值,一次取一个,取一次就循环一次

# 冒号后面必有缩进,缩进的代表是同一个代码块

# 照着用就行了,注意一个字符都不能敲错,不能用中文符号

for i in range(3):

t.forward(l)

t.right(120)

# 如果想画两个三角形怎么办,再复制一个 for 循环?

# 我们用函数将代码封装起来,到时候直接调用就好了

# def 关键字用来定义函数, triangle 是函数名

# 必须要有冒号接缩进,函数里面也是一个代码块

def triangle():

for i in range(3):

t.forward(l)

t.right(120)

# 函数的调用

# triangle()

# 函数可以传递参数进去

def triangle2(l):

for i in range(3):

t.forward(l)

t.right(120)

# 需要传递个参数进去才能调用这个函数

# triangle2(250)

# 定一个函数画长方形

# 四则运算

# + 加

# - 减

# * 乘

# / 除

# // 整除

# % 取余

# 写一个画 n 边形的通用函数

def polygon(l, n):

angle = 360 / n

for i in range(n):

t.forward(l)

t.right(angle)

# polygon(100, 6)

# 画一个五角星

def five_star(l):

for i in range(5):

t.forward(l)

t.right(144)

# five_star(100)

# 画一个圆

# 边长在 36 以上就是个圆

def circle():

for i in range(36):

t.forward(10)

t.right(15)

# circle()

# 在指定的坐标画图

# 比如要在坐标为 (100, 150) 的位置画个正方形

def square(x, y, l):

t.penup()

t.goto(x, y)

t.pendown()

for i in range(4):

t.forward(l)

t.right(90)

# square(100, 150, 100)

# 将画笔定位封装成函数使用,就能有效去除重复代码

def setpen(x, y):

t.penup()

t.goto(x, y)

t.pendown()

t.setheading(0)

def square(x, y, l):

setpen(x, y)

for i in range(4):

t.forward(l)

t.right(90)

# square(100, 150, 100)

# 画一排正方形,共五个,间隔 10

# 蠢方法

# square(100, 150, 30)

# square(140, 150, 30)

# square(180, 150, 30)

# square(220, 150, 30)

# square(260, 150, 30)

# 使用 for 循环、函数

def square_line(x, y, l, n, dis):

for i in range(n):

inner_x = x + (l + dis) * i

square(inner_x, y, l)

# square_line(100, 150, 30, 6, 10)

# 画一个正方形方阵

def square_matrix(x, y, l, n, dis, m):

for i in range(m):

inner_y = y - (l + dis) * i

square_line(x, inner_y, l, n, dis)

# square_matrix(100, 150, 30, 5, 10, 6)

# 填充颜色,给图形上色

def five_star(l):

t.fillcolor('yello')

t.begin_fill()

for i in range(5):

t.forward(l)

t.right(144)

t.end_fill()

# five_star(100)

# 字典的简单用法

# 抽象画

# for i in range(500):

# t.forward(i)

# t.left(90)

# for i in range(500):

# t.forward(i)

# t.left(91)

colors = ['red', 'yellow', 'blue', 'green']

# for i in range(500):

# t.pencolor(colors[i % 4])

# t.circle(i)

# t.left(91)

# sides = 5

# colors = ['red', 'yellow', 'blue', 'orange', 'green', 'purple']

# for i in range(360):

# t.pencolor(colors[i % sides])

# t.forward(i * 3 / sides + i)

# t.left(360 / sides + 1)

# t.width(i * sides / 200)

# 美队盾牌

def circle(x, y, r, color):

n = 36

angle = 360 / n

pi = 3.1415926

c = 2 * pi * r

l = c / n

start_x = x - l / 2

start_y = y + r

setpen(start_x, start_y)

t.pencolor(color)

t.fillcolor(color)

t.begin_fill()

for i in range(n):

t.forward(l)

t.right(angle)

t.end_fill()

def five_star(l):

setpen(0, 0)

t.setheading(162)

t.forward(150)

t.setheading(0)

t.fillcolor('WhiteSmoke')

t.begin_fill()

t.hideturtle()

t.penup()

for i in range(5):

t.forward(l)

t.right(144)

t.end_fill()

def sheild():

circle(0, 0, 300, 'red')

circle(0, 0, 250, 'white')

circle(0, 0, 200, 'red')

circle(0, 0, 150, 'blue')

five_star(284)

sheild()

# 结尾这一行必须有,照着用就行了

turtle.done()

效果图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值