Python绘制白羊座卡通

有没有人和我一样,弄错过自己的星座?我以前一直以为自己是水瓶座,后来才发现星座是以阳历做区分的,实际我是白羊座……
  
本文介绍运用Python中的turtle库控制函数画白羊座卡通图像。

  

一、效果展示

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


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

  
  

二、代码详解

  
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库播放背景音乐。

#播放音乐
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 画白羊座卡通的脸和帽子

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

#画白羊座卡通
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 画白羊座卡通的耳朵和眼睛

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

#左边耳朵下面的尖尖
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 写文字

  
最后介绍写文字的代码。

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中实现画白羊座卡通的逻辑已大致讲解完毕,感兴趣的朋友可以尝试自己实现一下。
  

【限时免费进群】 在群内讨论学习Python、玩转Python、风控建模、人工智能学习、数据分析等内容,提供学习、就业信息,也可交流工作中遇到的相关问题。需要的朋友添加微信号19967879837,加时备注想进的群,比如风控建模。
  
你可能感兴趣:
用Python绘制皮卡丘
用Python绘制词云图
用Python绘制520永恒心动
Python人脸识别—我的眼里只有你
Python画好看的星空图(唯美的背景)
【Python】情人节表白烟花(带声音和文字)
用Python中的py2neo库操作neo4j,搭建关联图谱
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿黎逸阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值