当前位置:   article > 正文

用python写的好玩的小程序,简单的python有趣小程序_python有趣小程序代码

python有趣小程序代码

本篇文章给大家谈谈用python写一个有趣的小程序,以及python可以写小程序代码吗,希望对各位有所帮助,不要忘了收藏本站喔。

系列文章目录


前言

这里有一些有趣的pythonxiao程序如:五角星,爱心,哆啦A梦,小猫等等(都是积累下来的)


1.搞怪的弹窗,自带的tk

  1. import tkinter as tk
  2. import random
  3. import threading
  4. import time
  5. def dow():
  6. window = tk.Tk()
  7. width = window.winfo_screenwidth()
  8. height = window.winfo_screenheight()
  9. a = random.randrange(0, width)
  10. b = random.randrange(0, height)
  11. window.title('你是傻子')
  12. window.geometry("200x50" +"+" +str(a) +"+" +str(b))
  13. tk.Label(window,
  14. text='你就是个傻子!啦啦啦!',# 标签的文字
  15. bg='Red',# 背景颜色
  16. font=('楷体',17),# 字体和字体大小
  17. width=15,height=2 # 标签长宽
  18. ).pack()# 固定窗口位置
  19. window.mainloop()
  20. threads = []
  21. for i in range(100):# 需要的弹框数量
  22. t = threading.Thread(target=dow)
  23. threads.append(t)
  24. time.sleep(0.1)
  25. threads[i].start()

2.爱心

  1. # -*- coding:utf-8 -*-
  2. import turtle
  3. import time
  4. # 画爱心的顶部
  5. def LittleHeart():
  6. for i in range(200):
  7. turtle.right(1)
  8. turtle.forward(2)
  9. # 输入表白的语句,默认I Love you
  10. love = input('请输入表白语句,默认为输入为"I Love you": ')
  11. # 输入署名或者赠谁,没有不执行
  12. me = input('请输入您心上人的姓名或者昵称: ')
  13. if love == '':
  14. love = 'I Love you'# 窗口大小
  15. turtle.setup(width=800, height=500)# 颜色
  16. turtle.color('red', 'pink')# 笔粗细
  17. turtle.pensize(5)# 速度
  18. turtle.speed(3)# 提笔
  19. turtle.up()# 隐藏笔
  20. turtle.hideturtle()# 去到的坐标,窗口中心为0,0
  21. turtle.goto(0, -180)
  22. turtle.showturtle()# 画上线
  23. turtle.down()
  24. turtle.speed(1)
  25. turtle.begin_fill()
  26. turtle.left(140)
  27. turtle.forward(224)# 调用画爱心左边的顶部
  28. LittleHeart()# 调用画爱右边的顶部
  29. turtle.left(120)
  30. LittleHeart()# 画下线
  31. turtle.forward(224)
  32. turtle.end_fill()
  33. turtle.pensize(5)
  34. turtle.up()
  35. turtle.hideturtle()# 在心中写字 一次
  36. turtle.goto(0, 0)
  37. turtle.showturtle()
  38. turtle.color('#CD5C5C', 'pink')# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
  39. turtle.write(love, font=('gungsuh', 30,), align="center")
  40. turtle.up()
  41. turtle.hideturtle()
  42. time.sleep(2)
  43. #
  44. turtle.goto(0, 0)
  45. turtle.showturtle()
  46. turtle.color('red', 'pink')
  47. turtle.write(love, font=('gungsuh', 30,), align="center")
  48. turtle.up()
  49. turtle.hideturtle()
  50. # 写署名
  51. if me != '':
  52. turtle.color('black', 'pink')
  53. time.sleep(2)
  54. turtle.goto(180, -180)
  55. turtle.showturtle()
  56. turtle.write(me, font=(20,), align="center", move=True)
  57. # 点击窗口关闭
  58. window = turtle.Screen()
  59. window.exitonclick()

3.哆啦A梦

  1. # 哆啦A梦
  2. import turtle
  3. def flyTo(x, y):
  4. turtle.penup()
  5. turtle.goto(x, y)
  6. turtle.pendown()
  7. def drawEye():
  8. turtle.tracer(False)
  9. a = 2.5
  10. for i in range(120):
  11. turtle.left(3)
  12. if 0 <= i < 30 or 60 <= i < 90:
  13. a -= 0.05
  14. else:
  15. a += 0.05
  16. turtle.fd(a)
  17. turtle.tracer(True)
  18. def beard():
  19. """ 画胡子, 一共六根
  20. """
  21. # 左边第一根胡子
  22. flyTo(-37, 135)
  23. turtle.seth(165)
  24. turtle.fd(60)
  25. # 左边第二根胡子
  26. flyTo(-37, 125)
  27. turtle.seth(180)
  28. turtle.fd(60)
  29. # 左边第三根胡子
  30. flyTo(-37, 115)
  31. turtle.seth(193)
  32. turtle.fd(60)
  33. # 右边第一根胡子
  34. flyTo(37, 135)
  35. turtle.seth(15)
  36. turtle.fd(60)
  37. # 右边第二根胡子
  38. flyTo(37, 125)
  39. turtle.seth(0)
  40. turtle.fd(60)
  41. # 右边第三根胡子
  42. flyTo(37, 115)
  43. turtle.seth(-13)
  44. turtle.fd(60)
  45. def drawRedScarf():
  46. """ 画围巾
  47. """
  48. turtle.fillcolor("red") # 填充颜色
  49. turtle.begin_fill()
  50. turtle.seth(0) # 朝向右
  51. turtle.fd(200) # 前进10个单位
  52. turtle.circle(-5, 90)
  53. turtle.fd(10)
  54. turtle.circle(-5, 90)
  55. turtle.fd(207)
  56. turtle.circle(-5, 90)
  57. turtle.fd(10)
  58. turtle.circle(-5, 90)
  59. turtle.end_fill()
  60. def drawMouse():
  61. flyTo(5, 148)
  62. turtle.seth(270)
  63. turtle.fd(100)
  64. turtle.seth(0)
  65. turtle.circle(120, 50)
  66. turtle.seth(230)
  67. turtle.circle(-120, 100)
  68. def drawRedNose():
  69. flyTo(-10, 158)
  70. turtle.fillcolor("red") # 填充颜色
  71. turtle.begin_fill()
  72. turtle.circle(20)
  73. turtle.end_fill()
  74. def drawBlackdrawEye():
  75. turtle.seth(0)
  76. flyTo(-20, 195)
  77. turtle.fillcolor("#000000") # 填充颜色
  78. turtle.begin_fill()
  79. turtle.circle(13)
  80. turtle.end_fill()
  81. turtle.pensize(6)
  82. flyTo(20, 205)
  83. turtle.seth(75)
  84. turtle.circle(-10, 150)
  85. turtle.pensize(3)
  86. flyTo(-17, 200)
  87. turtle.seth(0)
  88. turtle.fillcolor("#ffffff")
  89. turtle.begin_fill()
  90. turtle.circle(5)
  91. turtle.end_fill()
  92. flyTo(0, 0)
  93. def drawFace():
  94. """
  95. """
  96. turtle.forward(183) # 前行183个单位
  97. turtle.fillcolor("white") # 填充颜色为白色
  98. turtle.begin_fill() # 开始填充
  99. turtle.left(45) # 左转45度
  100. turtle.circle(120, 100) # 右边那半边脸
  101. turtle.seth(90) # 朝向向上
  102. drawEye() # 画右眼睛
  103. turtle.seth(180) # 朝向左
  104. turtle.penup() # 抬笔
  105. turtle.fd(60) # 前行60
  106. turtle.pendown() # 落笔
  107. turtle.seth(90) # 朝向上
  108. drawEye() # 画左眼睛
  109. turtle.penup() # 抬笔
  110. turtle.seth(180) # 朝向左
  111. turtle.fd(64) # 前进64
  112. turtle.pendown() # 落笔
  113. turtle.seth(215) # 修改朝向
  114. turtle.circle(120, 100) # 左边那半边脸
  115. turtle.end_fill() #
  116. def drawHead():
  117. """ 画了一个被切掉下半部分的圆
  118. """
  119. turtle.penup() # 抬笔
  120. turtle.circle(150, 40) # 画圆, 半径150,圆周角40
  121. turtle.pendown() # 落笔
  122. turtle.fillcolor("#00a0de") # 填充色
  123. turtle.begin_fill() # 开始填充
  124. turtle.circle(150, 280) # 画圆,半径150, 圆周角280
  125. turtle.end_fill()
  126. def drawAll():
  127. drawHead()
  128. drawRedScarf()
  129. drawFace()
  130. drawRedNose()
  131. drawMouse()
  132. beard()
  133. flyTo(0, 0)
  134. turtle.seth(0)
  135. turtle.penup()
  136. turtle.circle(150, 50)
  137. turtle.pendown()
  138. turtle.seth(30)
  139. turtle.fd(40)
  140. turtle.seth(70)
  141. turtle.circle(-30, 270)
  142. turtle.fillcolor("#00a0de")
  143. turtle.begin_fill()
  144. turtle.seth(230)
  145. turtle.fd(80)
  146. turtle.seth(90)
  147. turtle.circle(1000, 1)
  148. turtle.seth(-89)
  149. turtle.circle(-1000, 10)
  150. turtle.seth(180)
  151. turtle.fd(70)
  152. turtle.seth(90)
  153. turtle.circle(30, 180)
  154. turtle.seth(180)
  155. turtle.fd(70)
  156. turtle.seth(100)
  157. turtle.circle(-1000, 9)
  158. turtle.seth(-86)
  159. turtle.circle(1000, 2)
  160. turtle.seth(230)
  161. turtle.fd(40)
  162. turtle.circle(-30, 230)
  163. turtle.seth(45)
  164. turtle.fd(81)
  165. turtle.seth(0)
  166. turtle.fd(203)
  167. turtle.circle(5, 90)
  168. turtle.fd(10)
  169. turtle.circle(5, 90)
  170. turtle.fd(7)
  171. turtle.seth(40)
  172. turtle.circle(150, 10)
  173. turtle.seth(30)
  174. turtle.fd(40)
  175. turtle.end_fill()
  176. # 左手
  177. turtle.seth(70)
  178. turtle.fillcolor("#FFFFFF")
  179. turtle.begin_fill()
  180. turtle.circle(-30)
  181. turtle.end_fill()
  182. # 脚
  183. flyTo(103.74, -182.59)
  184. turtle.seth(0)
  185. turtle.fillcolor("#FFFFFF")
  186. turtle.begin_fill()
  187. turtle.fd(15)
  188. turtle.circle(-15, 180)
  189. turtle.fd(90)
  190. turtle.circle(-15, 180)
  191. turtle.fd(10)
  192. turtle.end_fill()
  193. flyTo(-96.26, -182.59)
  194. turtle.seth(180)
  195. turtle.fillcolor("#FFFFFF")
  196. turtle.begin_fill()
  197. turtle.fd(15)
  198. turtle.circle(15, 180)
  199. turtle.fd(90)
  200. turtle.circle(15, 180)
  201. turtle.fd(10)
  202. turtle.end_fill()
  203. # 右手
  204. flyTo(-133.97, -91.81)
  205. turtle.seth(50)
  206. turtle.fillcolor("#FFFFFF")
  207. turtle.begin_fill()
  208. turtle.circle(30)
  209. turtle.end_fill()
  210. # 口袋
  211. flyTo(-103.42, 15.09)
  212. turtle.seth(0)
  213. turtle.fd(38)
  214. turtle.seth(230)
  215. turtle.begin_fill()
  216. turtle.circle(90, 260)
  217. turtle.end_fill()
  218. flyTo(5, -40)
  219. turtle.seth(0)
  220. turtle.fd(70)
  221. turtle.seth(-90)
  222. turtle.circle(-70, 180)
  223. turtle.seth(0)
  224. turtle.fd(70)
  225. # 铃铛
  226. flyTo(-103.42, 15.09)
  227. turtle.fd(90)
  228. turtle.seth(70)
  229. turtle.fillcolor("#ffd200")
  230. turtle.begin_fill()
  231. turtle.circle(-20)
  232. turtle.end_fill()
  233. turtle.seth(170)
  234. turtle.fillcolor("#ffd200")
  235. turtle.begin_fill()
  236. turtle.circle(-2, 180)
  237. turtle.seth(10)
  238. turtle.circle(-100, 22)
  239. turtle.circle(-2, 180)
  240. turtle.seth(180 - 10)
  241. turtle.circle(100, 22)
  242. turtle.end_fill()
  243. flyTo(-13.42, 15.09)
  244. turtle.seth(250)
  245. turtle.circle(20, 110)
  246. turtle.seth(90)
  247. turtle.fd(15)
  248. turtle.dot(10)
  249. flyTo(0, -150)
  250. drawBlackdrawEye()
  251. def main():
  252. turtle.screensize(800, 6000, "#F0F0F0")
  253. turtle.pensize(3)
  254. turtle.speed(9)
  255. drawAll()
  256. if __name__ == "__main__":
  257. main()
  258. turtle.mainloop()

4.画小恐龙

  1. import turtle as t
  2. #龙身
  3. t.setup(1000,600) # 设置画布的大小
  4. t.speed(10) # 设置画笔速度为10
  5. t.pensize(5) # 设置画笔大小
  6. t.pencolor("SpringGreen4") # 设置画笔颜色
  7. t.penup() # 提笔
  8. t.goto(250,180) # 画笔前往坐标(250,180)
  9. t.begin_fill() # 准备填充
  10. t.pendown() # 落笔
  11. t.seth(120) # 画笔角度为120°
  12. t.circle(100,140) # 画一个半径为100,角度为140°的圆
  13. t.seth(-96)
  14. t.fd(120) # 向前移动120
  15. t.circle(-100,50);t.circle(-80,23);t.seth(176);t.fd(20);t.seth(180)
  16. t.circle(-315,40);t.seth(270);t.circle(50,30);t.circle(10,3);t.seth(-60)
  17. t.circle(180,40);t.circle(500,20);t.circle(750,8);t.circle(80,60);t.circle(70,30)
  18. t.fd(90);t.circle(-80,30);t.seth(10);t.fd(60);t.seth(160);t.fd(90);t.seth(22)
  19. t.fd(89);t.color("LightGreen")# 设置填充颜色#t.color('SpringGreen1')
  20. t.end_fill() # 依据轮廓填充
  21. #龙眼
  22. t.pensize(5);t.pencolor("SpringGreen4");t.penup();t.goto(128,165);t.pendown()
  23. t.begin_fill();t.seth(0);t.circle(20,360);t.color((1,1,1)) #填充颜色为白色;t.end_fill()
  24. t.penup();t.goto(128,185);t.pendown();t.seth(0);t.pensize(15);t.pencolor((0,0,0));t.circle(1,360)
  25. t.pensize(5);t.pencolor("SpringGreen4");t.penup();t.goto(177,175);t.pendown()
  26. t.begin_fill();t.seth(0);t.circle(20,360);t.color((1,1,1));t.end_fill();t.penup()
  27. t.goto(177,195);t.pendown();t.seth(0);t.pensize(15);t.pencolor((0,0,0));t.circle(1,360)
  28. #龙爪
  29. #上爪
  30. t.penup();t.pencolor("Aquamarine4");t.goto(195,60);t.pensize(11);t.pendown()
  31. t.seth(10);t.fd(34);t.penup();t.goto(208,66);t.pendown();t.pensize(9);t.seth(45)
  32. t.fd(21);t.penup();t.goto(208,66);t.pendown();t.pensize(9);t.seth(-35);t.fd(21)
  33. #下爪
  34. t.penup();t.goto(171,20);t.pensize(11);t.pendown();t.seth(-3);t.fd(58);t.penup()
  35. t.goto(213,22);t.pendown();t.pensize(9);t.seth(35);t.fd(18);t.penup();t.goto(213,20)
  36. t.pendown();t.pensize(9);t.seth(-55);t.fd(20);t.penup();t.goto(171,20);t.pensize(14)
  37. t.pendown();t.seth(-3);t.fd(39);t.penup();t.goto(195,60);t.pensize(14);t.pendown()
  38. t.seth(10);t.fd(10)
  39. #龙脚#左脚
  40. t.penup();t.goto(71,-100);t.pensize(16);t.pendown();t.seth(-110);t.fd(40)
  41. t.penup();t.goto(55,-140);t.pendown();t.pensize(11);t.seth(-150);t.fd(28)
  42. t.penup();t.goto(55,-140);t.pendown();t.seth(-70);t.fd(23);t.penup();t.goto(55,-140)
  43. t.pendown();t.seth(-20);t.pensize(10);t.fd(23);t.seth(-40);t.pensize(11);t.fd(8)
  44. #右脚
  45. t.penup();t.goto(142,-113);t.pensize(16);t.pendown();t.seth(-80);t.fd(23);t.penup()
  46. t.goto(145,-133);t.pendown();t.pensize(11);t.seth(-120);t.fd(30);t.penup()
  47. t.goto(145,-133);t.pendown();t.pensize(11);t.seth(-70);t.fd(27);t.penup();t.goto(145,-133)
  48. t.pendown();t.pensize(10);t.seth(-27);t.fd(27);t.pensize(11);t.seth(-50);t.fd(8)
  49. #牙齿#上牙
  50. t.penup();t.goto(240,172);t.pendown();t.color("Yellow2");t.pensize(4);t.seth(-110)
  51. t.fd(12);t.seth(120);t.fd(12);t.penup();t.goto(220,165);t.pendown();t.seth(-110)
  52. t.fd(12);t.seth(130);t.fd(12);t.penup();t.goto(200,157);t.pendown();t.seth(-110)
  53. t.fd(12);t.seth(140);t.fd(12);t.penup();t.goto(250,180);t.pensize(5);t.pencolor("SpringGreen4")
  54. t.pendown();t.seth(22);t.bk(88)
  55. #下牙
  56. t.penup();t.goto(200,140);t.pendown();t.color("Yellow2");t.pensize(4);t.seth(45)
  57. t.fd(12);t.seth(-90);t.fd(12);t.penup();t.goto(215,135);t.pendown();t.seth(45)
  58. t.fd(12);t.seth(-90);t.fd(12);t.penup();t.goto(230,130);t.pendown();t.seth(45)
  59. t.fd(12);t.seth(-90);t.fd(12);t.penup();t.goto(251,119);t.pensize(5)
  60. t.pencolor("SpringGreen4");t.pendown();t.seth(160);t.fd(89)
  61. #龙脊
  62. t.penup();t.goto(120,220);t.pensize(5);t.pendown();t.pencolor("SeaGreen")
  63. #自头而尾 14片
  64. t.begin_fill();t.color('SeaGreen');t.seth(160);t.fd(40);t.seth(-60);t.fd(33);t.end_fill()
  65. t.begin_fill();t.color('SeaGreen');t.seth(180);t.fd(40);t.seth(-60);t.fd(33);t.end_fill()
  66. t.begin_fill();t.color('SeaGreen');t.seth(200);t.fd(40);t.seth(-50);t.fd(38);t.end_fill()
  67. t.begin_fill();t.color('SeaGreen');t.seth(205);t.fd(40);t.seth(-50);t.fd(46);t.end_fill()
  68. t.begin_fill();t.color('SeaGreen');t.seth(220);t.fd(40);t.seth(-50);t.fd(41.2);t.end_fill()#5
  69. t.begin_fill();t.color('SeaGreen');t.seth(190);t.fd(40);t.seth(-50);t.fd(40.8);t.end_fill()
  70. t.begin_fill();t.color('SeaGreen');t.seth(168);t.fd(44);t.seth(-89);t.fd(47);t.end_fill()
  71. t.begin_fill();t.color('SeaGreen');t.seth(138);t.fd(33);t.seth(-120);t.fd(28);t.end_fill()
  72. t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(28);t.end_fill()
  73. t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(30);t.end_fill()#10
  74. t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(24);t.end_fill()
  75. t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(24);t.end_fill()
  76. t.begin_fill();t.color('SeaGreen');t.seth(110);t.fd(32);t.seth(-120);t.fd(24);t.end_fill()
  77. t.begin_fill();t.color('SeaGreen');t.seth(110);t.fd(32);t.seth(-120);t.fd(22);t.end_fill()
  78. #补充没上色的部分
  79. t.penup();t.pensize(7);t.goto(-99,-49);t.pendown();t.seth(-1);t.fd(86);t.seth(0);t.fd(6);t.seth(40)
  80. t.fd(30);t.seth(50);t.fd(48);t.seth(90);t.fd(15);t.done()

5.小小的心意

  1. from turtle import *
  2. pensize(20)
  3. pencolor("red")
  4. fillcolor("pink")
  5. speed(5)
  6. up()
  7. goto(-30,100)
  8. down()
  9. begin_fill()
  10. left(90)
  11. circle(120,180)
  12. circle(360,70)
  13. left(38)
  14. circle(360,70)
  15. circle(120,180)
  16. end_fill()
  17. up()
  18. goto(-100,-100)
  19. down()

尝试

  1. import turtle
  2. ##turtle.right(100)
  3. turtle.screensize(400,200,"yellow");turtle.speed(10);turtle.color("red");turtle.circle(50)
  4. turtle.circle(100);turtle.circle(-50);turtle.circle(-100);turtle.speed(8);turtle.goto(100,200)
  5. turtle.color("black");turtle.forward(100);turtle.right(90);turtle.forward(100);turtle.right(90)
  6. turtle.forward(200);turtle.right(90);turtle.forward(100);turtle.right(90);turtle.forward(100)
  7. turtle.dot(5,"green");turtle.home();turtle.goto(-100,200);turtle.color("black");turtle.forward(100)
  8. turtle.right(90);turtle.forward(100);turtle.right(90);turtle.forward(200);turtle.right(90)
  9. turtle.forward(100);turtle.right(90);turtle.forward(100);turtle.dot(5,"green");turtle.home()
  10. turtle.goto(-100,-200);turtle.color("black");turtle.forward(100);turtle.left(90);turtle.forward(100)
  11. turtle.left(90);turtle.forward(200);turtle.left(90);turtle.forward(100);turtle.left(90)
  12. turtle.forward(100);turtle.dot(5,"green");turtle.home();turtle.goto(100,-200);turtle.color("black")
  13. turtle.forward(100);turtle.left(90);turtle.forward(100);turtle.left(90);turtle.forward(200)
  14. turtle.left(90);turtle.forward(100);turtle.left(90);turtle.forward(100);turtle.dot(5,"green")
  15. turtle.home()
  16. turtle.mainloop()

总结

有待更新

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览392952 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/92109
推荐阅读
相关标签
  

闽ICP备14008679号