当前位置:   article > 正文

用python编写一个小程序,python入门小程序编写_python 小程序源码

python 小程序源码

大家好,小编来为大家解答以下问题,用python编写一个小程序,python入门小程序编写,今天让我们一起来看看吧!

一:樱花树

运行效果:
在这里插入图片描述

完整代码如下:

  1. import turtle
  2. import random
  3. from turtle import *
  4. from time import sleep
  5. # 画樱花的躯干(60,t)
  6. def tree(branchLen,t):
  7. sleep(0.0005)
  8. if branchLen >3:
  9. if 8<= branchLen <=12:
  10. if random.randint(0,2) == 0:
  11. t.color('snow') # 白
  12. else:
  13. t.color('lightcoral') # 淡珊瑚色
  14. t.pensize(branchLen / 3)
  15. elif branchLen <8:
  16. if random.randint(0,1) == 0:
  17. t.color('snow')
  18. else:
  19. t.color('lightcoral') # 淡珊瑚色
  20. t.pensize(branchLen / 2)
  21. else:
  22. t.color('sienna') # 赭(zhě)色
  23. t.pensize(branchLen / 10) # 6
  24. t.forward(branchLen)
  25. a = 1.5 * random.random()
  26. t.right(20*a)
  27. b = 1.5 * random.random()
  28. tree(branchLen-10*b, t)
  29. t.left(40*a)
  30. tree(branchLen-10*b, t)
  31. t.right(20*a)
  32. t.up()
  33. t.backward(branchLen)
  34. t.down()
  35. # 掉落的花瓣
  36. def petal(m, t):
  37. for i in range(m):
  38. a = 200 - 400 * random.random()
  39. b = 10 - 20 * random.random()
  40. t.up()
  41. t.forward(b)
  42. t.left(90)
  43. t.forward(a)
  44. t.down()
  45. t.color('lightcoral') # 淡珊瑚色
  46. t.circle(1)
  47. t.up()
  48. t.backward(a)
  49. t.right(90)
  50. t.backward(b)
  51. def main():
  52. # 绘图区域
  53. t = turtle.Turtle()
  54. # 画布大小
  55. w = turtle.Screen()
  56. t.hideturtle() # 隐藏画笔
  57. getscreen().tracer(1,0)
  58. w.screensize(bg='wheat')
  59. t.left(90)
  60. t.up()
  61. t.backward(150)
  62. t.down()
  63. t.color('sienna')
  64. # 画樱花的躯干
  65. tree(60,t)
  66. # 掉落的花瓣
  67. petal(200, t)
  68. w.exitonclick()
  69. main()

二:呆萌小鸭子

运行效果:
在这里插入图片描述

完整代码如下:

  1. from turtle import *
  2. #扁嘴
  3. pensize(2)
  4. pu()
  5. goto(-100,100)#上嘴最高顶点
  6. seth(-50)
  7. pd()
  8. color('#6C3100','#FADD77')
  9. begin_fill()
  10. fd(16)
  11. vertex_right = pos()#嘴最右顶点
  12. rt(50)
  13. fd(12)
  14. vertex_down = pos()#下嘴最低顶点
  15. rt(80)
  16. fd(30)
  17. circle(-3,200)
  18. vertex_left = pos()#嘴最左顶点
  19. goto(-100,100)
  20. end_fill()
  21. goto(vertex_left)#回到最左顶点
  22. circle(-3,-200)#扁嘴
  23. goto(vertex_right)
  24. #身体
  25. #头颈背尾曲线
  26. color('#B6A88E')
  27. pu()
  28. goto(-100,100)
  29. pd()
  30. seth(80)
  31. circle(-36,160)
  32. fd(25)
  33. circle(115,20)
  34. circle(60,55)
  35. circle(-200,20)
  36. circle(110,20)
  37. color('#7D6A4C')
  38. circle(40,40)
  39. color('#B6A88E')
  40. seth(-100)
  41. circle(-180,30)
  42. circle(-20,50)
  43. #右鸭腿
  44. circle(20,70)
  45. color('#736856')
  46. circle(-12,120)
  47. leg_pos1 = pos()#定位左腿位置
  48. fd(25)
  49. #前胸肚曲线
  50. pu()
  51. goto(vertex_down)
  52. pd()
  53. seth(-10)
  54. color('#B9AD9D')
  55. circle(-40,50)
  56. circle(-80,48)
  57. color('#736856')
  58. circle(250,5)
  59. circle(50,75)
  60. color('#B9AD9D')
  61. circle(220,28)
  62. #左鸭腿
  63. pu()
  64. seth(175)
  65. fd(40)
  66. pd()
  67. seth(-120)
  68. fd(8)
  69. circle(-10,120)
  70. leg_pos2 = pos()#定位右腿位置
  71. fd(15)
  72. #眼睛
  73. color('black')
  74. #左眼
  75. pu()
  76. goto(vertex_down - (1,-29))
  77. pd()
  78. dot(4,'black')
  79. #右眼
  80. pu()
  81. goto(vertex_down + (23,20))
  82. pd()
  83. dot(4,'black')
  84. #翅膀
  85. color('#BCB2A6')
  86. pu()
  87. goto(vertex_down - (-75,130))
  88. seth(130)
  89. pd()
  90. circle(-25,130)
  91. circle(-100,30)
  92. fd(85)
  93. point = pos()
  94. rt(137)
  95. fd(52)
  96. circle(-100,58)
  97. pu()
  98. goto(point)
  99. lt(30)
  100. pd()
  101. fd(60)
  102. pu()
  103. goto(point)
  104. pd()
  105. lt(10)
  106. fd(70)
  107. #腿部
  108. #左腿
  109. def leg(pos0):#鸭腿绘制函数
  110. pensize(8)
  111. color('#ECC578')
  112. pu()
  113. goto(pos0)
  114. seth(0)
  115. fd(7)
  116. seth(-90)
  117. fd(8.5)
  118. pd()
  119. fd(20)#腿长
  120. leg(leg_pos1)
  121. leg(leg_pos2)
  122. #小红靴——函数
  123. def boot(pos0):
  124. pensize(2)
  125. color('#B4070B','#FBA06B')
  126. pu()
  127. goto(pos0)#靴子右上顶点
  128. pd()
  129. begin_fill()
  130. seth(140)
  131. circle(25,80)
  132. seth(-80)
  133. fd(35)
  134. circle(-2,60)#靴低
  135. fd(20)
  136. circle(4,180)
  137. seth(5)
  138. fd(30)
  139. circle(2,60)
  140. goto(pos0)#右侧线条
  141. end_fill()
  142. boot(leg_pos1-(-20,30))
  143. boot(leg_pos2-(-20,30))
  144. #小雨滴
  145. color('#77DDFF','#D8E8E5')
  146. fd_ls = [200,140,250,240,230,220,180,250]
  147. lt_ls = [30,60,60,100,125,170,200,330]
  148. for i in range(8):
  149. pu()
  150. home()
  151. lt(lt_ls[i])
  152. fd(fd_ls[i])
  153. pd()
  154. seth(-78)
  155. fd(15)
  156. begin_fill()
  157. circle(-3,200)
  158. end_fill()
  159. fd(15)
  160. #文字
  161. pu()
  162. goto(vertex_left)
  163. seth(180)
  164. fd(150)
  165. seth(-90)
  166. fd(300)
  167. color('black')
  168. write('呆萌小鸭子',font=("Arial",15,"normal"))
  169. hideturtle()
  170. done()

三:计算器

运行效果:
在这里插入图片描述

完整代码如下:

  1. import tkinter as tk
  2. class Calc(tk.Tk):
  3. """计算器窗体类"""
  4. def __init__(self):
  5. """初始化实例"""
  6. tk.Tk.__init__(self)
  7. self.title("计算器")
  8. self.memory = 0 # 暂存数值
  9. self.create()
  10. def create(self):
  11. """创建界面"""
  12. btn_list = ["C", "M->", "->M", "/",
  13. "7", "8", "9", "*",
  14. "4", "5", "6", "-",
  15. "1", "2", "3", "+",
  16. "+/-", "0", ".", "="]
  17. r = 1
  18. c = 0
  19. for b in btn_list:
  20. self.button = tk.Button(self, text=b, width=5,
  21. command=(lambda x=b: self.click(x)))
  22. self.button.grid(row=r, column=c, padx=3, pady=6)
  23. c += 1
  24. if c > 3:
  25. c = 0
  26. r += 1
  27. self.entry = tk.Entry(self, width=24, borderwidth=2,
  28. bg="yellow", font=("Consolas", 12))
  29. self.entry.grid(row=0, column=0, columnspan=4, padx=8, pady=6)
  30. def click(self, key):
  31. """响应按钮"""
  32. if key == "=": # 输出结果
  33. result = eval(self.entry.get())
  34. self.entry.insert(tk.END, " = " + str(result))
  35. elif key == "C": # 清空输入框
  36. self.entry.delete(0, tk.END)
  37. elif key == "->M": # 存入数值
  38. self.memory = self.entry.get()
  39. if "=" in self.memory:
  40. ix = self.memory.find("=")
  41. self.memory = self.memory[ix + 2:]
  42. self.title("M=" + self.memory)
  43. elif key == "M->": # 取出数值
  44. if self.memory:
  45. self.entry.insert(tk.END, self.memory)
  46. elif key == "+/-": # 正负翻转
  47. if "=" in self.entry.get():
  48. self.entry.delete(0, tk.END)
  49. elif self.entry.get()[0] == "-":
  50. self.entry.delete(0)
  51. else:
  52. self.entry.insert(0, "-")
  53. else: # 其他键
  54. if "=" in self.entry.get():
  55. self.entry.delete(0, tk.END)
  56. self.entry.insert(tk.END, key)
  57. if __name__ == "__main__":
  58. Calc().mainloop()

四:皮卡丘

运行效果:
在这里插入图片描述

完整代码如下:

  1. from turtle import *
  2. '''
  3. 绘制皮卡丘头部
  4. '''
  5. def face(x,y):
  6. """画脸"""
  7. begin_fill()
  8. penup()
  9. # 将海龟移动到指定的坐标
  10. goto(x, y)
  11. pendown()
  12. # 设置海龟的方向
  13. setheading(40)
  14. circle(-150, 69)
  15. fillcolor("#FBD624")
  16. # 将海龟移动到指定的坐标
  17. penup()
  18. goto(53.14, 113.29)
  19. pendown()
  20. setheading(300)
  21. circle(-150, 30)
  22. setheading(295)
  23. circle(-140, 20)
  24. print(position())
  25. forward(5)
  26. setheading(260)
  27. circle(-80, 70)
  28. print(position())
  29. penup()
  30. goto(-74.43,-79.09)
  31. pendown()
  32. penup()
  33. # 将海龟移动到指定的坐标
  34. goto(-144,103)
  35. pendown()
  36. setheading(242)
  37. circle(110, 35)
  38. right(10)
  39. forward(10)
  40. setheading(250)
  41. circle(80, 115)
  42. print(position())
  43. penup()
  44. goto(-74.43,-79.09)
  45. pendown()
  46. setheading(10)
  47. penup()
  48. goto(-144, 103)
  49. pendown()
  50. penup()
  51. goto(x, y)
  52. pendown()
  53. end_fill()
  54. # 下巴
  55. penup()
  56. goto(-50, -82.09)
  57. pendown()
  58. pencolor("#DDA120")
  59. fillcolor("#DDA120")
  60. begin_fill()
  61. setheading(-12)
  62. circle(120, 25)
  63. setheading(-145)
  64. forward(30)
  65. setheading(180)
  66. circle(-20, 20)
  67. setheading(143)
  68. forward(30)
  69. end_fill()
  70. def eye():
  71. """画眼睛"""
  72. # 左眼
  73. color("black","black")
  74. penup()
  75. goto(-110, 27)
  76. pendown()
  77. begin_fill()
  78. setheading(0)
  79. circle(24)
  80. end_fill()
  81. # 左眼仁
  82. color("white", "white")
  83. penup()
  84. goto(-105, 51)
  85. pendown()
  86. begin_fill()
  87. setheading(0)
  88. circle(10)
  89. end_fill()
  90. # 右眼
  91. color("black", "black")
  92. penup()
  93. goto(25, 40)
  94. pendown()
  95. begin_fill()
  96. setheading(0)
  97. circle(24)
  98. end_fill()
  99. # 右眼仁
  100. color("white", "white")
  101. penup()
  102. goto(17, 62)
  103. pendown()
  104. begin_fill()
  105. setheading(0)
  106. circle(10)
  107. end_fill()
  108. def cheek():
  109. """画脸颊"""
  110. # 右边
  111. color("#9E4406", "#FE2C21")
  112. penup()
  113. goto(-130, -50)
  114. pendown()
  115. begin_fill()
  116. setheading(0)
  117. circle(27)
  118. end_fill()
  119. # 左边
  120. color("#9E4406", "#FE2C21")
  121. penup()
  122. goto(53, -20)
  123. pendown()
  124. begin_fill()
  125. setheading(0)
  126. circle(27)
  127. end_fill()
  128. def nose():
  129. """画鼻子"""
  130. color("black", "black")
  131. penup()
  132. goto(-40, 38)
  133. pendown()
  134. begin_fill()
  135. circle(7,steps = 3)
  136. end_fill()
  137. def mouth():
  138. """画嘴"""
  139. color("black", "#F35590")
  140. # 嘴唇
  141. penup()
  142. goto(-10, 22)
  143. pendown()
  144. begin_fill()
  145. setheading(260)
  146. forward(60)
  147. circle(-11, 150)
  148. forward(55)
  149. print(position())
  150. penup()
  151. goto(-38.46, 21.97)
  152. pendown()
  153. end_fill()
  154. # 舌头
  155. color("#6A070D", "#6A070D")
  156. begin_fill()
  157. penup()
  158. goto(-10.00, 22.00)
  159. pendown()
  160. penup()
  161. goto(-14.29, -1.7)
  162. pendown()
  163. penup()
  164. goto(-52, -5)
  165. pendown()
  166. penup()
  167. goto(-60.40, 12.74)
  168. pendown()
  169. penup()
  170. goto(-38.46, 21.97)
  171. pendown()
  172. penup()
  173. goto(-10.00, 22.00)
  174. pendown()
  175. end_fill()
  176. color("black","#FFD624")
  177. penup()
  178. goto(-78, 15)
  179. pendown()
  180. begin_fill()
  181. setheading(-25)
  182. for i in range(2):
  183. setheading(-25)
  184. circle(35, 70)
  185. end_fill()
  186. color("#AB1945", "#AB1945")
  187. penup()
  188. goto(-52, -5)
  189. pendown()
  190. begin_fill()
  191. setheading(40)
  192. circle(-33, 70)
  193. goto(-16,-1.7)
  194. penup()
  195. goto(-18,-17)
  196. pendown()
  197. setheading(155)
  198. circle(25, 70)
  199. end_fill()
  200. def ear():
  201. """画耳朵"""
  202. # 左耳
  203. color("black","#FFD624")
  204. penup()
  205. goto(-145, 93)
  206. pendown()
  207. begin_fill()
  208. setheading(165)
  209. circle(-248,50)
  210. right(120)
  211. circle(-248,50)
  212. end_fill()
  213. color("black", "black")
  214. penup()
  215. goto(-240, 143)
  216. pendown()
  217. begin_fill()
  218. setheading(107)
  219. circle(-170, 25)
  220. left(80)
  221. circle(229, 15)
  222. left(120)
  223. circle(300, 15)
  224. end_fill()
  225. # 右耳
  226. color("black", "#FFD624")
  227. penup()
  228. goto(30, 136)
  229. pendown()
  230. begin_fill()
  231. setheading(64)
  232. circle(-248, 50)
  233. right(120)
  234. circle(-248, 50)
  235. end_fill()
  236. color("black", "black")
  237. penup()
  238. goto(160, 200)
  239. pendown()
  240. begin_fill()
  241. setheading(52)
  242. circle(170, 25)
  243. left(116)
  244. circle(229, 15)
  245. left(71)
  246. circle(-300, 15)
  247. end_fill()
  248. def setting():
  249. """设置参数"""
  250. pensize(2)
  251. # 隐藏海龟
  252. hideturtle()
  253. speed(10)
  254. def main():
  255. """主函数"""
  256. setting()
  257. face(-132,115)
  258. eye()
  259. cheek()
  260. nose()
  261. mouth()
  262. ear()
  263. done()
  264. if __name__ == '__main__':
  265. main()

五:表白专用

运行效果:
在这里插入图片描述
在这里插入图片描述

完整代码如下:

  1. import turtle
  2. import time
  3. # 清屏函数
  4. def clear_all():
  5. turtle.penup()
  6. turtle.goto(0, 0)
  7. turtle.color('white')
  8. turtle.pensize(800)
  9. turtle.pendown()
  10. turtle.setheading(0)
  11. turtle.fd(300)
  12. turtle.bk(600)
  13. # 重定位海龟的位置
  14. def go_to(x, y, state):
  15. turtle.pendown() if state else turtle.penup()
  16. turtle.goto(x, y)
  17. # 画线
  18. # state为真时海龟回到原点,为假时不回到原来的出发点
  19. def draw_line(length, angle, state):
  20. turtle.pensize(1)
  21. turtle.pendown()
  22. turtle.setheading(angle)
  23. turtle.fd(length)
  24. turtle.bk(length) if state else turtle.penup()
  25. turtle.penup()
  26. # 画箭羽
  27. def draw_feather(size):
  28. angle = 30 # 箭的倾角
  29. feather_num = size//6 # 羽毛的数量
  30. feather_length = size // 3 # 羽毛的长度
  31. feather_gap = size//10 # 羽毛的间隔
  32. for i in range(feather_num):
  33. draw_line(feather_gap, angle+180, False) # 箭柄,不折返
  34. draw_line(feather_length, angle + 145, True) # 羽翼,要折返
  35. draw_line(feather_length, angle + 145, False)
  36. draw_line(feather_num*feather_gap, angle, False)
  37. draw_line(feather_length, angle + 145 + 180, False)
  38. for i in range(feather_num):
  39. draw_line(feather_gap, angle+180, False) # 箭柄,不折返
  40. draw_line(feather_length, angle - 145, True) # 羽翼,要折返
  41. draw_line(feather_length, angle - 145, False)
  42. draw_line(feather_num*feather_gap, angle, False)
  43. draw_line(feather_length, angle - 145 + 180, False)
  44. # 画爱心
  45. def draw_heart(size):
  46. turtle.color('red', 'pink')
  47. turtle.pensize(2)
  48. turtle.pendown()
  49. turtle.setheading(150)
  50. turtle.begin_fill()
  51. turtle.fd(size)
  52. turtle.circle(size * -3.745, 45)
  53. turtle.circle(size * -1.431, 165)
  54. turtle.left(120)
  55. turtle.circle(size * -1.431, 165)
  56. turtle.circle(size * -3.745, 45)
  57. turtle.fd(size)
  58. turtle.end_fill()
  59. # 画箭
  60. def draw_arrow(size):
  61. angle = 30
  62. turtle.color('black')
  63. draw_feather(size)
  64. turtle.pensize(4)
  65. turtle.setheading(angle)
  66. turtle.pendown()
  67. turtle.fd(size*2)
  68. # 一箭穿心
  69. # 箭的头没有画出来,而是用海龟来代替
  70. def arrow_heart(x, y, size):
  71. go_to(x, y, False)
  72. draw_heart(size*1.15)
  73. turtle.setheading(-150)
  74. turtle.penup()
  75. turtle.fd(size*2.2)
  76. draw_heart(size)
  77. turtle.penup()
  78. turtle.setheading(150)
  79. turtle.fd(size * 2.2)
  80. draw_arrow(size)
  81. # 画出发射爱心的小人
  82. def draw_people(x, y):
  83. turtle.penup()
  84. turtle.goto(x, y)
  85. turtle.pendown()
  86. turtle.pensize(2)
  87. turtle.color('black')
  88. turtle.setheading(0)
  89. turtle.circle(60, 360)
  90. turtle.penup()
  91. turtle.setheading(90)
  92. turtle.fd(75)
  93. turtle.setheading(180)
  94. turtle.fd(20)
  95. turtle.pensize(4)
  96. turtle.pendown()
  97. turtle.circle(2, 360)
  98. turtle.setheading(0)
  99. turtle.penup()
  100. turtle.fd(40)
  101. turtle.pensize(4)
  102. turtle.pendown()
  103. turtle.circle(-2, 360)
  104. turtle.penup()
  105. turtle.goto(x, y)
  106. turtle.setheading(-90)
  107. turtle.pendown()
  108. turtle.fd(20)
  109. turtle.setheading(0)
  110. turtle.fd(35)
  111. turtle.setheading(60)
  112. turtle.fd(10)
  113. turtle.penup()
  114. turtle.goto(x, y)
  115. turtle.setheading(-90)
  116. turtle.pendown()
  117. turtle.fd(40)
  118. turtle.setheading(0)
  119. turtle.fd(35)
  120. turtle.setheading(-60)
  121. turtle.fd(10)
  122. turtle.penup()
  123. turtle.goto(x, y)
  124. turtle.setheading(-90)
  125. turtle.pendown()
  126. turtle.fd(60)
  127. turtle.setheading(-135)
  128. turtle.fd(60)
  129. turtle.bk(60)
  130. turtle.setheading(-45)
  131. turtle.fd(30)
  132. turtle.setheading(-135)
  133. turtle.fd(35)
  134. turtle.penup()
  135. # 第一个画面,显示文字
  136. def page0():
  137. turtle.penup()
  138. turtle.goto(-350, 0)
  139. turtle.color('black')
  140. turtle.write('专属于我们的什么节', font=('宋体', 60, 'normal')) #这里的字大家自己替换
  141. time.sleep(3)
  142. # 第二个画面,显示发射爱心的小人
  143. def page1():
  144. turtle.speed(10)
  145. draw_people(-250, 20)
  146. turtle.penup()
  147. turtle.goto(-150, -30)
  148. draw_heart(14)
  149. turtle.penup()
  150. turtle.goto(-20, -60)
  151. draw_heart(25)
  152. turtle.penup()
  153. turtle.goto(250, -100)
  154. draw_heart(45)
  155. turtle.hideturtle()
  156. time.sleep(3)
  157. # 最后一个画面,一箭穿心
  158. def page2():
  159. turtle.speed(1)
  160. turtle.penup()
  161. turtle.goto(-200, -200)
  162. turtle.color('blue')
  163. turtle.pendown()
  164. turtle.write('温轻舟 温轻舟', font=('wisdom', 25, 'normal')) #这里的字大家自己替换
  165. turtle.penup()
  166. turtle.goto(0, -180)
  167. draw_heart(10)
  168. arrow_heart(20, -60, 51)
  169. turtle.showturtle()
  170. def main():
  171. turtle.setup(900, 500)
  172. page0()
  173. clear_all()
  174. page1()
  175. clear_all()
  176. page2()
  177. turtle.done()
  178. main()
文章知识点与官方知识档案匹配,可进一步学习相关知识
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/532465
推荐阅读
相关标签
  

闽ICP备14008679号