Python+Turtle绘制表白比心图案

2022-11-23 11:00:22
目录
一、效果展示二、代码详解1 导入库2 播放音乐3 画手4 定义画心的函数5 定义写名字的函数并实现动态画心

一、效果展示

在介绍代码之前,先来看下本文的实现效果。

可以参考下面步骤把Python文件转化成exe,发给未安装Python的他/她。

Pinstaller(Python打包为exe文件)

之前自己把 Python 文件打包成 exe 的时候,折腾了很久,本文将详细地讲述如何快速生成在不安装 Python 的电脑上也能执行的文件

1. 在 prompt 中运行 pip install pyinstaller , 安装 pyinstaller 库

2.  在 prompt 中运行 where pyinstaller 

3.  找到待打包文件存放的路径

把要打包的文件放到找到的路径 

C:\Users\Administrator\Anaconda3\Scripts 中 (我的路径是这个,你就按照第二步的路径)

4.  调用 cmd 窗口

把待打包文件放在

C:\Users\Administrator\Anaconda3 \Scripts 目录下,在该文件夹中按shift+鼠标右键 , 点击 在此处打开命令窗口 调用 cmd 

5.  在 cmd 中输入 pyinstaller -F  文件名

例子:打包 Python 绘制皮卡丘的视频,在cmd中输入 pyinstaller -F  pkq_1.py

即可生成普通图标的exe可执行文件。

6.  生成 exe 文件

可以在路径

C:\Users\Administrator\Anaconda3\Scripts 下的 dist 文件夹中找到打包好的exe文件(即不用安装 Python 也可以运行的文件)。

这样生成的文件图标是标准固定格式,如果想生成特定特定形状的图标需要用第7点中的语句。

7.  生成自定义形状的图标,在cmd中输入:pyinstaller -i  ico路径 -F xxxxx.py

例子: 打包  Python 绘制皮卡丘视频的py文件,在cmd中输入 (注: 我把ico图标和待打包文件放到一个文件夹下了, 所以直接输入了ico的名字)

pyinstaller -i  pikaqiu2.ico -F pkq_1.py

生成图标是皮卡丘形状的exe文件。

二、代码详解

Python绘制比心图的原理是:应用turtle库控制函数绘制不同曲线构成比心图。  

1>

首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。

# -*- coding: UTF-8 -*-
'''
代码用途 :画比心手
作者     :阿黎逸阳
博客     :  https://blog.csdn.net/qq_32532663/article/details/106176609
'''
import os
import pygame
import turtle as t 
from time import sleep

本文应用到的库较少,只应用了os、pygame、turtle和time四个库。

    os库可以设置文件读取的位置。pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。time库可以设置程序休眠的时间,达到动态画心的效果。

    2>

    接着应用pygame库播放背景音乐,本文的音乐是《赵海洋 - 《瞬间的永恒》夜色钢琴曲》。

    #播放音乐
    print('播放音乐')
    pygame.mixer.init()
    pygame.mixer.music.load(r"F:\公众号\520\赵海洋 - 《瞬间的永恒》夜色钢琴曲.mp3") 
    pygame.mixer.music.set_volume(0.5) 
    pygame.mixer.music.play(1, 10)
    

    这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。如果选择播放音乐,需要在代码music.load函数中把你想放音乐的电脑本地存放地址填进去。有部分朋友对这一块有疑问,填充格式可参考如下图片:

    3>

    然后设置画板的大小,并画手。

    #画图
    print('画图')
    t.title('阿黎逸阳的代码公众号')
    t.speed(1)
    #t.screensize(1000, 800)
    t.setup(startx=0, starty = 0, width=800, height = 600)
    #第一根手指头
    print('画第一跟手指头')
    t.penup()
    t.goto(0, 60)
    t.pendown()
    t.color('black')
    t.pensize(2)
    t.setheading(30)
    t.circle(-200, 30)
    t.circle(-8, 120)
    t.setheading(205)
    t.circle(-300, 12)
    t.left(10)
    t.forward(45)
    #画第二条线
    print('画第二条线')
    t.penup()
    t.goto(18, 13)
    t.pendown()
    t.setheading(20)
    t.forward(50)
    #画第三条线
    print('画第三条线')
    t.penup()
    t.goto(18, -15)
    t.pendown()
    t.setheading(20)
    t.forward(50)
    #画第四条线
    print('画第四条线')
    t.penup()
    t.goto(22, -36)
    t.pendown()
    t.setheading(10)
    t.circle(90, 26)
    #画第二根手指的弧线
    print('画第二根手指的弧线')
    t.penup()
    t.goto(55, 55)
    t.pendown()
    t.setheading(-20)
    t.circle(-25, 85)
    #画第三根手指的弧线
    print('画第三根手指的弧线')
    t.penup()
    t.goto(62, 30)
    t.pendown()
    t.setheading(-10)
    t.circle(-20, 120)
    #画第四根手指弧线
    print('画第四根手指的弧线')
    t.penup()
    t.goto(64, 2)
    t.pendown()
    t.setheading(-10)
    t.circle(-20, 120)
    t.right(5)
    t.forward(20)
    t.right(10)
    t.circle(-10, 80)
    t.setheading(78)
    t.circle(-50, 30)
    t.penup()
    t.goto(57, -25)
    t.pendown()
    t.setheading(-50)
    t.forward(10)
    #手的下弧度
    print('画手的下弧线')
    t.penup()
    t.goto(22, -36)
    t.pendown()
    t.setheading(270)
    t.circle(-30, 80)
    t.forward(10)
    t.setheading(240)
    t.forward(40)
    #手的上弧度
    print('画第手的上弧线')
    t.penup()
    t.goto(0, 60)
    t.pendown()
    t.setheading(190)
    t.circle(150, 20)
    t.setheading(225)
    t.forward(10)
    t.setheading(265)
    t.forward(80)
    t.setheading(230)
    t.forward(60)
    #画大拇指
    print('画大拇指')
    t.penup()
    t.goto(8, 63)
    t.pendown()
    t.setheading(95)
    t.circle(-80, 30)
    t.circle(-10, 180)
    t.left(20)
    t.forward(20)
    #画指甲
    print('画指甲')
    t.penup()
    t.goto(84, 85)
    t.pendown()
    t.color('black', 'red')
    t.begin_fill()
    t.setheading(18)
    t.circle(-50, 20)
    t.setheading(270)
    t.circle(-10, 170)
    t.end_fill()
    

    关键代码详解:

      t.pensize(width):设置画笔的尺寸。t.color(color):设置画笔的颜色。t.penup():抬起画笔,一般用于另起一个地方绘图使用。t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。t.pendown():放下画笔,一般和penup组合使用。t.left(degree):画笔向左转多少度,括号里表示度数。t.right(degree):画笔向右转多少度,括号里表示度数。t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。

      画手的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得手的轮廓比较流畅。

      4>

      接着定义画心的函数。

      def heart_bit():
          #画爱心
          print('画爱心')
          t.penup()
          t.goto(70, 135)
          t.pendown()
          t.color('black', 'red')
          t.begin_fill()
          t.setheading(90)
          t.circle(10, 180)
          t.left(30)
          t.forward(30)
          t.setheading(30)
          t.forward(30)
          t.left(38)
          t.circle(12, 188)
          t.end_fill() 

      5>

      最后定义写名字的函数并实现动态画心。

      def write_name(x, y, size, ss):
          t.hideturtle()
          t.penup()
          t.goto(x, y)
          t.pendown()
          t.pencolor('black')
          t.write(ss, font=('Times New Roman', size, 'normal'))
      while 1:
          t.speed(10)
          print('写名字')
          write_name(5, 125, 12, '杨紫')
          write_name(5, 125, 12, '杨紫')
          heart_bit()
          sleep(1)
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
          t.undo()
      

      到此这篇关于Python+Turtle绘制表白比心图案的文章就介绍到这了,更多相关Python Turtle比心内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!