Python+Turtle绘制蜘蛛侠的示例代码

目录
  • 一、效果展示
  • 二、代码详解
    • 1.导入库
    • 2.播放音乐
    • 3.定义画蜘蛛侠上半身的函数
    • 4.定义画左手和右手的函数
    • 5.定义画蜘蛛的函数
    • 6.调用函数绘制图形

蜘蛛侠(Spider-Man)即彼得·帕克(Peter Parker),是美国漫威漫画旗下超级英雄。

由编剧斯坦·李和画家史蒂夫·迪特科联合创造,初次登场于《惊奇幻想》(Amazing Fantasy)第15期(1962年8月).

因为广受欢迎,几个月后,便开始拥有以自己为主角的单行本漫画。

网易云中关于蜘蛛侠主题曲热评过万的评论说到。

蜘蛛侠之所以成为最受欢迎的超级英雄之一,是因为这面具下的人,不分肤色、种族、性别。。。

他/她可能是你,是我,是和你一起生活的人。

任何人都能带上这个面具,你也可以做到,如果你以前没有想过,希望现在可以了~

本文主要介绍运用python中的turtle库控制函数绘制蜘蛛侠。

一、效果展示

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

可以参考下面步骤把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 

本文应用到的库较少,只应用了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+Turtle绘制蜘蛛侠的示例代码的详细内容,更多关于Python Turtle绘制蜘蛛侠的资料请关注我们其它相关文章!

(0)

相关推荐

  • Python+Turtle绘制一个可爱的生日蛋糕

    每当有朋友过生日时,生日蛋糕自然是必不可少的,今天我们来看一下如何用 Python 画一个生日蛋糕. 本文我们用到的 Python 库包括:turtle.math 和 random. 实现的主要代码如下: import math as m import random as r import turtle as t t.speed(0) t.delay(0) # 设置背景颜色及窗口 t.bgcolor("#FFFFFF") t.setup(800, 600) t.penup() t.go

  • Python+Turtle绘制可爱的可达鸭

    目录 一.效果展示 二.代码详解 1.导入库 2.播放音乐 3.画可达鸭的头和身体外轮廓 4.画眼睛 5.画手 6.画嘴和脚 7.画头发 8.写文字 一年一度的六一儿童节又来了,祝大朋友小朋友节日快乐. 你有没有一瞬间,特别想要回到童年? 童年是一盒水彩笔,五颜六色精彩纷呈.童年是一幅漫画,新奇幻想思绪缤纷.童年是用水彩笔绘出的一幅漫画,一个追风的少年. 童年的时光总是安静又美好,我们总是盼望着长大,憧憬着未来的生活. 长大后,我们又总是在怀念,怀念过去简单又纯粹的自己. 本文主要介绍运用tur

  • Python+Turtle绘制可爱的多啦A梦的示例代码

    目录 1 送她的多啦A梦 2 白驹过隙 3 Python代码实现 1 送她的多啦A梦 一个哆啦A梦让她开心开心好久好久.我也很开心,昨天送了一个实体模型,今天用Python代码再弄一个送给她. 哆啦A梦(日语:ドラえもん,英语:Doraemon),旧译为机器猫,日本漫画<多啦A梦>及其衍生作品中的猫型机器人,本作的主人公.名字的意思是铜锣(ドラ)卫门(えもん). 哆啦A梦肚子上拥有四次元口袋,这个口袋直接通往四次元空间,再多的东西也放得下.害怕老鼠.平时的职责是照顾野比大雄. 2 白驹过隙 虽

  • Python+Turtle绘制航海王草帽路飞详解

    目录 一.程序运行 1.效果展示-轮廓描绘 2.效果展示-颜色填充 二.实现过程 1.绘图数据下载 2.海龟绘图配置项 3.轮廓绘制 4.颜色填充:衣服.裤子 5.颜色填充:草帽.腰带 6.完整源码 一.程序运行 1.效果展示 - 轮廓描绘 看轮廓描绘效果: 2.效果展示 - 颜色填充 衣服和裤子颜色填充效果: 二.实现过程 1.绘图数据下载 获取地址 内容预览: 2.海龟绘图配置项 降低刷新率可提升绘制速度,值越大刷新频率越低,速度越快 t.tracer(5000) def set_trutl

  • Python利用Turtle绘制哆啦A梦和小猪佩奇

    目录 1.哆啦A梦 2.小猪佩奇 3.Python代码实现(哆啦A梦) 4.Python代码实现(小猪佩奇 ) 1.哆啦A梦 “只要把愿望系在竹竿上请求月亮女神,心愿便能达成”.我超喜欢这句话. 哆啦A梦的创造要追溯到1969年的某个截稿日,作者藤子·F·不二雄的家里突然闯进了一只小猫,虽然很快就要截稿了,但作者还是和小猫玩了起来,还替小猫挠虱子,而这一挠就是几个小时.等作者发现时间不够用的时候,已经来不及完成稿子.这时作者像热锅上的蚂蚁走来走去,突然踢到了女儿的不倒翁玩具,于是作者灵光一现,把

  • Python使用turtle库绘制小猪佩奇(实例代码)

    turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制.turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言. turtle库绘制图形有一个基本框架:一个小海龟在坐标系中爬行,其爬行轨迹形成了绘制图形.刚开始绘制时,小海龟位于画布正中央,此处坐标为(0,0),前进方向为水平右方. 在Python3系列版本安装目录的Lib文件夹下可以找到turtle.py文件. 下面通过代码给大家介绍Python使用turtle库绘制小猪佩奇, 具体代码如下所示: # -*

  • Python+Turtle绘制蜘蛛侠的示例代码

    目录 一.效果展示 二.代码详解 1.导入库 2.播放音乐 3.定义画蜘蛛侠上半身的函数 4.定义画左手和右手的函数 5.定义画蜘蛛的函数 6.调用函数绘制图形 蜘蛛侠(Spider-Man)即彼得·帕克(Peter Parker),是美国漫威漫画旗下超级英雄. 由编剧斯坦·李和画家史蒂夫·迪特科联合创造,初次登场于<惊奇幻想>(Amazing Fantasy)第15期(1962年8月). 因为广受欢迎,几个月后,便开始拥有以自己为主角的单行本漫画. 网易云中关于蜘蛛侠主题曲热评过万的评论说到

  • Python+Turtle绘制幸运草的示例代码

    目录 一.效果展示 二.代码详解 1.导入库 2.播放音乐 3.定义画四叶草的函数 4.调用函数绘制四叶草 5.设置写文字的函数 幸运草又名四叶草,一般指四叶的苜蓿.或车轴草. 在十万株苜蓿草中,你可能只会发现一株是四叶草,机会率大约是十万分之一. 因此四叶草是国际公认的幸运象征. 本文主要介绍运用turtle库控制函数绘制四叶草,希望见者皆好运,祝福大家的生活都能幸福安康. 一.效果展示 在介绍代码之前,先来看下本文的实现效果. 可以把视频中的名字替换成你所想的名字,并参考下面步骤把Pytho

  • Python+turtle绘制对称图形的示例代码

    目录 1.图1 2.图2 3.图3 4.图4 5.图5 6.图6 最近有个朋友,想要我帮忙用python画几个图,在画的过程中觉得有些图还挺有意思的,分享给大家. 1.图1 第一个图是由三角形组成的花,感兴趣的小伙伴可以自己尝试在python中用turtle库绘制一下. 具体代码如下: # -*- coding: UTF-8 -*- ''' 代码用途 :画对称图形 作者 :阿黎逸阳 博客 : https://blog.csdn.net/qq_32532663/article/details/10

  • Python利用Turtle绘制Technoblade的示例代码

    在刚过去不久的6月30日那天,国外一位在YouTube拥有上千万粉丝的我的世界游戏主播Technoblade因癌症与世长辞,年仅23岁,他并没有离开我们,只是用另外一种方式活在了这个世界上. 为了纪念他,特地写了这篇文章,教大家用Turtle绘制出Technoblade,运行效果如下: 代码如下: 首先,用坐标表示每个像素块的颜色,定义一个列表 POS=[ [4,0,(213,164,9)], [6,0,(213,164,9)], [9,0,(213,164,9)], [11,0,(213,16

  • 通过Python绘制中国结的示例代码

    目录 1 中国结的组成部分 2 设计中国结对象 3 绘制结体 4 绘制耳翼 5 绘制挂耳和流苏 6 完整代码,一键运行 1 中国结的组成部分 中国结是一种手工编织工艺品,它身上所显示的情致与智慧正是汉族古老文明中的一个侧面.因为其外观对称精致,可以代表汉族悠久的历史,符合中国传统装饰的习俗和审美观念,故命名为中国结.中国结代表着团结幸福平安,特别是在民间,它精致的做工深受大众的喜爱.其主要组成部分如下图所示. 2 设计中国结对象 基于Python Turtle库实现绘制,首先设计一个中国结对象,

  • Python绘制灯笼的示例代码

    目录 一.效果展示 二.代码展示 三.拓展 一年一度的元宵节刚刚过去,由于时间关系,在元宵节当天晚上11点多才完成本文灯笼的绘制.这两天又在忙着别的事情,所以现在才跟大家分享. 一.效果展示 在介绍代码之前,先来看下本文的实现效果. 视频链接 二.代码展示 接下来展示绘制灯笼的全量源代码 import os import pygame import turtle as t ##画外轮廓 t.title('元宵节字谜灯笼') t.setup(startx=0, starty = 0) #画灯笼提线

  • Python绘制时钟的示例代码

    目录 导入需要的包设置变量 写数字 绘制时针 完整代码 导入需要的包设置变量 from datetime import datetime from pygame.locals import * import sys, math, pygame def print_text(font, x, y, text, color=(255, 255, 255)): img_text = font.render(text, True, color) screen.blit(img_text, (x, y))

  • python 动态绘制爱心的示例

    代码 import turtle turtle.bgcolor("black") turtle.pensize(2) sizeh = 1.2 def curve(): for ii in range(200): turtle.right(1) turtle.forward(1 * sizeh) turtle.speed(0) turtle.color("red", "red") turtle.begin_fill() turtle.left(14

  • python+matplotlib绘制3D条形图实例代码

    本文分享的实例主要实现的是Python+matplotlib绘制一个有阴影和没有阴影的3D条形图,具体如下. 首先看看演示效果: 完整代码如下: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # setup the figure and axes fig = plt.figure(figsize=(8, 3)) ax1 = fig.add_subplot(121

随机推荐