当前位置: 首页 > news >正文

python语言入门必须要学习的模块化编程案例游戏---画图案例(三)【源码大全】

彩虹五角星

import turtle                                                          #引用turtle库
q = turtle.Pen()                                                       #构造画笔
turtle.bgcolor("black")                                                #画布的背景颜色为黑色
sides = 7                                                              #定义变量控制环绕程度
colors = ["red","orange","yellow","green","cyan","blue","purple"]
for x in range(360):                                                   #for循环控制画笔的走向与速度q.speed(35)q.pencolor(colors[x%sides])q.forward(x*3/sides+x)q.left(360/sides+1)q.width(x*sides/200)

导入turtle库:

import turtle

这行代码导入了Python的turtle图形库,允许我们在一个窗口中绘制图形。turtle图形库通过一个小海龟(pen)在屏幕上移动来绘制图形。

构造画笔:

q = turtle.Pen()

这里创建了一个Pen对象q,作为我们的画笔。默认情况下,画笔是黑色的,并且初始位置在画布(窗口)的中心。

设置画布背景颜色:

turtle.bgcolor("black")

这行代码将画布的背景颜色设置为黑色。

定义变量控制环绕程度:

sides = 7

这个变量sides用于控制螺旋的环绕程度或者说是每次旋转的角度。这里设置为7,意味着每次循环会旋转约51.43度(360度/7)。

定义颜色列表:

colors = ["red","orange","yellow","green","cyan","blue","purple"]

这是一个包含7种颜色的列表,与sides变量相匹配,用于控制螺旋线每次绘制时的颜色。

绘制螺旋图案:

for x in range(360):q.speed(35)q.pencolor(colors[x%sides])q.forward(x*3/sides+x)q.left(360/sides+1)q.width(x*sides/200)

循环遍历:通过for x in range(360):循环360次,x从0递增到359。
画笔速度:q.speed(35)设置画笔的移动速度。这里的35是一个相对速度值,其中0表示最快,10表示最慢。
画笔颜色:q.pencolor(colors[x%sides])设置画笔颜色。x%sides确保颜色索引在colors列表的范围内循环。
向前移动:q.forward(x3/sides+x)控制画笔向前移动的距离。这个距离随着x的增加而增加,但增长的速度受sides影响。
向左旋转:q.left(360/sides+1)控制画笔每次绘制后向左旋转的角度。360/sides确保螺旋的形状,而额外的+1导致螺旋线之间有一些轻微的交错。
画笔宽度:q.width(x
sides/200)改变画笔的宽度。随着x的增加,画笔宽度也逐渐增加,但增长的速度受到sides和分母200的调节。

超级爱心

# coding=utf-8
import turtle
import timedef draw_circle():for i in range(400):turtle.right(0.5)turtle.forward(1)
def draw_love():
#    turtle.color('red','darkred')
#    turtle.pensize(1)turtle.pen(fillcolor="black",pencolor="red",pensize=8)turtle.speed(2000)turtle.goto(0,0)turtle.begin_fill()turtle.left(140)turtle.forward(224)draw_circle()turtle.left(120)draw_circle()turtle.forward(224)turtle.end_fill()turtle.write("I Love you")time.sleep(2)turtle.up()turtle.goto(150,20)turtle.color('black')turtle.write('相夕妄想 来日方长 很喜欢你 初心永远',font=("微软雅黑",18,"normal"))time.sleep(2)
def draw_abc():turtle.fillcolor("black")turtle.pencolor("red")turtle.pensize(10)turtle.speed(1)turtle.up()turtle.goto(0,-50)turtle.down()turtle.begin_fill()turtle.circle(45)turtle.end_fill()time.sleep(2)
def word():turtle.up()turtle.goto(-100,200)turtle.color("red")turtle.pensize(4)
#   turtle.down()turtle.write('宝贝,宝贝你就是唯一',font=("隶书",18,"bold"))time.sleep(10)
draw_love()
draw_abc()
word()

导入模块

import turtle
import time

**turtle:Python的内置绘图库,用于在屏幕上绘制图形。

time:用于在程序中添加延时,以便更好地观察绘图过程。**

绘制圆形函数

def draw_circle():for i in range(400):turtle.right(0.5)turtle.forward(1)

这个函数通过逐步旋转和前进来绘制一个近似的圆形。每次前进1个单位,并向右旋转0.5度,总共进行400次迭代。

绘制心形函数

def draw_love():...turtle.begin_fill()...draw_circle()...draw_circle()...turtle.end_fill()...

这个函数首先设置画笔的填充颜色和笔的颜色,然后移动到原点开始绘制。它首先向左旋转140度,前进一段距离,然后调用draw_circle()函数绘制心形的上半部分。接着,向左旋转120度,再次调用draw_circle()函数绘制心形的下半部分。最后,使用end_fill()完成心形的填充,并在心形内部写上“I Love you”。

绘制圆形图案函数

def draw_abc():...turtle.begin_fill()turtle.circle(45)turtle.end_fill()...

这个函数绘制一个填充的圆形,半径为45个单位。

绘制文本函数

def word():...turtle.write('宝贝,宝贝你就是唯一',font=("隶书",18,"bold"))...

** …
这个函数在屏幕上指定位置写上文本“宝贝,宝贝你就是唯一”,并设置了文本的字体样式。**

主执行部分

draw_love()
draw_abc()
word()

最后,这三个函数被依次调用,首先绘制心形图案,然后绘制圆形图案,最后在屏幕上写上文本。

带帽的皮卡丘

在这里插入图片描述

完整代码

import turtledef getPosition(x, y):turtle.setx(x)turtle.sety(y)print(x, y)class Pikachu:def __init__(self):self.t = turtle.Turtle()t = self.tt.pensize(3)t.speed(9)t.ondrag(getPosition)def noTrace_goto(self, x, y):self.t.penup()self.t.goto(x, y)self.t.pendown()def leftEye(self, x, y):self.noTrace_goto(x, y)t = self.tt.seth(0)t.fillcolor('#333333')t.begin_fill()t.circle(22)t.end_fill()self.noTrace_goto(x, y + 10)t.fillcolor('#000000')t.begin_fill()t.circle(10)t.end_fill()self.noTrace_goto(x + 6, y + 22)t.fillcolor('#ffffff')t.begin_fill()t.circle(10)t.end_fill()def rightEye(self, x, y):self.noTrace_goto(x, y)t = self.tt.seth(0)t.fillcolor('#333333')t.begin_fill()t.circle(22)t.end_fill()self.noTrace_goto(x, y + 10)t.fillcolor('#000000')t.begin_fill()t.circle(10)t.end_fill()self.noTrace_goto(x - 6, y + 22)t.fillcolor('#ffffff')t.begin_fill()t.circle(10)t.end_fill()def mouth(self, x, y):self.noTrace_goto(x, y)t = self.tt.fillcolor('#88141D')t.begin_fill()# 下嘴唇l1 = []l2 = []t.seth(190)a = 0.7for i in range(28):a += 0.1t.right(3)t.fd(a)l1.append(t.position())self.noTrace_goto(x, y)t.seth(10)a = 0.7for i in range(28):a += 0.1t.left(3)t.fd(a)l2.append(t.position())# 上嘴唇t.seth(10)t.circle(50, 15)t.left(180)t.circle(-50, 15)t.circle(-50, 40)t.seth(233)t.circle(-50, 55)t.left(180)t.circle(50, 12.1)t.end_fill()# 舌头self.noTrace_goto(17, 54)t.fillcolor('#DD716F')t.begin_fill()t.seth(145)t.circle(40, 86)t.penup()for pos in reversed(l1[:20]):t.goto(pos[0], pos[1] + 1.5)for pos in l2[:20]:t.goto(pos[0], pos[1] + 1.5)t.pendown()t.end_fill()# 鼻子self.noTrace_goto(-17, 94)t.seth(8)t.fd(4)t.back(8)# 红脸颊def leftCheek(self, x, y):turtle.tracer(False)t = self.tself.noTrace_goto(x, y)t.seth(300)t.fillcolor('#DD4D28')t.begin_fill()a = 2.3for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a -= 0.05t.lt(3)t.fd(a)else:a += 0.05t.lt(3)t.fd(a)t.end_fill()turtle.tracer(True)def rightCheek(self, x, y):t = self.tturtle.tracer(False)self.noTrace_goto(x, y)t.seth(60)t.fillcolor('#DD4D28')t.begin_fill()a = 2.3for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a -= 0.05t.lt(3)t.fd(a)else:a += 0.05t.lt(3)t.fd(a)t.end_fill()turtle.tracer(True)def colorLeftEar(self, x, y):t = self.tself.noTrace_goto(x, y)t.fillcolor('#000000')t.begin_fill()t.seth(330)t.circle(100, 35)t.seth(219)t.circle(-300, 19)t.seth(110)t.circle(-30, 50)t.circle(-300, 10)t.end_fill()def colorRightEar(self, x, y):t = self.tself.noTrace_goto(x, y)t.fillcolor('#000000')t.begin_fill()t.seth(300)t.circle(-100, 30)t.seth(35)t.circle(300, 15)t.circle(30, 50)t.seth(190)t.circle(300, 17)t.end_fill()def body(self):t = self.tt.fillcolor('#F6D02F')t.begin_fill()# 右脸轮廓t.penup()t.circle(130, 40)t.pendown()t.circle(100, 105)t.left(180)t.circle(-100, 5)# 右耳朵t.seth(20)t.circle(300, 30)t.circle(30, 50)t.seth(190)t.circle(300, 36)# 上轮廓t.seth(150)t.circle(150, 70)# 左耳朵t.seth(200)t.circle(300, 40)t.circle(30, 50)t.seth(20)t.circle(300, 35)# print(t.pos())# 左脸轮廓t.seth(240)t.circle(105, 95)t.left(180)t.circle(-105, 5)# 左手t.seth(210)t.circle(500, 18)t.seth(200)t.fd(10)t.seth(280)t.fd(7)t.seth(210)t.fd(10)t.seth(300)t.circle(10, 80)t.seth(220)t.fd(10)t.seth(300)t.circle(10, 80)t.seth(240)t.fd(12)t.seth(0)t.fd(13)t.seth(240)t.circle(10, 70)t.seth(10)t.circle(10, 70)t.seth(10)t.circle(300, 18)t.seth(75)t.circle(500, 8)t.left(180)t.circle(-500, 15)t.seth(250)t.circle(100, 65)# 左脚t.seth(320)t.circle(100, 5)t.left(180)t.circle(-100, 5)t.seth(220)t.circle(200, 20)t.circle(20, 70)t.seth(60)t.circle(-100, 20)t.left(180)t.circle(100, 20)t.seth(300)t.circle(10, 70)t.seth(60)t.circle(-100, 20)t.left(180)t.circle(100, 20)t.seth(10)t.circle(100, 60)# 横向t.seth(180)t.circle(-100, 10)t.left(180)t.circle(100, 10)t.seth(5)t.circle(100, 10)t.circle(-100, 40)t.circle(100, 35)t.left(180)t.circle(-100, 10)# 右脚t.seth(290)t.circle(100, 55)t.circle(10, 50)t.seth(120)t.circle(100, 20)t.left(180)t.circle(-100, 20)t.seth(0)t.circle(10, 50)t.seth(110)t.circle(100, 20)t.left(180)t.circle(-100, 20)t.seth(30)t.circle(20, 50)t.seth(100)t.circle(100, 40)# 右侧身体轮廓t.seth(200)t.circle(-100, 5)t.left(180)t.circle(100, 5)t.left(30)t.circle(100, 75)t.right(15)t.circle(-300, 21)t.left(180)t.circle(300, 3)# 右手t.seth(43)t.circle(200, 60)t.right(10)t.fd(10)t.circle(5, 160)t.seth(90)t.circle(5, 160)t.seth(90)t.fd(10)t.seth(90)t.circle(5, 180)t.fd(10)t.left(180)t.left(20)t.fd(10)t.circle(5, 170)t.fd(10)t.seth(240)t.circle(50, 30)t.end_fill()self.noTrace_goto(130, 125)t.seth(-20)t.fd(5)t.circle(-5, 160)t.fd(5)# 手指纹self.noTrace_goto(166, 130)t.seth(-90)t.fd(3)t.circle(-4, 180)t.fd(3)t.seth(-90)t.fd(3)t.circle(-4, 180)t.fd(3)# 尾巴self.noTrace_goto(168, 134)t.fillcolor('#F6D02F')t.begin_fill()t.seth(40)t.fd(200)t.seth(-80)t.fd(150)t.seth(210)t.fd(150)t.left(90)t.fd(100)t.right(95)t.fd(100)t.left(110)t.fd(70)t.right(110)t.fd(80)t.left(110)t.fd(30)t.right(110)t.fd(32)t.right(106)t.circle(100, 25)t.right(15)t.circle(-300, 2)############### print(t.pos())t.seth(30)t.fd(40)t.left(100)t.fd(70)t.right(100)t.fd(80)t.left(100)t.fd(46)t.seth(66)t.circle(200, 38)t.right(10)t.fd(10)t.end_fill()# 尾巴花纹t.fillcolor('#923E24')self.noTrace_goto(126.82, -156.84)t.begin_fill()t.seth(30)t.fd(40)t.left(100)t.fd(40)t.pencolor('#923e24')t.seth(-30)t.fd(30)t.left(140)t.fd(20)t.right(150)t.fd(20)t.left(150)t.fd(20)t.right(150)t.fd(20)t.left(130)t.fd(18)t.pencolor('#000000')t.seth(-45)t.fd(67)t.right(110)t.fd(80)t.left(110)t.fd(30)t.right(110)t.fd(32)t.right(106)t.circle(100, 25)t.right(15)t.circle(-300, 2)t.end_fill()# 帽子、眼睛、嘴巴、脸颊self.cap(-134.07, 147.81)self.mouth(-5, 25)self.leftCheek(-126, 32)self.rightCheek(107, 63)self.colorLeftEar(-250, 100)self.colorRightEar(140, 270)self.leftEye(-85, 90)self.rightEye(50, 110)t.hideturtle()def cap(self, x, y):self.noTrace_goto(x, y)t = self.tt.fillcolor('#CD0000')t.begin_fill()t.seth(200)t.circle(400, 7)t.left(180)t.circle(-400, 30)t.circle(30, 60)t.fd(50)t.circle(30, 45)t.fd(60)t.left(5)t.circle(30, 70)t.right(20)t.circle(200, 70)t.circle(30, 60)t.fd(70)# print(t.pos())t.right(35)t.fd(50)t.circle(8, 100)t.end_fill()self.noTrace_goto(-168.47, 185.52)t.seth(36)t.circle(-270, 54)t.left(180)t.circle(270, 27)t.circle(-80, 98)t.fillcolor('#444444')t.begin_fill()t.left(180)t.circle(80, 197)t.left(58)t.circle(200, 45)t.end_fill()self.noTrace_goto(-58, 270)t.pencolor('#228B22')t.dot(35)self.noTrace_goto(-30, 280)t.fillcolor('#228B22')t.begin_fill()t.seth(100)t.circle(30, 180)t.seth(190)t.fd(15)t.seth(100)t.circle(-45, 180)t.right(90)t.fd(15)t.end_fill()t.pencolor('#000000')def start(self):self.body()def main():print('Painting the Pikachu... ')turtle.screensize(800, 600)turtle.title('Pikachu')pikachu = Pikachu()pikachu.start()turtle.mainloop()if __name__ == '__main__':main()

http://www.mrgr.cn/news/54631.html

相关文章:

  • string
  • 如何使用Git推送本地搭建的仓库以及远程克隆的仓库
  • 提升SQL技能,掌握数据分析
  • PostgreSQL的学习心得和知识总结(一百五十五)|[performance]优化期间将 WHERE 子句中的 IN VALUES 替换为 ANY
  • ab命令深入解析:ApacheBench性能测试工具
  • 常用的网络配置命令
  • 前端大佬都在用的useFetcher究竟有多强?
  • 医院信息化与智能化系统(3)
  • Atlas800昇腾服务器(型号:3000)—YOLO全系列NPU推理【跟踪】(八)
  • LeetCode Hot 100
  • 公交线路查询web管理系统||公交线路查询|基于SprinBoot+vue公交线路查询系统(源码+数据库+文档)
  • 第十七周:机器学习笔记
  • 音频/视频提取器:Python和moviepy实现
  • 【网安笔记】4种拒绝服务攻击
  • 【Android】JNI报错 non-zero capacity for nullptr pointer分析
  • 跨国SAP实施 - 美国 - 税法 - 咨询
  • YoloV10改进策略:注意力改进|DeBiFormer,可变形双级路由注意力|引入DeBiLevelRoutingAttention注意力模块(全网首发)
  • C++:反向迭代器
  • ThreadLocal为什么会内存泄漏?如何解决?
  • python 几个日常小工具(计划表,合并文件)
  • 轻松应对PDF编辑难题:四款免费pdf编辑器实测体验
  • 公共字段自动填充-MyBatis-Plus
  • K近邻算法(KNN)的概述与实现
  • 【TDA】持续同调的矢量化方法
  • docker清理未使用的 Docker 资源
  • 【Linux】从 fork() 到 exec():理解 Linux 进程程序替换的魔法