前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >白羊座

白羊座

作者头像
阿黎逸阳
发布2023-08-21 18:59:18
1590
发布2023-08-21 18:59:18
举报

有没有人和我一样,弄错过自己的星座?

我以前一直以为自己是水瓶座,后来才发现星座是以阳历做区分的,实际我是白羊座……

本文介绍运用Python中的turtle库控制函数画白羊座卡通图像。

一、效果展示

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

http://mpvideo.qpic.cn/0bc3jiaa6aaaayahhyrjnnsfaswdb5faadya.f10002.mp4?

注:如需全量直接可运行的代码,到公众号中回复“白羊座”即可免费获取

可以参考Pinstaller(Python打包为exe文件)一文把Python文件转化成exe,发给未安装Python的朋友。

二、代码详解

Python绘制白羊座卡通的原理是:应用turtle库控制函数绘制不同曲线构成效果图。

1 导入库

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

代码语言:javascript
复制
# -*- 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库播放背景音乐。

代码语言:javascript
复制
#播放音乐
print('播放音乐')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公众号\69.白羊座\白羊座.mp3") 
pygame.mixer.music.set_volume(0.5) 
pygame.mixer.music.play(1, 0)

这一部分的代码和整体代码是剥离的,可以选择在最开始放上该代码,也可以直接删除。

如果选择播放音乐,需要在代码music.load函数中把你想放音乐的电脑本地存放地址填进去。

有部分朋友对这一块有疑问,填充格式可参考如下图片:

3 画白羊座卡通的脸和帽子

然后设置画板的大小,画白羊座卡通的脸和帽子。

代码语言:javascript
复制
#画白羊座卡通
print('画白羊座卡通')
t.title('阿黎逸阳的代码公众号')
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.bgcolor('white')
#画脸
print('画脸')
t.penup()
t.goto(120, 100)
t.pendown()
t.color('black', 'pink')
t.pensize(2)
t.setheading(150)
t.circle(150, 60)
t.setheading(-80)
t.circle(75, 160)
#画帽子
print('画帽子')
t.penup()
t.goto(120, 100)
t.pendown()
t.color('black', 'mistyrose')
t.pensize(2)
t.setheading(150)
t.begin_fill()
t.circle(150, 60)
t.setheading(-80)
t.circle(75, 20)
t.setheading(200)
t.circle(-25, 160)
t.setheading(105)
t.circle(-100, 35)
t.setheading(38)
t.circle(-150, 76)
t.setheading(-75)
t.circle(-100, 35)
t.setheading(-45)
t.circle(-25, 160)
t.setheading(62)
t.circle(75, 20)
t.end_fill()

关键代码详解:

t.setup():设置画布的尺寸和位置。

t.bgcolor(color):设置画布的背景颜色。

t.penup():抬起画笔,一般用于另起一个地方绘图使用。

t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。

t.pendown():放下画笔,一般和penup组合使用。

t.pensize(width):设置画笔的尺寸。

t.color(color1, color2):设置画笔的颜色和填充颜色。

t.setheading(θ):设置画笔的初始方向。

t.begin_fill():开始填充颜色。

t.end_fill():结束填充颜色。

t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。

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

4 画白羊座卡通的耳朵和眼睛

接着画白羊座卡通的耳朵和眼睛。

代码语言:javascript
复制
#左边耳朵下面的尖尖
print('画左边耳朵下面的尖尖')
t.penup()
t.goto(-38, 158)
t.pendown()
t.color('black', 'yellow')
t.pensize(2)
t.begin_fill()
t.setheading(-60)
t.circle(90, 15)
t.setheading(60)
t.circle(90, 12)
t.end_fill()
#左边耳朵
print('画左边耳朵')
t.penup()
t.goto(0, 200)
t.pendown()
t.color('black', 'yellow')
t.pensize(2)
t.begin_fill()
t.setheading(200)
t.circle(-200, 20)
t.setheading(-70)
t.circle(50, 80)
t.right(12)
t.circle(35, 273)
t.end_fill()
#右边耳朵下面的尖尖
print('画右边耳朵下面的尖尖')
t.penup()
t.goto(138, 158)
t.pendown()
t.color('black', 'yellow')
t.pensize(2)
t.begin_fill()
t.setheading(-120)
t.circle(-90, 15)
t.setheading(105)
t.circle(90, 12)
t.end_fill()
#右边耳朵
print('画右边耳朵')
t.penup()
t.goto(100, 200)
t.pendown()
t.color('black', 'yellow')
t.pensize(2)
t.begin_fill()
t.setheading(-20)
t.circle(200, 20)
t.setheading(-110)
t.circle(-50, 80)
t.left(12)
t.circle(-35, 273)
t.end_fill()
#画左眼睛
print('画左眼睛')
t.penup()
t.goto(13, 80)
t.pendown()
t.color('black')
t.pensize(2)
t.begin_fill()
t.setheading(0)
t.circle(9, 360)
t.end_fill()
t.penup()
t.goto(11, 89)
t.pendown()
t.color('white')
t.pensize(2)
t.begin_fill()
t.setheading(0)
t.circle(2, 360)
t.end_fill()
#画右眼睛
print('画右眼睛')
t.penup()
t.goto(79, 80)
t.pendown()
t.color('black')
t.pensize(2)
t.begin_fill()
t.setheading(0)
t.circle(9, 360)
t.end_fill()
t.penup()
t.goto(81, 89)
t.pendown()
t.color('white')
t.pensize(2)
t.begin_fill()
t.setheading(0)
t.circle(2, 360)
t.end_fill()

关键代码详解:

t.left(degree):画笔向左转多少度,括号里表示度数。

t.right(degree):画笔向右转多少度,括号里表示度数。

5 写文字

最后介绍写文字的代码。

代码语言:javascript
复制
print('写文字')
def write_1(x, y,  ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('pink')
    t.write(ss, font=('Times New Roman', 50, 'normal'))
def write_2(x, y,  ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('pink')
    t.write(ss, font=('Times New Roman', 18, 'normal'))
while 1:
    write_1(-60, -135,  '白羊座')
    time.sleep(2)
    write_2(-53, -170,  '3月21日~4月20日')
    time.sleep(2)
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()
    t.undo()

关键代码详解:

t.write():设置字体的大小、颜色、类型等。

time.sleep():睡眠一段时间。

至此,在Python中实现画白羊座卡通的逻辑已大致讲解完毕,感兴趣的朋友可以尝试自己实现一下。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 阿黎逸阳的代码 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档