温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用python画一只帅气的皮卡丘

发布时间:2022-01-13 16:44:17 阅读:212 作者:iii 栏目:开发技术
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

今天小编给大家分享一下怎么用python画一只帅气的皮卡丘的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

为了访问 Python 库,您需要将它导入到您的 Python 环境中,使用以下命令将其导入 turtle到您的 Python 脚本中。

import turtle

首先,让我们创建一个 Screen 实例。

wn = turtle.Screen()

现在,创建一个 Turtle 实例。

self.t = turtle.Turtle()

让我们将速度设置为 3 使用 speed 方法,这意味着皮卡丘不会只是出现在屏幕上,绘图也会有一些动画。如果您想更改背景颜色,可以使用该 bgcolor 方法,默认为白色。您可以使用pensize 方法调整笔的粗细 ,它会稍微加粗。ondrag 拖动 Turtle 将在屏幕上移动它,从而产生手绘(如果笔已放下)。

t.pensize(3)
t.speed(9)
t.ondrag(getPosition)

现在让我们看看这个程序中使用的一些常用方法:

Turtle(): 用于创建并返回一个新的海龟对象。

forward(value): 相对于指定的值,海龟向前移动。

向后(值): 相对于指定的值,海龟向后移动。

right(angle): 乌龟顺时针转动。

left(angle): 海龟逆时针旋转。

penup(): 捡起乌龟笔。

pendown(): 乌龟笔放下。

up(): 与 penup() 相同。

down(): 与 pendown() 相同。

color(颜色名称): Turtle 笔的颜色发生变化。

fillcolor(颜色名称): 用于填充特定形状的颜色已更改。

标题(): 返回当前标题。

position(): 返回当前位置。

goto(x, y): 将海龟的位置移动到坐标 x, y。

end_fill(): 关闭多边形后填充当前填充颜色。

begin_fill(): 记住填充多边形的起点。

dot(): 点留在当前位置。

stamp(): 海龟形状的印象留在当前位置。

Shape(): 应该是 – ‘turtle’, ‘classic’, ‘arrow’ 或 ‘circle’。

上代码:

import turtle


def getPosition(x, y):
    turtle.setx(x)
    turtle.sety(y)
    print(x, y)


class Pikachu:

    def __init__(self):
        self.t = turtle.Turtle()
        t = self.t
        t.pensize(3)
        t.speed(9)
        t.ondrag(getPosition)

    def noTrace_goto(self, x, y):
        self.t.penup()
        self.t.goto(x, y)
        self.t.pendown()

    def leftEye(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.seth(0)
        t.fillcolor('#333333')
        t.begin_fill()
        t.circle(22)
        t.end_fill()

        self.noTrace_goto(x, y + 10)
        t.fillcolor('#000000')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

        self.noTrace_goto(x + 6, y + 22)
        t.fillcolor('#ffffff')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

    def rightEye(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.seth(0)
        t.fillcolor('#333333')
        t.begin_fill()
        t.circle(22)
        t.end_fill()

        self.noTrace_goto(x, y + 10)
        t.fillcolor('#000000')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

        self.noTrace_goto(x - 6, y + 22)
        t.fillcolor('#ffffff')
        t.begin_fill()
        t.circle(10)
        t.end_fill()

    def mouth(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t

        t.fillcolor('#88141D')
        t.begin_fill()

        # Lower Lip
        l1 = []
        l2 = []
        t.seth(190)
        a = 0.7
        for i in range(28):
            a += 0.1
            t.right(3)
            t.fd(a)
            l1.append(t.position())

        self.noTrace_goto(x, y)

        t.seth(10)
        a = 0.7
        for i in range(28):
            a += 0.1
            t.left(3)
            t.fd(a)
            l2.append(t.position())

        # Upper Lip
        t.seth(10)
        t.circle(5015)
        t.left(180)
        t.circle(-5015)

        t.circle(-5040)
        t.seth(233)
        t.circle(-5055)
        t.left(180)
        t.circle(5012.1)
        t.end_fill()

        # Tongue
        self.noTrace_goto(1754)
        t.fillcolor('#DD716F')
        t.begin_fill()
        t.seth(145)
        t.circle(4086)
        t.penup()
        for pos in reversed(l1[:20]):
            t.goto(pos[0], pos[1] + 1.5)
        for pos in l2[:20]:
            t.goto(pos[0], pos[1] + 1.5)
        t.pendown()
        t.end_fill()

        # Nose
        self.noTrace_goto(-1794)
        t.seth(8)
        t.fd(4)
        t.back(8)

    # Red Cheeks
    def leftCheek(self, x, y):
        turtle.tracer(False)
        t = self.t
        self.noTrace_goto(x, y)
        t.seth(300)
        t.fillcolor('#DD4D28')
        t.begin_fill()
        a = 2.3
        for i in range(120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
                t.lt(3)
                t.fd(a)
            else:
                a += 0.05
                t.lt(3)
                t.fd(a)
        t.end_fill()
        turtle.tracer(True)

    def rightCheek(self, x, y):
        t = self.t
        turtle.tracer(False)
        self.noTrace_goto(x, y)
        t.seth(60)
        t.fillcolor('#DD4D28')
        t.begin_fill()
        a = 2.3
        for i in range(120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
                t.lt(3)
                t.fd(a)
            else:
                a += 0.05
                t.lt(3)
                t.fd(a)
        t.end_fill()
        turtle.tracer(True)

    def colorLeftEar(self, x, y):
        t = self.t
        self.noTrace_goto(x, y)
        t.fillcolor('#000000')
        t.begin_fill()
        t.seth(330)
        t.circle(10035)
        t.seth(219)
        t.circle(-30019)
        t.seth(110)
        t.circle(-3050)
        t.circle(-30010)
        t.end_fill()

    def colorRightEar(self, x, y):
        t = self.t
        self.noTrace_goto(x, y)
        t.fillcolor('#000000')
        t.begin_fill()
        t.seth(300)
        t.circle(-10030)
        t.seth(35)
        t.circle(30015)
        t.circle(3050)
        t.seth(190)
        t.circle(30017)
        t.end_fill()

    def body(self):
        t = self.t

        t.fillcolor('#F6D02F')
        t.begin_fill()
        # Right face contour
        t.penup()
        t.circle(13040)
        t.pendown()
        t.circle(100105)
        t.left(180)
        t.circle(-1005)

        # Right ear
        t.seth(20)
        t.circle(30030)
        t.circle(3050)
        t.seth(190)
        t.circle(30036)

        # Upper profile
        t.seth(150)
        t.circle(15070)

        # Left ear
        t.seth(200)
        t.circle(30040)
        t.circle(3050)
        t.seth(20)
        t.circle(30035)
        #print(t.pos())

        # Left face contour
        t.seth(240)
        t.circle(10595)
        t.left(180)
        t.circle(-1055)

        # Left hand
        t.seth(210)
        t.circle(50018)
        t.seth(200)
        t.fd(10)
        t.seth(280)
        t.fd(7)
        t.seth(210)
        t.fd(10)
        t.seth(300)
        t.circle(1080)
        t.seth(220)
        t.fd(10)
        t.seth(300)
        t.circle(1080)
        t.seth(240)
        t.fd(12)
        t.seth(0)
        t.fd(13)
        t.seth(240)
        t.circle(1070)
        t.seth(10)
        t.circle(1070)
        t.seth(10)
        t.circle(30018)

        t.seth(75)
        t.circle(5008)
        t.left(180)
        t.circle(-50015)
        t.seth(250)
        t.circle(10065)

        # Left foot
        t.seth(320)
        t.circle(1005)
        t.left(180)
        t.circle(-1005)
        t.seth(220)
        t.circle(20020)
        t.circle(2070)

        t.seth(60)
        t.circle(-10020)
        t.left(180)
        t.circle(10020)
        t.seth(300)
        t.circle(1070)

        t.seth(60)
        t.circle(-10020)
        t.left(180)
        t.circle(10020)
        t.seth(10)
        t.circle(10060)

        # Horizontal
        t.seth(180)
        t.circle(-10010)
        t.left(180)
        t.circle(10010)
        t.seth(5)
        t.circle(10010)
        t.circle(-10040)
        t.circle(10035)
        t.left(180)
        t.circle(-10010)

        # Right foot
        t.seth(290)
        t.circle(10055)
        t.circle(1050)

        t.seth(120)
        t.circle(10020)
        t.left(180)
        t.circle(-10020)

        t.seth(0)
        t.circle(1050)

        t.seth(110)
        t.circle(10020)
        t.left(180)
        t.circle(-10020)

        t.seth(30)
        t.circle(2050)

        t.seth(100)
        t.circle(10040)

        # Right body contour
        t.seth(200)
        t.circle(-1005)
        t.left(180)
        t.circle(1005)
        t.left(30)
        t.circle(10075)
        t.right(15)
        t.circle(-30021)
        t.left(180)
        t.circle(3003)

        # Right hand
        t.seth(43)
        t.circle(20060)

        t.right(10)
        t.fd(10)

        t.circle(5160)
        t.seth(90)
        t.circle(5160)
        t.seth(90)

        t.fd(10)
        t.seth(90)
        t.circle(5180)
        t.fd(10)

        t.left(180)
        t.left(20)
        t.fd(10)
        t.circle(5170)
        t.fd(10)
        t.seth(240)
        t.circle(5030)

        t.end_fill()
        self.noTrace_goto(130125)
        t.seth(-20)
        t.fd(5)
        t.circle(-5160)
        t.fd(5)

        # Fingers
        self.noTrace_goto(166130)
        t.seth(-90)
        t.fd(3)
        t.circle(-4180)
        t.fd(3)
        t.seth(-90)
        t.fd(3)
        t.circle(-4180)
        t.fd(3)

        # Tail
        self.noTrace_goto(168134)
        t.fillcolor('#F6D02F')
        t.begin_fill()
        t.seth(40)
        t.fd(200)
        t.seth(-80)
        t.fd(150)
        t.seth(210)
        t.fd(150)
        t.left(90)
        t.fd(100)
        t.right(95)
        t.fd(100)
        t.left(110)
        t.fd(70)
        t.right(110)
        t.fd(80)
        t.left(110)
        t.fd(30)
        t.right(110)
        t.fd(32)

        t.right(106)
        t.circle(10025)
        t.right(15)
        t.circle(-3002)
        #print(t.pos())
        t.seth(30)
        t.fd(40)
        t.left(100)
        t.fd(70)
        t.right(100)
        t.fd(80)
        t.left(100)
        t.fd(46)
        t.seth(66)
        t.circle(20038)
        t.right(10)
        t.fd(10)
        t.end_fill()

        # Tail Pattern
        t.fillcolor('#923E24')
        self.noTrace_goto(126.82, -156.84)
        t.begin_fill()

        t.seth(30)
        t.fd(40)
        t.left(100)
        t.fd(40)
        t.pencolor('#923e24')
        t.seth(-30)
        t.fd(30)
        t.left(140)
        t.fd(20)
        t.right(150)
        t.fd(20)
        t.left(150)
        t.fd(20)
        t.right(150)
        t.fd(20)
        t.left(130)
        t.fd(18)
        t.pencolor('#000000')
        t.seth(-45)
        t.fd(67)
        t.right(110)
        t.fd(80)
        t.left(110)
        t.fd(30)
        t.right(110)
        t.fd(32)
        t.right(106)
        t.circle(10025)
        t.right(15)
        t.circle(-3002)
        t.end_fill()

        # Hat, Eye, Mouth, Cheek
        self.cap(-134.07147.81)
        self.mouth(-525)
        self.leftCheek(-12632)
        self.rightCheek(10763)
        self.colorLeftEar(-250100)
        self.colorRightEar(140270)
        self.leftEye(-8590)
        self.rightEye(50110)
        t.hideturtle()

    def cap(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.fillcolor('#CD0000')
        t.begin_fill()
        t.seth(200)
        t.circle(4007)
        t.left(180)
        t.circle(-40030)
        t.circle(3060)
        t.fd(50)
        t.circle(3045)
        t.fd(60)
        t.left(5)
        t.circle(3070)
        t.right(20)
        t.circle(20070)
        t.circle(3060)
        t.fd(70)
        #print(t.pos())
        t.right(35)
        t.fd(50)
        t.circle(8100)
        t.end_fill()
        self.noTrace_goto(-168.47185.52)
        t.seth(36)
        t.circle(-27054)
        t.left(180)
        t.circle(27027)
        t.circle(-8098)

        t.fillcolor('#444444')
        t.begin_fill()
        t.left(180)
        t.circle(80197)
        t.left(58)
        t.circle(20045)
        t.end_fill()

        self.noTrace_goto(-58270)
        t.pencolor('#228B22')
        t.dot(35)

        self.noTrace_goto(-30280)
        t.fillcolor('#228B22')
        t.begin_fill()
        t.seth(100)
        t.circle(30180)
        t.seth(190)
        t.fd(15)
        t.seth(100)
        t.circle(-45180)
        t.right(90)
        t.fd(15)
        t.end_fill()
        t.pencolor('#000000')

    def start(self):
        self.body()


def main():
    print('Painting the Pikachu... ')
    wn = turtle.Screen()
    wn.setup(width=600, height=800)
    pikachu = Pikachu()
    pikachu.start()
    turtle.mainloop()


if __name__ == '__main__':
    main()

以上就是“怎么用python画一只帅气的皮卡丘”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×