趣味python(内附源代码!!!)
前言
本人计算机专业大学生一枚,上个学期学了C语言。。。唉😔…真没想到这课实在是无趣的很,更主要的是听任课老师说这还只是个开始,之后几年要学的专业课程大都如此枯燥乏味…这…这还的了。。。
度过了一个漫长美好又愉快的假期,前几天突然收到了开学通知,没想到就剩下最后一点点的日子就得开学了。。。查看课表发现这学期要学一门很强大的编程语言-----python。于是立马去各大网站搜索了“如何培养学习编程语言的兴趣?” 结论就是找乐子—即用它来做些好玩的事(没办法嘛,人类的天性贪玩ヾ(≧▽≦*)o)。
下面我就把我从各处搜集来的“乐子”分享给大家。
正文
上面是我收集起来的所有的玩的,都是用python写出来的。因为文本原因(都懂的其实只是懒),我从中挑选了部分源代码及效果图分享至下文。想要尝试下其它的话,请评论区回复,我会及时给出的哈。
1.时钟
import turtle
from datetime import *
def Skip(step):
turtle.penup()
turtle.forward(step)
turtle.pendown()
def mkHand(name, length):
turtle.reset()
Skip(-length * 0.1)
turtle.begin_poly()
turtle.forward(length * 1.1)
turtle.end_poly()
handForm = turtle.get_poly()
turtle.register_shape(name, handForm)
def Init():
global secHand, minHand, hurHand, printer
turtle.mode("logo")
mkHand("secHand", 135)
mkHand("minHand", 125)
mkHand("hurHand", 90)
secHand = turtle.Turtle()
secHand.shape("secHand")
minHand = turtle.Turtle()
minHand.shape("minHand")
hurHand = turtle.Turtle()
hurHand.shape("hurHand")
for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 3)
hand.speed(0)
# 建立输出文字Turtle
printer = turtle.Turtle()
# 隐藏画笔的turtle形状
printer.hideturtle()
printer.penup()
def SetupClock(radius):
turtle.reset()
turtle.pensize(7)
for i in range(60):
Skip(radius)
if i % 5 == 0:
turtle.forward(20)
Skip(-radius - 20)
Skip(radius + 20)
if i == 0:
turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
elif i == 30:
Skip(25)
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
Skip(-25)
elif (i == 25 or i == 35):
Skip(20)
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
Skip(-20)
else:
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
Skip(-radius - 20)
else:
turtle.dot(5)
Skip(-radius)
turtle.right(6)
def Week(t):
week = ["星期一", "星期二", "星期三",
"星期四", "星期五", "星期六", "星期日"]
return week[t.weekday()]
def Date(t):
y = t.year
m = t.month
d = t.day
return "%s %d%d" % (y, m, d)
def Tick():
t = datetime.today()
second = t.second + t.microsecond * 0.000001
minute = t.minute + second / 60.0
hour = t.hour + minute / 60.0
secHand.setheading(6 * second)
minHand.setheading(6 * minute)
hurHand.setheading(30 * hour)
turtle.tracer(False)
printer.forward(65)
printer.write(Week(t), align="center",
font=("Courier", 14, "bold"))
printer.back(130)
printer.write(Date(t), align="center",
font=("Courier", 14, "bold"))
printer.home()
turtle.tracer(True)
turtle.ontimer(Tick, 100)
def main():
turtle.tracer(False)
Init()
SetupClock(160)
turtle.tracer(True)
Tick()
turtle.mainloop()
if __name__ == "__main__":
main()
效果图
2.花样显示你的名字
import turtle
t = turtle.Pen()
t.speed( 0 )
turtle.bgcolor( "white" )
colors = [ "red","orange","yellow","green","blue","purple","pink" ]
sides = int( turtle.numinput( "边数","请输入图形边数,1 到 8 之间:",4,1,8 ) )
your_name = turtle.textinput( "名字","请输入你的名字:" )
for x in range( 0,200):
t.pencolor( colors[x%sides] )
t.penup()
t.forward( x*3/sides + x )
t.pendown()
t.write( your_name,font=( "宋体",int( (x+4)/4 ),"bold" ) )
t.left( 360/sides + 1 )
t.width( x*sides / 200 )
效果图
3.玫瑰花
from turtle import *
setup (700,700)
pencolor("red")
hideturtle()
penup()
goto(0,280)
pendown()
left(135)
speed(0)
a=0.2
for i in range (80):
if i < 50:
pensize(a)
forward(2)
a+=0.15
if i<10:
left(0.7)
elif 10<i<24:
left(1)
elif 24<i<40:
left(3)
elif 40<i<50:
left(4)
elif i >=50:
pensize(a)
forward(2.2)
a-=0.25
if 50<i<65:
left(6.7)
elif 65<i<80:
left(2)
setheading(0)
penup()
goto(40,305)
pendown()
left(145)
a=0.1
for i in range (100):
if i < 60:
pensize(a)
forward(3)
a+=0.15
if i<10:
left(0.8)
elif 10<i<24:
left(0.8)
elif 24<i<40:
left(1.7)
elif 40<i<60:
left(2.2)
elif i >=60:
pensize(a)
forward(2.4)
a-=0.15
if 60<i<85:
left(3.2)
elif 85<i<90:
left(4)
setheading(0)
penup()
goto(45,320)
pendown()
left(10)
a=0.1
for i in range (100):
if i < 60:
pensize(a)
forward(3)
a+=0.15
if i<17:
right(3.2)
elif 17<i<20:
right(3.5)
elif 20<i<40:
right(6.8)
elif 40<i<50:
right(0.6)
elif 50<i<60:
left(1)
elif i >=60:
pensize(a)
forward(2.4)
a-=0.16
if 60<i<85:
left(0.4)
elif 85<i<90:
left(1)
setheading(0)
penup()
goto(-135,265)
pendown()
left(160)
a=0.1
for i in range (120):
if i < 60:
pensize(a)
forward(3)
a+=0.15
if i<10:
left(2)
elif 10<i<15:
left(8)
elif 15<i<35:
left(6)
elif 35<i<40:
left(4)
elif 40<i<60:
left(1)
elif i >=60:
pensize(a)
forward(3)
a-=0.15
if 60<i<90:
right(0.4)
elif 90<i<120:
right(0.8)
setheading(0)
penup()
goto(80,250)
pendown()
left(245)
a=0.2
for i in range (80):
if i < 40:
pensize(a)
forward(1.2)
a+=0.15
if i<15:
left(0.6)
elif 15<i<40:
left(0.3)
elif i >=40:
pensize(a)
forward(1.2)
a-=0.15
if 40<i<65:
left(0.6)
elif 65<i<80:
left(0.35)
setheading(0)
penup()
goto(-150,195)
pendown()
left(300)
a=0.2
for i in range (80):
if i < 40:
pensize(a)
forward(1.2)
a+=0.16
if i<15:
right(0.6)
elif 15<i<40:
right(0.3)
elif i >=40:
pensize(a)
forward(1.2)
a-=0.16
if 40<i<65:
right(0.6)
elif 65<i<80:
right(0.35)
setheading(0)
penup()
goto(-205,225)
pendown()
left(194)
a=0.1
for i in range (100):
if i < 50:
pensize(a)
forward(3)
a+=0.15
if i<10:
left(7)
elif 10<i<30:
left(3)
elif 30<i<50:
left(0.1)
elif i >=50:
pensize(a)
forward(3)
a-=0.15
if 50<i<80:
right(0.3)
elif 80<i<100:
right(0.6)
setheading(0)
penup()
goto(110,280)
pendown()
left(6)
a=0.1
for i in range (120):
if i < 60:
pensize(a)
forward(3)
a+=0.15
if i<10:
right(4.5)
elif 10<i<30:
right(5)
elif 30<i<60:
right(0.1)
elif i >=60:
pensize(a)
forward(3)
a-=0.15
if 50<i<80:
left(0.6)
elif 80<i<100:
left(0.9)
setheading(0)
penup()
goto(-160,110)
pendown()
left(283)
a=0.2
for i in range (80):
if i < 40:
pensize(a)
forward(2.4)
a+=0.15
if i<10:
right(0.6)
elif 15<i<40:
right(0.1)
elif i >=40:
pensize(a)
forward(2.4)
a-=0.15
if 40<i<65:
left(2)
elif 65<i<80:
left(3)
setheading(0)
penup()
goto(90,155)
pendown()
left(258)
a=0.2
for i in range (80):
if i < 40:
pensize(a)
forward(3)
a+=0.15
if i<10:
left(0.8)
elif 15<i<40:
left(0.1)
elif i >=40:
pensize(a)
forward(3)
a-=0.15
if 40<i<65:
right(2)
elif 65<i<80:
right(3)
setheading(0)
penup()
goto(-60,-45)
pendown()
right(9)
a=0.2
for i in range (40):
if i < 20:
pensize(a)
forward(1.3)
a+=0.15
if i<5:
left(0.7)
elif 5<i<20:
left(0.4)
elif i >=20:
pensize(a)
forward(1.3)
a-=0.15
if 20<i<35:
left(0.7)
elif 35<i<40:
left(0.4)
setheading(0)
penup()
goto(-70,-55)
pencolor("green")
pendown()
right(170)
a=0.2
for i in range (80):
if i <40 :
pensize(a)
forward(2)
a+=0.15
if i<25:
right(0.5)
elif 25<i<40:
right(1)
elif i >=40:
pensize(a)
forward(2.5)
a-=0.15
if 40<i<55:
right(1)
elif 55<i<70:
right(2)
elif 70<i<80:
right(1)
setheading(270)
left(30)
a=0.2
for i in range (80):
if i <40 :
pensize(a)
forward(2.5)
a+=0.15
if i<25:
left(0.5)
elif 25<i<40:
left(1)
elif i >=40:
pensize(a)
forward(3)
a-=0.15
if 40<i<55:
left(2)
elif 55<i<70:
left(1.3)
elif 70<i<80:
left(1)
setheading(270)
penup()
goto(20,-55)
pendown()
left(65)
a=0.2
for i in range (60):
if i <30 :
pensize(a)
forward(2)
a+=0.15
if i<15:
left(1)
elif 15<i<30:
left(2)
elif i >=30:
pensize(a)
forward(2.5)
a-=0.15
if 30<i<45:
left(2)
elif 45<i<50:
left(2.5)
elif 50<i<60:
left(1.5)
setheading(270)
right(17)
a=0.2
for i in range (80):
if i <40 :
pensize(a)
forward(2.2)
a+=0.15
if i<25:
right(1)
elif 25<i<40:
right(2)
elif i >=40:
pensize(a)
forward(2.5)
a-=0.15
if 40<i<55:
right(2)
elif 55<i<70:
right(1.5)
elif 70<i<80:
right(0.6)
setheading(0)
penup()
pencolor("brown")
goto(-49,-90)
pendown()
right(89)
a=1
for i in range (120):
if i < 60:
pensize(a)
forward(3.5)
a+=0.08
elif i >=60:
pensize(a)
forward(3.5)
a-=0.08
setheading(0)
penup()
goto(-16,-70)
pendown()
right(91)
a=1
for i in range (120):
if i < 60:
pensize(a)
forward(3.5)
a+=0.08
elif i >=60:
pensize(a)
forward(3.5)
a-=0.08
setheading(0)
speed(1)
penup()
goto(-16,-150)
pendown()
pensize(3)
left(35)
forward(15)
setheading(0)
right(115)
forward(34)
setheading(180)
penup()
goto(-49,-250)
pendown()
pensize(3)
right(35)
forward(15)
setheading(180)
left(115)
forward(34)
input()
done
效果图
总结
今天的分享就到这了,喜欢的话就请多多点赞支持哦。其他源代码有需求的话欢迎评论区留言哦。另外的话,希望大家在新的一年,新的学期里,学业进步,事业顺利,一起加油成长,一起进步吧!
PS:
如果用的是windows操作系统的话,运行这些源代码需要去python官网(https://www.python.org/)上下载个python的解释器
路径 https://www.python.org/downloads/windows/
最好再安装个文本编辑器,如:Visual Studio Code,Sublime Text等,具体的安装方法可以去网上搜索教程。