IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 【Python画蜘蛛侠】你的DNA动了吗? -> 正文阅读

[Python知识库]【Python画蜘蛛侠】你的DNA动了吗?

蜘蛛侠(Spider-Man)即彼得·帕克(Peter Parker),是美国漫威漫画旗下超级英雄。由编剧斯坦·李和画家史蒂夫·迪特科联合创造,初次登场于《惊奇幻想》(Amazing Fantasy)第15期(1962年8月)。因为广受欢迎,几个月后,便开始拥有以自己为主角的单行本漫画。
??

网易云中关于蜘蛛侠主题曲热评过万的评论说到。蜘蛛侠之所以成为最受欢迎的超级英雄之一,是因为这面具下的人,不分肤色、种族、性别。。。他/她可能是你,是我,是和你一起生活的人。任何人都能带上这个面具,你也可以做到,如果你以前没有想过,希望现在可以了~
??

本文主要介绍运用python中的turtle库控制函数绘制蜘蛛侠。
??
在这里插入图片描述

??
??

一、效果展示

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


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

二、代码详解

??
Python绘制蜘蛛侠的原理是:应用turtle库绘制身体的不同部位。
??

1 导入库

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

# -*- coding: UTF-8 -*-
'''
代码用途 :画蜘蛛侠
作者     :阿黎逸阳
公众号     :  阿黎逸阳的代码
'''
import os
import pygame
import turtle as t 

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

os库可以设置文件读取的位置。

pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。

??

2 播放音乐

??
接着应用pygame库播放背景音乐,本文的音乐是《Sunflower》。

os.chdir(r'F:\公众号\56.蜘蛛侠')
#播放音乐
print('播放音乐')
pygame.mixer.init()
pygame.mixer.music.load("Cope - Sunflower (Original Version).mp3") 
pygame.mixer.music.set_volume(0.5) 
pygame.mixer.music.play(1, 10)

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

在这里插入图片描述

??
??

3 定义画蜘蛛侠上半身的函数

??
然后设置画板的大小,并定义绘制蜘蛛侠上半身的函数。

t.title('阿黎逸阳的代码公众号')
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
def up_body():
    #画头
    t.penup()
    t.goto(60, 200)
    t.pendown()
    t.pensize(1)
    t.color('black', 'red')
    t.begin_fill()
    t.setheading(60)
    t.circle(60, 30)
    t.left(4)
    t.circle(40, 173)
    t.left(4)
    t.circle(60, 30)
    #画脖子
    t.setheading(260)
    t.circle(30, 29)
    #画肩膀
    t.setheading(220)
    t.forward(30)
    #画手上肌肉
    t.setheading(150)
    t.circle(30, 130)
    #画胸部的内部线
    t.setheading(30)
    t.circle(-100, 13)
    t.setheading(270)
    t.circle(50, 40)
    t.setheading(255)
    t.circle(55, 40)
    t.circle(-40, 50)
    #画腰部的外横线
    t.setheading(0)
    t.forward(-7)
    t.setheading(270)
    t.forward(18)
    #画腰线
    t.setheading(-30)
    t.forward(50)
    t.setheading(15)
    t.forward(80)
    t.setheading(90)
    t.forward(22)
    #重复的地方
    #画衣服内轮廓
    t.setheading(190)
    t.forward(20)
    t.setheading(103)
    t.circle(-160, 41)
    #画手内轮廓
    t.setheading(5)
    t.circle(-80, 30)
    t.setheading(20)
    t.circle(30, 30)
    #重复的地方
    #手臂上肌肉
    t.setheading(70)
    t.circle(22, 150)
    t.setheading(150)
    t.forward(30)
    t.setheading(120)
    t.forward(15)
    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 left_hand():
    #画左手臂
    #画胸部的内部线
    t.penup()
    t.goto(-69, 134)
    t.color('black', 'blue')
    t.pendown()
    t.begin_fill()
    t.setheading(30)
    t.circle(-100, 13)
    t.setheading(270)
    t.circle(50, 40)
    t.setheading(255)
    t.circle(55, 40)
    t.circle(-40, 50)
    #画腰部的外横线
    t.setheading(0)
    t.forward(-8)
    t.setheading(90)
    t.circle(220, 18)
    t.setheading(-90)
    t.circle(-40, 50)
    t.setheading(-85)
    t.circle(-50, 50)
    t.setheading(135)
    t.circle(30, 40)
    t.setheading(95)
    t.circle(-50, 50)
    t.setheading(98)
    t.circle(-60, 51)
    t.end_fill()
def right_hand():
    #画右手臂
    #画衣服内轮廓
    t.penup()
    t.goto(80, 39)
    t.color('black', 'blue')
    t.pendown()
    t.begin_fill()
    t.setheading(190)
    t.forward(20)
    t.setheading(103)
    t.circle(-160, 41)
    #画手内轮廓
    t.setheading(5)
    t.circle(-80, 30)
    t.setheading(20)
    t.circle(30, 30)
    t.setheading(-20)
    t.circle(-55, 65)
    t.setheading(-30)
    t.circle(-50, 60)
    t.setheading(180)
    t.circle(30, 40)
    t.setheading(154)
    t.circle(-48, 60)
    t.setheading(164)
    t.circle(-50, 60)
    t.setheading(-90)
    t.circle(-40, 60)
    t.left(40)
    t.circle(150, 23)
    t.end_fill()
def left_wrist():
    #画左手腕
    t.penup()
    t.goto(-81, 37)
    t.color('black', 'red')
    t.pendown()
    t.begin_fill()
    t.setheading(135)
    t.circle(30, 40)
    t.setheading(-90)
    t.circle(-60, 30)
    t.setheading(-90)
    t.forward(20)
    t.setheading(-45)
    t.forward(12)
    t.circle(6, 180)
    t.setheading(-50)
    t.circle(5, 160)
    t.setheading(95)
    t.forward(10)
    t.setheading(135)
    t.forward(8)
    t.setheading(95)
    t.forward(6)
    t.setheading(35)
    t.circle(30, 10)
    t.left(10)
    t.circle(30, 27)
    t.end_fill()
    #画手腕上的线
    #横线
    #第一条横线
    t.penup()
    t.goto(-84, 30)
    t.color('black')
    t.pendown()
    t.setheading(145)
    t.circle(30, 36)
    #第二条横线
    t.penup()
    t.goto(-90, 22)
    t.color('black')
    t.pendown()
    t.setheading(185)
    t.circle(-30, 31)
    #第三条横线
    t.penup()
    t.goto(-83, 10)
    t.color('black')
    t.pendown()
    t.setheading(210)
    t.circle(-50, 31)
    #第四条横线
    t.penup()
    t.goto(-102, -10)
    t.color('black')
    t.pendown()
    t.setheading(50)
    t.circle(-20, 41)
    t.setheading(55)
    t.circle(-90, 8)
    #第一条竖线
    t.penup()
    t.goto(-105, 24)
    t.color('black')
    t.pendown()
    t.setheading(-95)
    t.circle(100, 20)
    #第二条竖线
    t.penup()
    t.goto(-87, 42)
    t.color('black')
    t.pendown()
    t.setheading(-110)
    t.forward(22)
    t.setheading(-63)
    t.circle(-50, 40)
def right_wrist():
    #画右手腕
    t.penup()
    t.goto(189, 57)
    t.color('black', 'red')
    t.pendown()
    t.begin_fill()
    t.setheading(180)
    t.circle(30, 40)
    t.setheading(-55)
    t.circle(-100, 10)
    t.circle(-20, 70)
    t.setheading(-90)
    t.forward(10)
    t.setheading(-0)
    t.forward(5)
    t.setheading(-85)
    t.forward(8)
    t.setheading(-20)
    t.circle(8, 60)
    t.setheading(-35)
    t.circle(8, 70)
    t.setheading(-15)
    t.circle(6, 70)
    t.setheading(60)
    t.circle(20, 80)
    t.setheading(115)
    t.circle(-100, 20)
    t.end_fill()
    #画第一条横线
    t.goto(191, 45)
    t.color('black')
    t.pendown()
    t.setheading(215)
    t.circle(-30, 34)
    #画第二条横线
    t.penup()
    t.goto(197, 29)
    t.color('black')
    t.pendown()
    t.setheading(215)
    t.circle(-30, 37)
    #画第三条横线
    t.penup()
    t.goto(174, 11)
    t.color('black')
    t.pendown()
    t.setheading(-0)
    t.circle(-30, 27)
    t.setheading(20)
    t.circle(-20, 27)
    t.setheading(40)
    t.circle(-30, 23)
    #画第一条竖线
    t.penup()
    t.goto(178, 55)
    t.color('black')
    t.pendown()
    t.setheading(-70)
    t.circle(-200, 9)
    t.setheading(-82)
    t.circle(-100, 18)
    #画第二条竖线
    t.penup()
    t.goto(185, 55)
    t.color('black')
    t.pendown()
    t.setheading(-70)
    t.circle(-200, 8)
    t.setheading(-68)
    t.circle(-80, 25)

??
??

5 定义画蜘蛛的函数

??
接着定义画蜘蛛的函数。

def spider():
    #画蜘蛛
    t.penup()
    t.goto(8, 146)
    t.color('black')
    t.pendown()
    t.begin_fill()
    t.setheading(-120)
    t.circle(40, 60)
    t.setheading(60)
    t.circle(40,60)
    t.end_fill()
    #画蜘蛛的脚
    #右边的脚1
    t.penup()
    t.goto(13, 129)
    t.color('black')
    t.pendown()
    t.setheading(30)
    t.forward(10)
    t.setheading(90)
    t.forward(15)
    #右边的脚2
    t.penup()
    t.goto(14, 125)
    t.color('black')
    t.pendown()
    t.setheading(30)
    t.forward(16)
    t.setheading(90)
    t.forward(17)
    #右边的脚3
    t.penup()
    t.goto(14, 124)
    t.color('black')
    t.pendown()
    t.setheading(-20)
    t.forward(16)
    t.setheading(-90)
    t.forward(17)
    #右边的脚4
    t.penup()
    t.goto(14, 120)
    t.color('black')
    t.pendown()
    t.setheading(-20)
    t.forward(10)
    t.setheading(-90)
    t.forward(15)
    #画蜘蛛的脚
    #左边的脚1
    t.penup()
    t.goto(3, 129)
    t.color('black')
    t.pendown()
    t.setheading(150)
    t.forward(10)
    t.setheading(90)
    t.forward(15)
    #右边的脚2
    t.penup()
    t.goto(2, 125)
    t.color('black')
    t.pendown()
    t.setheading(150)
    t.forward(16)
    t.setheading(90)
    t.forward(17)
    #右边的脚3
    t.penup()
    t.goto(2, 124)
    t.color('black')
    t.pendown()
    t.setheading(-170)
    t.forward(16)
    t.setheading(-99)
    t.forward(17)
    #右边的脚4
    t.penup()
    t.goto(3, 120)
    t.color('black')
    t.pendown()
    t.setheading(-170)
    t.forward(10)
    t.setheading(-90)
    t.forward(15)

??
??

6 调用函数绘制图形

??
最后调用函数绘制图形。

print('绘制上半身外轮廓')
up_body()
print('绘制右手')
right_hand()
print('绘制左手')
left_hand()
print('绘制左拳头')
left_wrist()
print('绘制右拳头')
right_wrist()
print('绘制蜘蛛')
spider()

至此,在Python中实现蜘蛛侠的绘制逻辑已大致讲解完毕。快喊你喜欢挑战的朋友一起把缺失部分补充完整吧,看看谁最先补完。
??
你可能感兴趣:
用Python绘制皮卡丘
用Python绘制词云图
用Python绘制520永恒心动
Python人脸识别—我的眼里只有你
Python画好看的星空图(唯美的背景)
【Python】情人节表白烟花(带声音和文字)
用Python中的py2neo库操作neo4j,搭建关联图谱
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-07-03 10:44:49  更:2022-07-03 10:45:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 10:17:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码