Python有趣的小案例

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

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )