前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python有趣的小案例

Python有趣的小案例

作者头像
小小科
发布2018-06-20 11:17:44
2.6K0
发布2018-06-20 11:17:44
举报
文章被收录于专栏:北京马哥教育北京马哥教育

来源:python

ID:python6359

美国队长的锅

emmmmmmmm.......没错就是他的锅

代码

  1. # 所需依赖:python3 pycharm
  2. # print 打印
  3. print('hello world!')
  4. # 注释符号
  5. # 井号后面灰色的内容是注释,相当于笔记,会被机器忽略
  6. # 变量和值
  7. # n 是变量, 100 是值,等号的作用是赋值
  8. # n 相当于高中数学的 xyz ,只不过 xyz 的值只能是数字,变量的功能要更强大
  9. n = 100
  10. m = 'hello'
  11. print(n)
  12. print(m)
  13. # 数据类型,这里只讲两个,剩下的需要同学自己去系统地学习了
  14. # 字符串 和 整数
  15. # 100 是整数类型
  16. # 'hello' 是字符串类型
  17. # 导入 turtle 模块
  18. # 模块是 python 自带的工具箱,这里将工具箱导入就能使用了
  19. # turtle 模块是 python 用来画图的工具箱
  20. import turtle
  21. # 将 turtle 里的工具拿出来,赋给 t 变量
  22. # 照猫画虎用就是了,这些东西要到很后面才能理解
  23. t = turtle.Turtle()
  24. # 这一行用来加快画笔速度,从 1~9 依次变快,但 0 是最快
  25. t.speed(0)
  26. # 这是向前走,单位是像素
  27. t.forward(100)
  28. # 这是转弯,单位是角度
  29. t.right(120)
  30. t.forward(100)
  31. t.right(120)
  32. t.forward(100)
  33. t.right(120)
  34. # 复制三次,就画了一个三角形
  35. # 正方形
  36. # 长方形
  37. # 如果我们需要改变三角形的边长怎么办?
  38. # 这就要用到变量了,到时候只需改变变量就能改变长度
  39. # 如果有相同的变量,后面定义的会覆盖前面的
  40. l = 200
  41. t.forward(l)
  42. t.right(120)
  43. t.forward(l)
  44. t.right(120)
  45. t.forward(l)
  46. t.right(120)
  47. # for 循环
  48. # 循环还有 while 循环,考虑到用不着就不讲了
  49. # 循环用来处理重复的事情
  50. # range() 是一个区间
  51. # range(3) 相当于 0 1 2
  52. # range(5) 相当于 0 1 2 3 4
  53. # i 取的是 range() 里的值,一次取一个,取一次就循环一次
  54. # 冒号后面必有缩进,缩进的代表是同一个代码块
  55. # 照着用就行了,注意一个字符都不能敲错,不能用中文符号
  56. for i in range(3):
  57. t.forward(l)
  58. t.right(120)
  59. # 如果想画两个三角形怎么办,再复制一个 for 循环?
  60. # 我们用函数将代码封装起来,到时候直接调用就好了
  61. # def 关键字用来定义函数, triangle 是函数名
  62. # 必须要有冒号接缩进,函数里面也是一个代码块
  63. def triangle():
  64. for i in range(3):
  65. t.forward(l)
  66. t.right(120)
  67. # 函数的调用
  68. # triangle()
  69. # 函数可以传递参数进去
  70. def triangle2(l):
  71. for i in range(3):
  72. t.forward(l)
  73. t.right(120)
  74. # 需要传递个参数进去才能调用这个函数
  75. # triangle2(250)
  76. # 定一个函数画长方形
  77. # 四则运算
  78. # + 加
  79. # - 减
  80. # * 乘
  81. # / 除
  82. # // 整除
  83. # % 取余
  84. # 写一个画 n 边形的通用函数
  85. def polygon(l, n):
  86. angle = 360 / n
  87. for i in range(n):
  88. t.forward(l)
  89. t.right(angle)
  90. # polygon(100, 6)
  91. # 画一个五角星
  92. def five_star(l):
  93. for i in range(5):
  94. t.forward(l)
  95. t.right(144)
  96. # five_star(100)
  97. # 画一个圆
  98. # 边长在 36 以上就是个圆
  99. def circle():
  100. for i in range(36):
  101. t.forward(10)
  102. t.right(15)
  103. # circle()
  104. # 在指定的坐标画图
  105. # 比如要在坐标为 (100, 150) 的位置画个正方形
  106. def square(x, y, l):
  107. t.penup()
  108. t.goto(x, y)
  109. t.pendown()
  110. for i in range(4):
  111. t.forward(l)
  112. t.right(90)
  113. # square(100, 150, 100)
  114. # 将画笔定位封装成函数使用,就能有效去除重复代码
  115. def setpen(x, y):
  116. t.penup()
  117. t.goto(x, y)
  118. t.pendown()
  119. t.setheading(0)
  120. def square(x, y, l):
  121. setpen(x, y)
  122. for i in range(4):
  123. t.forward(l)
  124. t.right(90)
  125. # square(100, 150, 100)
  126. # 画一排正方形,共五个,间隔 10
  127. # 蠢方法
  128. # square(100, 150, 30)
  129. # square(140, 150, 30)
  130. # square(180, 150, 30)
  131. # square(220, 150, 30)
  132. # square(260, 150, 30)
  133. # 使用 for 循环、函数
  134. def square_line(x, y, l, n, dis):
  135. for i in range(n):
  136. inner_x = x + (l + dis) * i
  137. square(inner_x, y, l)
  138. # square_line(100, 150, 30, 6, 10)
  139. # 画一个正方形方阵
  140. def square_matrix(x, y, l, n, dis, m):
  141. for i in range(m):
  142. inner_y = y - (l + dis) * i
  143. square_line(x, inner_y, l, n, dis)
  144. # square_matrix(100, 150, 30, 5, 10, 6)
  145. # 填充颜色,给图形上色
  146. def five_star(l):
  147. t.fillcolor('yello')
  148. t.begin_fill()
  149. for i in range(5):
  150. t.forward(l)
  151. t.right(144)
  152. t.end_fill()
  153. # five_star(100)
  154. # 字典的简单用法
  155. # 抽象画
  156. # for i in range(500):
  157. # t.forward(i)
  158. # t.left(90)
  159. # for i in range(500):
  160. # t.forward(i)
  161. # t.left(91)
  162. colors = ['red', 'yellow', 'blue', 'green']
  163. # for i in range(500):
  164. # t.pencolor(colors[i % 4])
  165. # t.circle(i)
  166. # t.left(91)
  167. # sides = 5
  168. # colors = ['red', 'yellow', 'blue', 'orange', 'green', 'purple']
  169. # for i in range(360):
  170. # t.pencolor(colors[i % sides])
  171. # t.forward(i * 3 / sides + i)
  172. # t.left(360 / sides + 1)
  173. # t.width(i * sides / 200)
  174. # 美队盾牌
  175. def circle(x, y, r, color):
  176. n = 36
  177. angle = 360 / n
  178. pi = 3.1415926
  179. c = 2 * pi * r
  180. l = c / n
  181. start_x = x - l / 2
  182. start_y = y + r
  183. setpen(start_x, start_y)
  184. t.pencolor(color)
  185. t.fillcolor(color)
  186. t.begin_fill()
  187. for i in range(n):
  188. t.forward(l)
  189. t.right(angle)
  190. t.end_fill()
  191. def five_star(l):
  192. setpen(0, 0)
  193. t.setheading(162)
  194. t.forward(150)
  195. t.setheading(0)
  196. t.fillcolor('WhiteSmoke')
  197. t.begin_fill()
  198. t.hideturtle()
  199. t.penup()
  200. for i in range(5):
  201. t.forward(l)
  202. t.right(144)
  203. t.end_fill()
  204. def sheild():
  205. circle(0, 0, 300, 'red')
  206. circle(0, 0, 250, 'white')
  207. circle(0, 0, 200, 'red')
  208. circle(0, 0, 150, 'blue')
  209. five_star(284)
  210. sheild()
  211. # 结尾这一行必须有,照着用就行了
  212. turtle.done()

效果图

小猪佩奇

代码

  1. # coding:utf-8
  2. import turtle as t
  3. t.pensize(4)
  4. t.hideturtle()
  5. t.colormode(255)
  6. t.color((255,155,192),"pink")
  7. t.setup(840,500)
  8. t.speed(10)
  9. #鼻子
  10. t.pu()
  11. t.goto(-100,100)
  12. t.pd()
  13. t.seth(-30)
  14. t.begin_fill()
  15. a=0.4
  16. for i in range(120):
  17. if 0<=i<30 or 60<=i<90:
  18. a=a+0.08
  19. t.lt(3) #向左转3度
  20. t.fd(a) #向前走a的步长
  21. else:
  22. a=a-0.08
  23. t.lt(3)
  24. t.fd(a)
  25. t.end_fill()
  26. t.pu()
  27. t.seth(90)
  28. t.fd(25)
  29. t.seth(0)
  30. t.fd(10)
  31. t.pd()
  32. t.pencolor(255,155,192)
  33. t.seth(10)
  34. t.begin_fill()
  35. t.circle(5)
  36. t.color(160,82,45)
  37. t.end_fill()
  38. t.pu()
  39. t.seth(0)
  40. t.fd(20)
  41. t.pd()
  42. t.pencolor(255,155,192)
  43. t.seth(10)
  44. t.begin_fill()
  45. t.circle(5)
  46. t.color(160,82,45)
  47. t.end_fill()
  48. #头
  49. t.color((255,155,192),"pink")
  50. t.pu()
  51. t.seth(90)
  52. t.fd(41)
  53. t.seth(0)
  54. t.fd(0)
  55. t.pd()
  56. t.begin_fill()
  57. t.seth(180)
  58. t.circle(300,-30)
  59. t.circle(100,-60)
  60. t.circle(80,-100)
  61. t.circle(150,-20)
  62. t.circle(60,-95)
  63. t.seth(161)
  64. t.circle(-300,15)
  65. t.pu()
  66. t.goto(-100,100)
  67. t.pd()
  68. t.seth(-30)
  69. a=0.4
  70. for i in range(60):
  71. if 0<=i<30 or 60<=i<90:
  72. a=a+0.08
  73. t.lt(3) #向左转3度
  74. t.fd(a) #向前走a的步长
  75. else:
  76. a=a-0.08
  77. t.lt(3)
  78. t.fd(a)
  79. t.end_fill()
  80. #耳朵
  81. t.color((255,155,192),"pink")
  82. t.pu()
  83. t.seth(90)
  84. t.fd(-7)
  85. t.seth(0)
  86. t.fd(70)
  87. t.pd()
  88. t.begin_fill()
  89. t.seth(100)
  90. t.circle(-50,50)
  91. t.circle(-10,120)
  92. t.circle(-50,54)
  93. t.end_fill()
  94. t.pu()
  95. t.seth(90)
  96. t.fd(-12)
  97. t.seth(0)
  98. t.fd(30)
  99. t.pd()
  100. t.begin_fill()
  101. t.seth(100)
  102. t.circle(-50,50)
  103. t.circle(-10,120)
  104. t.circle(-50,56)
  105. t.end_fill()
  106. #眼睛
  107. t.color((255,155,192),"white")
  108. t.pu()
  109. t.seth(90)
  110. t.fd(-20)
  111. t.seth(0)
  112. t.fd(-95)
  113. t.pd()
  114. t.begin_fill()
  115. t.circle(15)
  116. t.end_fill()
  117. t.color("black")
  118. t.pu()
  119. t.seth(90)
  120. t.fd(12)
  121. t.seth(0)
  122. t.fd(-3)
  123. t.pd()
  124. t.begin_fill()
  125. t.circle(3)
  126. t.end_fill()
  127. t.color((255,155,192),"white")
  128. t.pu()
  129. t.seth(90)
  130. t.fd(-25)
  131. t.seth(0)
  132. t.fd(40)
  133. t.pd()
  134. t.begin_fill()
  135. t.circle(15)
  136. t.end_fill()
  137. t.color("black")
  138. t.pu()
  139. t.seth(90)
  140. t.fd(12)
  141. t.seth(0)
  142. t.fd(-3)
  143. t.pd()
  144. t.begin_fill()
  145. t.circle(3)
  146. t.end_fill()
  147. #腮
  148. t.color((255,155,192))
  149. t.pu()
  150. t.seth(90)
  151. t.fd(-95)
  152. t.seth(0)
  153. t.fd(65)
  154. t.pd()
  155. t.begin_fill()
  156. t.circle(30)
  157. t.end_fill()
  158. #嘴
  159. t.color(239,69,19)
  160. t.pu()
  161. t.seth(90)
  162. t.fd(15)
  163. t.seth(0)
  164. t.fd(-100)
  165. t.pd()
  166. t.seth(-80)
  167. t.circle(30,40)
  168. t.circle(40,80)
  169. #身体
  170. t.color("red",(255,99,71))
  171. t.pu()
  172. t.seth(90)
  173. t.fd(-20)
  174. t.seth(0)
  175. t.fd(-78)
  176. t.pd()
  177. t.begin_fill()
  178. t.seth(-130)
  179. t.circle(100,10)
  180. t.circle(300,30)
  181. t.seth(0)
  182. t.fd(230)
  183. t.seth(90)
  184. t.circle(300,30)
  185. t.circle(100,3)
  186. t.color((255,155,192),(255,100,100))
  187. t.seth(-135)
  188. t.circle(-80,63)
  189. t.circle(-150,24)
  190. t.end_fill()
  191. #手
  192. t.color((255,155,192))
  193. t.pu()
  194. t.seth(90)
  195. t.fd(-40)
  196. t.seth(0)
  197. t.fd(-27)
  198. t.pd()
  199. t.seth(-160)
  200. t.circle(300,15)
  201. t.pu()
  202. t.seth(90)
  203. t.fd(15)
  204. t.seth(0)
  205. t.fd(0)
  206. t.pd()
  207. t.seth(-10)
  208. t.circle(-20,90)
  209. t.pu()
  210. t.seth(90)
  211. t.fd(30)
  212. t.seth(0)
  213. t.fd(237)
  214. t.pd()
  215. t.seth(-20)
  216. t.circle(-300,15)
  217. t.pu()
  218. t.seth(90)
  219. t.fd(20)
  220. t.seth(0)
  221. t.fd(0)
  222. t.pd()
  223. t.seth(-170)
  224. t.circle(20,90)
  225. #脚
  226. t.pensize(10)
  227. t.color((240,128,128))
  228. t.pu()
  229. t.seth(90)
  230. t.fd(-75)
  231. t.seth(0)
  232. t.fd(-180)
  233. t.pd()
  234. t.seth(-90)
  235. t.fd(40)
  236. t.seth(-180)
  237. t.color("black")
  238. t.pensize(15)
  239. t.fd(20)
  240. t.pensize(10)
  241. t.color((240,128,128))
  242. t.pu()
  243. t.seth(90)
  244. t.fd(40)
  245. t.seth(0)
  246. t.fd(90)
  247. t.pd()
  248. t.seth(-90)
  249. t.fd(40)
  250. t.seth(-180)
  251. t.color("black")
  252. t.pensize(15)
  253. t.fd(20)
  254. #尾巴
  255. t.pensize(4)
  256. t.color((255,155,192))
  257. t.pu()
  258. t.seth(90)
  259. t.fd(70)
  260. t.seth(0)
  261. t.fd(95)
  262. t.pd()
  263. t.seth(0)
  264. t.circle(70,20)
  265. t.circle(10,330)
  266. t.circle(70,30)
  267. t.done()

效果图

蓝胖子

代码

  1. # !/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # @Author: dong dong
  4. # @Env: python 3.6
  5. from turtle import *
  6. # 无轨迹跳跃
  7. def my_goto(x, y):
  8. penup()
  9. goto(x, y)
  10. pendown()
  11. # 眼睛
  12. def eyes():
  13. tracer(False)
  14. a = 2.5
  15. for i in range(120):
  16. if 0 <= i < 30 or 60 <= i < 90:
  17. a -= 0.05
  18. lt(3)
  19. fd(a)
  20. else:
  21. a += 0.05
  22. lt(3)
  23. fd(a)
  24. tracer(True)
  25. # 胡须
  26. def beard():
  27. my_goto(-37, 135)
  28. seth(165)
  29. fd(60)
  30. my_goto(-37, 125)
  31. seth(180)
  32. fd(60)
  33. my_goto(-37, 115)
  34. seth(193)
  35. fd(60)
  36. my_goto(37, 135)
  37. seth(15)
  38. fd(60)
  39. my_goto(37, 125)
  40. seth(0)
  41. fd(60)
  42. my_goto(37, 115)
  43. seth(-13)
  44. fd(60)
  45. # 嘴巴
  46. def mouth():
  47. my_goto(5, 148)
  48. seth(270)
  49. fd(100)
  50. seth(0)
  51. circle(120, 50)
  52. seth(230)
  53. circle(-120, 100)
  54. # 围巾
  55. def scarf():
  56. fillcolor('#e70010')
  57. begin_fill()
  58. seth(0)
  59. fd(200)
  60. circle(-5, 90)
  61. fd(10)
  62. circle(-5, 90)
  63. fd(207)
  64. circle(-5, 90)
  65. fd(10)
  66. circle(-5, 90)
  67. end_fill()
  68. # 鼻子
  69. def nose():
  70. my_goto(-10, 158)
  71. fillcolor('#e70010')
  72. begin_fill()
  73. circle(20)
  74. end_fill()
  75. # 黑眼睛
  76. def black_eyes():
  77. seth(0)
  78. my_goto(-20, 195)
  79. fillcolor('#000000')
  80. begin_fill()
  81. circle(13)
  82. end_fill()
  83. pensize(6)
  84. my_goto(20, 205)
  85. seth(75)
  86. circle(-10, 150)
  87. pensize(3)
  88. my_goto(-17, 200)
  89. seth(0)
  90. fillcolor('#ffffff')
  91. begin_fill()
  92. circle(5)
  93. end_fill()
  94. my_goto(0, 0)
  95. # 脸
  96. def face():
  97. fd(183)
  98. fillcolor('#ffffff')
  99. begin_fill()
  100. lt(45)
  101. circle(120, 100)
  102. seth(90)
  103. eyes()
  104. seth(180)
  105. penup()
  106. fd(60)
  107. pendown()
  108. seth(90)
  109. eyes()
  110. penup()
  111. seth(180)
  112. fd(64)
  113. pendown()
  114. seth(215)
  115. circle(120, 100)
  116. end_fill()
  117. # 头型
  118. def head():
  119. penup()
  120. circle(150, 40)
  121. pendown()
  122. fillcolor('#00a0de')
  123. begin_fill()
  124. circle(150, 280)
  125. end_fill()
  126. # 画哆啦A梦
  127. def Doraemon():
  128. # 头部
  129. head()
  130. # 围脖
  131. scarf()
  132. # 脸
  133. face()
  134. # 红鼻子
  135. nose()
  136. # 嘴巴
  137. mouth()
  138. # 胡须
  139. beard()
  140. # 身体
  141. my_goto(0, 0)
  142. seth(0)
  143. penup()
  144. circle(150, 50)
  145. pendown()
  146. seth(30)
  147. fd(40)
  148. seth(70)
  149. circle(-30, 270)
  150. fillcolor('#00a0de')
  151. begin_fill()
  152. seth(230)
  153. fd(80)
  154. seth(90)
  155. circle(1000, 1)
  156. seth(-89)
  157. circle(-1000, 10)
  158. # print(pos())
  159. seth(180)
  160. fd(70)
  161. seth(90)
  162. circle(30, 180)
  163. seth(180)
  164. fd(70)
  165. # print(pos())
  166. seth(100)
  167. circle(-1000, 9)
  168. seth(-86)
  169. circle(1000, 2)
  170. seth(230)
  171. fd(40)
  172. # print(pos())
  173. circle(-30, 230)
  174. seth(45)
  175. fd(81)
  176. seth(0)
  177. fd(203)
  178. circle(5, 90)
  179. fd(10)
  180. circle(5, 90)
  181. fd(7)
  182. seth(40)
  183. circle(150, 10)
  184. seth(30)
  185. fd(40)
  186. end_fill()
  187. # 左手
  188. seth(70)
  189. fillcolor('#ffffff')
  190. begin_fill()
  191. circle(-30)
  192. end_fill()
  193. # 脚
  194. my_goto(103.74, -182.59)
  195. seth(0)
  196. fillcolor('#ffffff')
  197. begin_fill()
  198. fd(15)
  199. circle(-15, 180)
  200. fd(90)
  201. circle(-15, 180)
  202. fd(10)
  203. end_fill()
  204. my_goto(-96.26, -182.59)
  205. seth(180)
  206. fillcolor('#ffffff')
  207. begin_fill()
  208. fd(15)
  209. circle(15, 180)
  210. fd(90)
  211. circle(15, 180)
  212. fd(10)
  213. end_fill()
  214. # 右手
  215. my_goto(-133.97, -91.81)
  216. seth(50)
  217. fillcolor('#ffffff')
  218. begin_fill()
  219. circle(30)
  220. end_fill()
  221. # 口袋
  222. my_goto(-103.42, 15.09)
  223. seth(0)
  224. fd(38)
  225. seth(230)
  226. begin_fill()
  227. circle(90, 260)
  228. end_fill()
  229. my_goto(5, -40)
  230. seth(0)
  231. fd(70)
  232. seth(-90)
  233. circle(-70, 180)
  234. seth(0)
  235. fd(70)
  236. #铃铛
  237. my_goto(-103.42, 15.09)
  238. fd(90)
  239. seth(70)
  240. fillcolor('#ffd200')
  241. # print(pos())
  242. begin_fill()
  243. circle(-20)
  244. end_fill()
  245. seth(170)
  246. fillcolor('#ffd200')
  247. begin_fill()
  248. circle(-2, 180)
  249. seth(10)
  250. circle(-100, 22)
  251. circle(-2, 180)
  252. seth(180-10)
  253. circle(100, 22)
  254. end_fill()
  255. goto(-13.42, 15.09)
  256. seth(250)
  257. circle(20, 110)
  258. seth(90)
  259. fd(15)
  260. dot(10)
  261. my_goto(0, -150)
  262. # 画眼睛
  263. black_eyes()
  264. if __name__ == '__main__':
  265. screensize(800,600, "#f0f0f0")
  266. pensize(3) # 画笔宽度
  267. speed(9) # 画笔速度
  268. Doraemon()
  269. my_goto(100, -300)
  270. write('by dongdong', font=("Bradley Hand ITC", 30, "bold"))
  271. mainloop()

效果图

马哥教育618年中大促进行中

四重福利十重豪礼等你来拿!

了解详情请点击图片:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 马哥Linux运维 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档