来源:python
ID:python6359
美国队长的锅
emmmmmmmm.......没错就是他的锅
代码
# 所需依赖:python3 pycharm
# print 打印
print('hello world!')
# 注释符号
# 井号后面灰色的内容是注释,相当于笔记,会被机器忽略
# 变量和值
# n 是变量, 100 是值,等号的作用是赋值
# n 相当于高中数学的 xyz ,只不过 xyz 的值只能是数字,变量的功能要更强大
n =
100
m =
'hello'
print(n)
print(m)
# 数据类型,这里只讲两个,剩下的需要同学自己去系统地学习了
# 字符串 和 整数
# 100 是整数类型
# 'hello' 是字符串类型
# 导入 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()
效果图
小猪佩奇
代码
# coding:utf-8
import turtle as t
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255,155,192),"pink")
t.setup(840,500)
t.speed(10)
#鼻子
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
t.begin_fill()
a=0.4
for i in range(120):
if
0<=i<30
or
60<=i<90:
a=a+0.08
t.lt(3)
#向左转3度
t.fd(a)
#向前走a的步长
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill()
t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255,155,192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255,155,192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160,82,45)
t.end_fill()
#头
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300,-30)
t.circle(100,-60)
t.circle(80,-100)
t.circle(150,-20)
t.circle(60,-95)
t.seth(161)
t.circle(-300,15)
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
a=0.4
for i in range(60):
if
0<=i<30
or
60<=i<90:
a=a+0.08
t.lt(3)
#向左转3度
t.fd(a)
#向前走a的步长
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill()
#耳朵
t.color((255,155,192),"pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,56)
t.end_fill()
#眼睛
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255,155,192),"white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
#腮
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
#嘴
t.color(239,69,19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30,40)
t.circle(40,80)
#身体
t.color("red",(255,99,71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()
#手
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)
#脚
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
#尾巴
t.pensize(4)
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70,20)
t.circle(10,330)
t.circle(70,30)
t.done()
效果图
蓝胖子
代码
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: dong dong
# @Env: python 3.6
from turtle import
*
# 无轨迹跳跃
def my_goto(x, y):
penup()
goto(x, y)
pendown()
# 眼睛
def eyes():
tracer(False)
a =
2.5
for i in range(120):
if
0
<= i <
30
or
60
<= i <
90:
a -=
0.05
lt(3)
fd(a)
else:
a +=
0.05
lt(3)
fd(a)
tracer(True)
# 胡须
def beard():
my_goto(-37,
135)
seth(165)
fd(60)
my_goto(-37,
125)
seth(180)
fd(60)
my_goto(-37,
115)
seth(193)
fd(60)
my_goto(37,
135)
seth(15)
fd(60)
my_goto(37,
125)
seth(0)
fd(60)
my_goto(37,
115)
seth(-13)
fd(60)
# 嘴巴
def mouth():
my_goto(5,
148)
seth(270)
fd(100)
seth(0)
circle(120,
50)
seth(230)
circle(-120,
100)
# 围巾
def scarf():
fillcolor('#e70010')
begin_fill()
seth(0)
fd(200)
circle(-5,
90)
fd(10)
circle(-5,
90)
fd(207)
circle(-5,
90)
fd(10)
circle(-5,
90)
end_fill()
# 鼻子
def nose():
my_goto(-10,
158)
fillcolor('#e70010')
begin_fill()
circle(20)
end_fill()
# 黑眼睛
def black_eyes():
seth(0)
my_goto(-20,
195)
fillcolor('#000000')
begin_fill()
circle(13)
end_fill()
pensize(6)
my_goto(20,
205)
seth(75)
circle(-10,
150)
pensize(3)
my_goto(-17,
200)
seth(0)
fillcolor('#ffffff')
begin_fill()
circle(5)
end_fill()
my_goto(0,
0)
# 脸
def face():
fd(183)
fillcolor('#ffffff')
begin_fill()
lt(45)
circle(120,
100)
seth(90)
eyes()
seth(180)
penup()
fd(60)
pendown()
seth(90)
eyes()
penup()
seth(180)
fd(64)
pendown()
seth(215)
circle(120,
100)
end_fill()
# 头型
def head():
penup()
circle(150,
40)
pendown()
fillcolor('#00a0de')
begin_fill()
circle(150,
280)
end_fill()
# 画哆啦A梦
def
Doraemon():
# 头部
head()
# 围脖
scarf()
# 脸
face()
# 红鼻子
nose()
# 嘴巴
mouth()
# 胡须
beard()
# 身体
my_goto(0,
0)
seth(0)
penup()
circle(150,
50)
pendown()
seth(30)
fd(40)
seth(70)
circle(-30,
270)
fillcolor('#00a0de')
begin_fill()
seth(230)
fd(80)
seth(90)
circle(1000,
1)
seth(-89)
circle(-1000,
10)
# print(pos())
seth(180)
fd(70)
seth(90)
circle(30,
180)
seth(180)
fd(70)
# print(pos())
seth(100)
circle(-1000,
9)
seth(-86)
circle(1000,
2)
seth(230)
fd(40)
# print(pos())
circle(-30,
230)
seth(45)
fd(81)
seth(0)
fd(203)
circle(5,
90)
fd(10)
circle(5,
90)
fd(7)
seth(40)
circle(150,
10)
seth(30)
fd(40)
end_fill()
# 左手
seth(70)
fillcolor('#ffffff')
begin_fill()
circle(-30)
end_fill()
# 脚
my_goto(103.74,
-182.59)
seth(0)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(-15,
180)
fd(90)
circle(-15,
180)
fd(10)
end_fill()
my_goto(-96.26,
-182.59)
seth(180)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(15,
180)
fd(90)
circle(15,
180)
fd(10)
end_fill()
# 右手
my_goto(-133.97,
-91.81)
seth(50)
fillcolor('#ffffff')
begin_fill()
circle(30)
end_fill()
# 口袋
my_goto(-103.42,
15.09)
seth(0)
fd(38)
seth(230)
begin_fill()
circle(90,
260)
end_fill()
my_goto(5,
-40)
seth(0)
fd(70)
seth(-90)
circle(-70,
180)
seth(0)
fd(70)
#铃铛
my_goto(-103.42,
15.09)
fd(90)
seth(70)
fillcolor('#ffd200')
# print(pos())
begin_fill()
circle(-20)
end_fill()
seth(170)
fillcolor('#ffd200')
begin_fill()
circle(-2,
180)
seth(10)
circle(-100,
22)
circle(-2,
180)
seth(180-10)
circle(100,
22)
end_fill()
goto(-13.42,
15.09)
seth(250)
circle(20,
110)
seth(90)
fd(15)
dot(10)
my_goto(0,
-150)
# 画眼睛
black_eyes()
if __name__ ==
'__main__':
screensize(800,600,
"#f0f0f0")
pensize(3)
# 画笔宽度
speed(9)
# 画笔速度
Doraemon()
my_goto(100,
-300)
write('by dongdong', font=("Bradley Hand ITC",
30,
"bold"))
mainloop()
效果图
马哥教育618年中大促进行中
四重福利十重豪礼等你来拿!
了解详情请点击图片: