찾다
백엔드 개발파이썬 튜토리얼Python+Turtle 라이브러리를 사용하여 스파이더맨을 그리는 단계

一、效果展示

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

Python+Turtle 라이브러리를 사용하여 스파이더맨을 그리는 단계

Pinstaller(Python打包为exe文件)

之前自己把 Python 文件打包成 exe 的时候,折腾了很久,本文将详细地讲述如何快速生成在不安装 Python 的电脑上也能执行的文件

1. 在 prompt 中运行 pip install pyinstaller , 安装 pyinstaller 库

Python+Turtle 라이브러리를 사용하여 스파이더맨을 그리는 단계

2.  在 prompt 中运行 where pyinstaller 

Python+Turtle 라이브러리를 사용하여 스파이더맨을 그리는 단계

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函数中把你想放音乐的电脑本地存放地址填进去。

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

Python+Turtle 라이브러리를 사용하여 스파이더맨을 그리는 단계

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+Turtle 라이브러리를 사용하여 스파이더맨을 그리는 단계의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
이 기사는 亿速云에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제
파이썬 : 자동화, 스크립팅 및 작업 관리파이썬 : 자동화, 스크립팅 및 작업 관리Apr 16, 2025 am 12:14 AM

파이썬은 자동화, 스크립팅 및 작업 관리가 탁월합니다. 1) 자동화 : 파일 백업은 OS 및 Shutil과 같은 표준 라이브러리를 통해 실현됩니다. 2) 스크립트 쓰기 : PSUTIL 라이브러리를 사용하여 시스템 리소스를 모니터링합니다. 3) 작업 관리 : 일정 라이브러리를 사용하여 작업을 예약하십시오. Python의 사용 편의성과 풍부한 라이브러리 지원으로 인해 이러한 영역에서 선호하는 도구가됩니다.

파이썬과 시간 : 공부 시간을 최대한 활용파이썬과 시간 : 공부 시간을 최대한 활용Apr 14, 2025 am 12:02 AM

제한된 시간에 Python 학습 효율을 극대화하려면 Python의 DateTime, Time 및 Schedule 모듈을 사용할 수 있습니다. 1. DateTime 모듈은 학습 시간을 기록하고 계획하는 데 사용됩니다. 2. 시간 모듈은 학습과 휴식 시간을 설정하는 데 도움이됩니다. 3. 일정 모듈은 주간 학습 작업을 자동으로 배열합니다.

파이썬 : 게임, Guis 등파이썬 : 게임, Guis 등Apr 13, 2025 am 12:14 AM

Python은 게임 및 GUI 개발에서 탁월합니다. 1) 게임 개발은 Pygame을 사용하여 드로잉, 오디오 및 기타 기능을 제공하며 2D 게임을 만드는 데 적합합니다. 2) GUI 개발은 Tkinter 또는 PYQT를 선택할 수 있습니다. Tkinter는 간단하고 사용하기 쉽고 PYQT는 풍부한 기능을 가지고 있으며 전문 개발에 적합합니다.

Python vs. C : 응용 및 사용 사례가 비교되었습니다Python vs. C : 응용 및 사용 사례가 비교되었습니다Apr 12, 2025 am 12:01 AM

Python은 데이터 과학, 웹 개발 및 자동화 작업에 적합한 반면 C는 시스템 프로그래밍, 게임 개발 및 임베디드 시스템에 적합합니다. Python은 단순성과 강력한 생태계로 유명하며 C는 고성능 및 기본 제어 기능으로 유명합니다.

2 시간의 파이썬 계획 : 현실적인 접근2 시간의 파이썬 계획 : 현실적인 접근Apr 11, 2025 am 12:04 AM

2 시간 이내에 Python의 기본 프로그래밍 개념과 기술을 배울 수 있습니다. 1. 변수 및 데이터 유형을 배우기, 2. 마스터 제어 흐름 (조건부 명세서 및 루프), 3. 기능의 정의 및 사용을 이해하십시오. 4. 간단한 예제 및 코드 스 니펫을 통해 Python 프로그래밍을 신속하게 시작하십시오.

파이썬 : 기본 응용 프로그램 탐색파이썬 : 기본 응용 프로그램 탐색Apr 10, 2025 am 09:41 AM

Python은 웹 개발, 데이터 과학, 기계 학습, 자동화 및 스크립팅 분야에서 널리 사용됩니다. 1) 웹 개발에서 Django 및 Flask 프레임 워크는 개발 프로세스를 단순화합니다. 2) 데이터 과학 및 기계 학습 분야에서 Numpy, Pandas, Scikit-Learn 및 Tensorflow 라이브러리는 강력한 지원을 제공합니다. 3) 자동화 및 스크립팅 측면에서 Python은 자동화 된 테스트 및 시스템 관리와 ​​같은 작업에 적합합니다.

2 시간 안에 얼마나 많은 파이썬을 배울 수 있습니까?2 시간 안에 얼마나 많은 파이썬을 배울 수 있습니까?Apr 09, 2025 pm 04:33 PM

2 시간 이내에 파이썬의 기본 사항을 배울 수 있습니다. 1. 변수 및 데이터 유형을 배우십시오. 이를 통해 간단한 파이썬 프로그램 작성을 시작하는 데 도움이됩니다.

10 시간 이내에 프로젝트 및 문제 중심 방법에서 컴퓨터 초보자 프로그래밍 기본 사항을 가르치는 방법?10 시간 이내에 프로젝트 및 문제 중심 방법에서 컴퓨터 초보자 프로그래밍 기본 사항을 가르치는 방법?Apr 02, 2025 am 07:18 AM

10 시간 이내에 컴퓨터 초보자 프로그래밍 기본 사항을 가르치는 방법은 무엇입니까? 컴퓨터 초보자에게 프로그래밍 지식을 가르치는 데 10 시간 밖에 걸리지 않는다면 무엇을 가르치기로 선택 하시겠습니까?

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기