小工具知识网

当前位置: 首页 > 行业知识 > Python-5个有趣的小程序【每一个都附完整源码】

行业知识

Python-5个有趣的小程序【每一个都附完整源码】

2026-10-21 13:54:43 来源:互联网转载

一:樱花树

运行index.php/tags-41973.html" >���果:

完整代码如下:

import turtleimport randomfrom turtle import *from time import sleep# 画樱花的躯干(60,t)def tree(branchLen,t):    sleep(0.0005)    if branchLen >3:        if 8 3:                c = 0                r += 1        self.entry = tk.Entry(self, width=24, borderwidth=2,                              bg="yellow", font=("Consolas", 12))        self.entry.grid(row=0, column=0, columnspan=4, padx=8, pady=6)    def click(self, key):        """响应按钮"""        if key == "=":  # 输出结果            result = eval(self.entry.get())            self.entry.insert(tk.END, " = " + str(result))        elif key == "C":  # 清空输入框            self.entry.delete(0, tk.END)        elif key == "->M":  # 存入数值            self.memory = self.entry.get()            if "=" in self.memory:                ix = self.memory.find("=")                self.memory = self.memory[ix + 2:]            self.title("M=" + self.memory)        elif key == "M->":  # 取出数值            if self.memory:                self.entry.insert(tk.END, self.memory)        elif key == "+/-":  # 正负翻转            if "=" in self.entry.get():                self.entry.delete(0, tk.END)            elif self.entry.get()[0] == "-":                self.entry.delete(0)            else:                self.entry.insert(0, "-")        else:  # 其他键            if "=" in self.entry.get():                self.entry.delete(0, tk.END)            self.entry.insert(tk.END, key)if __name__ == "__main__":    Calc().mainloop()

四:皮卡丘

运行效果:

完整代码如下:

from turtle import *'''绘制皮卡丘头部'''def face(x,y):    """画脸"""    begin_fill()    penup()    # 将海龟移动到指定的坐标    goto(x, y)    pendown()    # 设置海龟的方向    setheading(40)    circle(-150, 69)    fillcolor("#FBD624")    # 将海龟移动到指定的坐标        penup()    goto(53.14, 113.29)    pendown()    setheading(300)    circle(-150, 30)    setheading(295)    circle(-140, 20)    print(position())    forward(5)    setheading(260)    circle(-80, 70)    print(position())    penup()    goto(-74.43,-79.09)    pendown()    penup()    # 将海龟移动到指定的坐标    goto(-144,103)    pendown()    setheading(242)    circle(110, 35)    right(10)    forward(10)    setheading(250)    circle(80, 115)    print(position())    penup()    goto(-74.43,-79.09)    pendown()    setheading(10)    penup()    goto(-144, 103)    pendown()    penup()    goto(x, y)    pendown()    end_fill()    # 下巴    penup()    goto(-50, -82.09)    pendown()    pencolor("#DDA120")    fillcolor("#DDA120")    begin_fill()    setheading(-12)    circle(120, 25)    setheading(-145)    forward(30)    setheading(180)    circle(-20, 20)    setheading(143)    forward(30)    end_fill()def eye():    """画眼睛"""    # 左眼    color("black","black")    penup()    goto(-110, 27)    pendown()    begin_fill()    setheading(0)    circle(24)    end_fill()    # 左眼仁    color("white", "white")    penup()    goto(-105, 51)    pendown()    begin_fill()    setheading(0)    circle(10)    end_fill()    # 右眼    color("black", "black")    penup()    goto(25, 40)    pendown()    begin_fill()    setheading(0)    circle(24)    end_fill()    # 右眼仁    color("white", "white")    penup()    goto(17, 62)    pendown()    begin_fill()    setheading(0)    circle(10)    end_fill()def cheek():    """画脸颊"""    # 右边    color("#9E4406", "#FE2C21")    penup()    goto(-130, -50)    pendown()    begin_fill()    setheading(0)    circle(27)    end_fill()    # 左边    color("#9E4406", "#FE2C21")    penup()    goto(53, -20)    pendown()    begin_fill()    setheading(0)    circle(27)    end_fill()def nose():    """画鼻子"""    color("black", "black")    penup()    goto(-40, 38)    pendown()    begin_fill()    circle(7,steps = 3)    end_fill()def mouth():    """画嘴"""    color("black", "#F35590")    # 嘴唇    penup()    goto(-10, 22)    pendown()    begin_fill()    setheading(260)    forward(60)    circle(-11, 150)    forward(55)    print(position())    penup()    goto(-38.46, 21.97)    pendown()    end_fill()    # 舌头    color("#6A070D", "#6A070D")    begin_fill()    penup()    goto(-10.00, 22.00)    pendown()    penup()    goto(-14.29, -1.7)    pendown()    penup()    goto(-52, -5)    pendown()    penup()    goto(-60.40, 12.74)    pendown()    penup()    goto(-38.46, 21.97)    pendown()    penup()    goto(-10.00, 22.00)    pendown()    end_fill()    color("black","#FFD624")    penup()    goto(-78, 15)    pendown()    begin_fill()    setheading(-25)    for i in range(2):        setheading(-25)        circle(35, 70)    end_fill()    color("#AB1945", "#AB1945")    penup()    goto(-52, -5)    pendown()    begin_fill()    setheading(40)    circle(-33, 70)    goto(-16,-1.7)    penup()    goto(-18,-17)    pendown()    setheading(155)    circle(25, 70)    end_fill()def ear():    """画耳朵"""    # 左耳    color("black","#FFD624")    penup()    goto(-145, 93)    pendown()    begin_fill()    setheading(165)    circle(-248,50)    right(120)    circle(-248,50)    end_fill()    color("black", "black")    penup()    goto(-240, 143)    pendown()    begin_fill()    setheading(107)    circle(-170, 25)    left(80)    circle(229, 15)    left(120)    circle(300, 15)    end_fill()    # 右耳    color("black", "#FFD624")    penup()    goto(30, 136)    pendown()    begin_fill()    setheading(64)    circle(-248, 50)    right(120)    circle(-248, 50)    end_fill()    color("black", "black")    penup()    goto(160, 200)    pendown()    begin_fill()    setheading(52)    circle(170, 25)    left(116)    circle(229, 15)    left(71)    circle(-300, 15)    end_fill()def setting():    """设置参数"""    pensize(2)    # 隐藏海龟    hideturtle()    speed(10)def main():    """主函数"""    setting()    face(-132,115)    eye()    cheek()    nose()    mouth()    ear()    done()if __name__ == '__main__':    main()

五:表白专用

运行效果:

完整代码如下:

import turtleimport time# 清屏函数def clear_all():    turtle.penup()    turtle.goto(0, 0)    turtle.color('white')    turtle.pensize(800)    turtle.pendown()    turtle.setheading(0)    turtle.fd(300)    turtle.bk(600) # 重定位海龟的位置def go_to(x, y, state):    turtle.pendown() if state else turtle.penup()    turtle.goto(x, y)# 画线# state为真时海龟回到原点,为假时不回到原来的出发点def draw_line(length, angle, state):    turtle.pensize(1)    turtle.pendown()    turtle.setheading(angle)    turtle.fd(length)    turtle.bk(length) if state else turtle.penup()    turtle.penup()# 画箭羽def draw_feather(size):    angle = 30                          # 箭的倾角    feather_num = size//6               # 羽毛的数量    feather_length = size // 3          # 羽毛的长度    feather_gap = size//10              # 羽毛的间隔    for i in range(feather_num):        draw_line(feather_gap, angle+180, False)            # 箭柄,不折返        draw_line(feather_length, angle + 145, True)        # 羽翼,要折返    draw_line(feather_length, angle + 145, False)    draw_line(feather_num*feather_gap, angle, False)    draw_line(feather_length, angle + 145 + 180, False)    for i in range(feather_num):        draw_line(feather_gap, angle+180, False)            # 箭柄,不折返        draw_line(feather_length, angle - 145, True)        # 羽翼,要折返    draw_line(feather_length, angle - 145, False)    draw_line(feather_num*feather_gap, angle, False)    draw_line(feather_length, angle - 145 + 180, False) # 画爱心def draw_heart(size):    turtle.color('red', 'pink')    turtle.pensize(2)    turtle.pendown()    turtle.setheading(150)    turtle.begin_fill()    turtle.fd(size)    turtle.circle(size * -3.745, 45)    turtle.circle(size * -1.431, 165)    turtle.left(120)    turtle.circle(size * -1.431, 165)    turtle.circle(size * -3.745, 45)    turtle.fd(size)    turtle.end_fill()# 画箭def draw_arrow(size):    angle = 30    turtle.color('black')    draw_feather(size)    turtle.pensize(4)    turtle.setheading(angle)    turtle.pendown()    turtle.fd(size*2) # 一箭穿心# 箭的头没有画出来,而是用海龟来代替def arrow_heart(x, y, size):    go_to(x, y, False)    draw_heart(size*1.15)    turtle.setheading(-150)    turtle.penup()    turtle.fd(size*2.2)    draw_heart(size)    turtle.penup()    turtle.setheading(150)    turtle.fd(size * 2.2)    draw_arrow(size)# 画出发射爱心的小人def draw_people(x, y):    turtle.penup()    turtle.goto(x, y)    turtle.pendown()    turtle.pensize(2)    turtle.color('black')    turtle.setheading(0)    turtle.circle(60, 360)    turtle.penup()    turtle.setheading(90)    turtle.fd(75)    turtle.setheading(180)    turtle.fd(20)    turtle.pensize(4)    turtle.pendown()    turtle.circle(2, 360)    turtle.setheading(0)    turtle.penup()    turtle.fd(40)    turtle.pensize(4)    turtle.pendown()    turtle.circle(-2, 360)    turtle.penup()    turtle.goto(x, y)    turtle.setheading(-90)    turtle.pendown()    turtle.fd(20)    turtle.setheading(0)    turtle.fd(35)    turtle.setheading(60)    turtle.fd(10)    turtle.penup()    turtle.goto(x, y)    turtle.setheading(-90)    turtle.pendown()    turtle.fd(40)    turtle.setheading(0)    turtle.fd(35)    turtle.setheading(-60)    turtle.fd(10)    turtle.penup()    turtle.goto(x, y)    turtle.setheading(-90)    turtle.pendown()    turtle.fd(60)    turtle.setheading(-135)    turtle.fd(60)    turtle.bk(60)    turtle.setheading(-45)    turtle.fd(30)    turtle.setheading(-135)    turtle.fd(35)    turtle.penup()# 第一个画面,显示文字def page0():    turtle.penup()    turtle.goto(-350, 0)    turtle.color('black')    turtle.write('专属于我们的什么节', font=('宋体', 60, 'normal')) #这里的字大家自己替换    time.sleep(3) # 第二个画面,显示发射爱心的小人def page1():    turtle.speed(10)    draw_people(-250, 20)    turtle.penup()    turtle.goto(-150, -30)    draw_heart(14)    turtle.penup()    turtle.goto(-20, -60)    draw_heart(25)    turtle.penup()    turtle.goto(250, -100)    draw_heart(45)    turtle.hideturtle()    time.sleep(3) # 最后一个画面,一箭穿心def page2():    turtle.speed(1)    turtle.penup()    turtle.goto(-200, -200)    turtle.color('blue')    turtle.pendown()    turtle.write('温轻舟       温轻舟', font=('wisdom', 25, 'normal')) #这里的字大家自己替换    turtle.penup()    turtle.goto(0, -180)    draw_heart(10)    arrow_heart(20, -60, 51)    turtle.showturtle() def main():    turtle.setup(900, 500)    page0()    clear_all()    page1()    clear_all()    page2()    turtle.done()main()

上一篇:Win10安全模式无法进入?揭秘F8按键问题及解决方法!

下一篇:云服务器公网ip怎么访问?