python画生肖兔

2022已经过去,2023正在继续,希望我们都能在平凡的日子里,熠熠生辉。本文介绍运用Python中的turtle库控制函数画生肖兔,并设置了2023年幸运词,快截屏看看你的幸运词吧。


  

一、效果展示

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

可以参考Pinstaller(Python打包为exe文件)一文把Python文件转化成exe,发给未安装Python的朋友。如果喜欢纯享版的,也可以看下如下效果:

  
  

二、代码详解

  
Python绘制生肖兔的原理是:应用turtle库控制函数绘制不同曲线构成效果图。

  
  

1 导入库

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

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

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

  
  

2 播放音乐

  
接着应用pygame库播放背景音乐,本文的音乐是《人间不值得·Rhythm(《逆水寒》大宋新年特制版)》。

#播放音乐
print('播放音乐')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公众号\67.2023春节\万象凡音,雪糕超人耶 - 人间不值得·Rhythm(《逆水寒》大宋新年特制版).mp3") 
pygame.mixer.music.set_volume(0.5) 
pygame.mixer.music.play(1, 0)

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

图片

  
  

3 写2022年文字

  
然后设置画板的大小,并写开篇的文字。

t.title('阿黎逸阳的代码公众号')
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.bgcolor('red')
def write_1(x, y,  ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('white')
    t.write(ss, font=('Times New Roman', 70, 'normal'))
def write_2(x, y,  ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('white')
    t.write(ss, font=('Times New Roman', 35, 'normal'))
def write_3(x, y,  ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('white')
    t.write(ss, font=('Kai Ti', 12, 'normal', 'bold'))
def write_4(x, y,  ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('white')
    t.write(ss, font=('Times New Roman', 50, 'normal'))
write_1(-100, 100,  '2 0 2 2')
time.sleep(1)
write_4(-105, 10,  '皆为过往')
time.sleep(1)
write_4(-55, -80,  '翻篇!')
time.sleep(2)
t.clear()

关键代码详解:
  
t.setup():设置画布的尺寸和位置。
  
t.bgcolor(color):设置画布的背景颜色。
  
t.penup():抬起画笔,一般用于另起一个地方绘图使用。
  
t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。
  
t.pendown():放下画笔,一般和penup组合使用。
  
t.pencolor(color):设置画笔的颜色。
  
t.write():设置字体的大小、颜色、类型等。
  
time.sleep():睡眠一段时间。
  
t.clear():清屏。

  
  

4 画兔子的外轮廓

  
接着画兔子的外轮廓。

print('画右耳朵')
#画右耳朵
t.penup()
t.goto(20, -50)
t.pendown()     
t.pensize(2)
t.color('black', 'white')
t.begin_fill()
t.setheading(80)
t.circle(-100, 60)
t.circle(-50, 20)
t.right(85)
t.circle(-100, 65)
print('画右上脸')
#右上脸
t.setheading(-40)
t.circle(-80, 40)
print('画右下脸')
#右下脸
t.left(20)
t.circle(-50, 90)
print('画身子')
#手臂
t.setheading(-40)
t.circle(-100, 69)
#横线
t.setheading(180)
t.forward(170)
#手臂
t.setheading(110)
t.circle(-100, 69)
print('画左下脸')
#左下脸
t.left(115)
t.circle(-50, 90)
print('画左上脸')
#左上脸
t.setheading(85)
t.circle(-80, 40)
print('画左耳朵')
#左耳朵
t.setheading(140)
t.circle(-100, 55)
t.circle(-50, 20)
t.right(85)
t.circle(-100, 66)
print('画脑袋上的横线')
#脑袋上的横线
t.setheading(10)
t.circle(-100, 22)
t.end_fill()
print('画右内耳朵')

关键代码详解:
  
t.pensize(width):设置画笔的尺寸。
  
t.color(color1, color2):设置画笔的颜色和填充颜色。
  
t.left(degree):画笔向左转多少度,括号里表示度数。
  
t.right(degree):画笔向右转多少度,括号里表示度数。
  
t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。
  
画兔子外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得兔子的轮廓比较流畅。

  
  

5 画兔子的上半身

  
最后控制函数画兔子的上半身。

#画右内耳朵
t.penup()
t.goto(31, -50)
t.pendown()     
t.pensize(2)
t.color('pink')
t.begin_fill()
t.setheading(77)
t.circle(-100, 46)
t.circle(-50, 20)
t.right(105)
t.circle(-100, 55)
t.end_fill()
print('画左内耳朵')
#画左内耳朵
t.penup()
t.goto(-47, -55)
t.pendown()     
t.pensize(2)
t.color('pink')
t.begin_fill()
t.setheading(140)
t.circle(-100, 42)
t.circle(-50, 20)
t.right(105)
t.circle(-100, 54)
t.end_fill()
print('画左眼睛')
#画左眼睛
t.penup()
t.goto(-37, -110)
t.pendown()     
t.pensize(2)
t.color('black')
t.begin_fill()
t.circle(8, 360)
t.end_fill()
print('画右眼睛')
#画右眼睛
t.penup()
t.goto(23, -110)
t.pendown()     
t.pensize(2)
t.color('black')
t.begin_fill()
t.circle(8, 360)
t.end_fill()
print('画左眼珠')
#画左眼珠
t.penup()
t.goto(-34, -105)
t.pendown()     
t.pensize(2)
t.color('white')
t.begin_fill()
t.circle(2, 360)
t.end_fill()
print('画右眼珠')
#画右眼珠
t.penup()
t.goto(27, -105)
t.pendown()     
t.pensize(2)
t.color('white')
t.begin_fill()
t.circle(2, 360)
t.end_fill()
print('画鼻子')
#画鼻子
t.penup()
t.goto(-5, -125)
t.pendown()
t.color('black')
t.begin_fill()
t.setheading(45)
t.circle(-10, 100)
t.right(82)
t.circle(-10, 90)
t.end_fill()
print('画嘴')
#画嘴
t.penup()
t.color('black', 'red')
t.pensize(0.5)
t.goto(2, -130)
t.pendown()
t.begin_fill()
t.setheading(-90)
t.circle(-7, 95)
t.setheading(-80)
t.circle(8, 174)
t.setheading(193)
t.circle(-7, 100)
t.end_fill()
#画左嘴
t.penup()
t.goto(2, -130)
t.pendown()
t.pensize(2)
t.color('black')
t.setheading(-90)
t.circle(-8, 110)
#画右嘴
t.penup()
t.goto(2, -130)
t.pendown()
t.color('black')
t.setheading(-90)
t.circle(7, 122)

至此,在Python中实现画兔子的逻辑已大致讲解完毕,感兴趣的朋友可以尝试自己实现一下。
  
你可能感兴趣:
用Python绘制皮卡丘
用Python绘制词云图
用Python绘制520永恒心动
Python人脸识别—我的眼里只有你
Python画好看的星空图(唯美的背景)
【Python】情人节表白烟花(带声音和文字)
用Python中的py2neo库操作neo4j,搭建关联图谱
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿黎逸阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值