赞
踩
前言:本次项目难度低,学要有Python,并安装turtle,想要开发方便,可以使用pycharm开发
- import turtle
- class main():
- def __init__(self):
- self.shape =turtle.Turtle()
- self.writting = turtle.Turtle()
- self.length = 0
- self.width = 0
- self.write_writting=""
- self.shape_goto_xy = [0,0]
- self.writting_goto_xy =[0,0]
- def draw_shape(self):
- self.shape.pensize(10)
- self.shape.pencolor("red")
- self.shape.fillcolor("yellow")
- self.shape.penup()
- self.shape.goto(self.shape_goto_xy[0],self.shape_goto_xy[1])
- self.shape.pendown()
- self.shape.begin_fill()
- self.shape.right(90)
-
- for i in range(4):
- self.shape.left(90)
- if i == 0 or i == 2:
- self.shape.forward(self.length)
- else:
- self.shape.forward(self.width)
- self.shape.end_fill()
- def draw_writting(self):
- self.writting.penup()
- self.writting.goto(self.writting_goto_xy[0],self.writting_goto_xy[1])
- self.writting.pendown()
- self.writting.write(self.write_writting, font=("STXINGKA", 50, "normal"))
-
- run = main()
-
- run.length=200
- run.width=200
- run.shape.left(45)
- run.draw_shape()
-
- run.writting_goto_xy = [-35,115]
- run.write_writting = "福"
- run.draw_writting()
-
- run.shape.penup()
- for i in range(2):
- run.shape.left(90)
- run.shape.forward(200)
- run.shape.right(45)
- run.shape.forward(200)
- run.shape.left(90)
- run.shape.forward(200)
- run.shape.pendown()
- run.shape.begin_fill()
- for i in range(5):
- run.shape.left(90)
- if i == 0 or i == 2 or i == 4:
- run.shape.forward(100)
- else:
- run.shape.forward(400)
- run.shape.end_fill()
-
- writting_goto_xy=list(run.shape.pos())
- run.writting_goto_xy = [writting_goto_xy[0]+70,writting_goto_xy[1]+10]
- run.write_writting="喜气洋洋"
- run.draw_writting()
-
- run.width=100
- run.length=700
- run.shape_goto_xy=[-450,300]
- run.draw_shape()
-
- writting = "好运常临平安宅"
- write = list(writting)
- write_y = 300
- for i in range(7):
- write_y= write_y-100
- run.writting_goto_xy = [-435,write_y]
- run.write_writting = write[i]
- run.draw_writting()
-
- run.width=700
- run.length=100
- run.shape_goto_xy=[450,300]
- run.draw_shape()
-
- writting = "和气生财幸福家"
- write = list(writting)
- write_y = 300
- for i in range(7):
- write_y= write_y-100
- run.writting_goto_xy = [365,write_y]
- run.write_writting = write[i]
- run.draw_writting()
-
-
-
-
-
- turtle.done()
效果如下:
文字可变,作者文采不高,从网上先找的对联,这里面的文字,也都可以更改,十分的方便,turtle自己也可以展现路径,也是很壮观了。而代码十分简单,做起来给朋友炫下技也是可以的
1.对象
在这里我做了一个类,在这个类里,有这个项目所需的函数与变量,用处不一定很大,但是在后期维护与开发会简单很多,一个是画矩形,一个是话文字,而因为一些问题 ,竖着的字没法直接写,后面会讲到,这里横着写文字是完全没问题的
2.函数
矩形部分如下:
- def draw_shape(self):
- self.shape.pensize(10)
- self.shape.pencolor("red")
- self.shape.fillcolor("yellow")
- self.shape.penup()
- self.shape.goto(self.shape_goto_xy[0],self.shape_goto_xy[1])
- self.shape.pendown()
- self.shape.begin_fill()
- self.shape.right(90)
-
- for i in range(4):
- self.shape.left(90)
- if i == 0 or i == 2:
- self.shape.forward(self.length)
- else:
- self.shape.forward(self.width)
在for循环之前,是一些初始化和调参,用penup让调参不留痕迹,但在这之后,画图是要调用penup进行落笔,fillcolor与pencolor分别表示内部填充颜色与画笔颜色, length代表长,width代表宽,默认画一个横向的长方形,但是画完之后不会自动重置,需要手动调回来。当然,如果长和宽一样的话会绘画正方形。
3.文字:
文字部分如下
- def draw_writting(self):
- self.writting.penup()
- self.writting.goto(self.writting_goto_xy[0],self.writting_goto_xy[1])
- self.writting.pendown()
- self.writting.write(self.write_writting, font=("STXINGKA", 50, "normal"))
写字用了自带的函数,write,font为一个类似常量的东西,第一个是字体文件,要找自己用的,但turtle会统一化简,用goto去到指定位置,writting_goto_xy里,以列表储存,总体来说这里是十分简单的。
初始部分的函数写完,下面开始实战。
1.矩形
先用之前的函数画一个正方形,但因为其回到起点,需要再跑半遍,之后绘画横批部分,再画左右对联,参数要自己找,但本人作为初中生,用勾股定理和直角坐标系就推了出来,难度不大,就是后面写文字的时候,横批的参数不是有理数于是用了pos获取坐标调的参。
2.文字:
写的文t我存在了writting_write里面,在写福字和横批时可以直接用,但在对联上犯了难,最后我有单独存在了一个变量,将对联用list拆成了一个个字,在用for循环每次调取一个,用减少的参数来代替下滑的作用,这是作者的最优解,如果有更好的方案还希望大家提出。
最后画的效果就是这样:
2023朋友用 - Trim
注:本人初中在读,在编程方面才学习三年,若有不好的地方,希望大家指正,谢谢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。