1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
| """ 改编自turtle.py自带demo 执行 python -m turtledemo 命令查看系统内置demo的源码
注意:1.全文中的 turtle.xxx()中的turtle本质是该海龟屏幕上默认的海龟实例对象,他等价于turtle.getturtle()返回的对象实例.即任何turtle.xxx()函数都可以写成turtle.getturtle.xxx() 2.屏幕正中心为(0,0)点 """ import turtle import time
def demo1(): turtle.reset() turtle.tracer(True) turtle.pu() turtle.bk(100) turtle.pd() turtle.pensize(3) for i in range(3): print("当前i=", i)
if i == 2: turtle.begin_fill()
turtle.write(i + 1, False, align="right")
draw_square(turtle.getturtle())
if i == 2: turtle.color("maroon") turtle.end_fill()
turtle.pu() turtle.fd(30) turtle.pd()
turtle.pensize(1) turtle.color("black")
turtle.tracer(False) turtle.pu() turtle.rt(90) turtle.fd(100) turtle.rt(90) turtle.fd(100) turtle.rt(180) turtle.pd() turtle.write("画楼梯", True) turtle.write("start", True) turtle.color("red") for i in range(5): turtle.fd(20) turtle.lt(90) turtle.fd(20) turtle.rt(90) turtle.tracer(True) turtle.begin_fill() for i in range(5): turtle.fd(20) turtle.lt(90) turtle.fd(20) turtle.rt(90) turtle.end_fill()
def switchpen(_turtle): """ 间断效果 :param _turtle: 将被处理的turtle对象 """ if _turtle.isdown(): _turtle.pu() else: _turtle.pd()
def draw_square(_turtle): """ 画边长20像素的正方形 :param _turtle: 将被处理的turtle对象 """ """ old = _turtle.heading() _turtle.setheading(315) _turtle.circle(15, None, 4) _turtle.setheading(old) """ for _ in range(4): _turtle.fd(20) _turtle.lt(90)
def demo2(): """Demo of some new features.""" turtle.speed(1) turtle.pensize(3) turtle.setheading(turtle.towards(0, 0)) radius = turtle.distance(0, 0)/2.0 turtle.rt(90) for _ in range(18): switchpen(turtle.getturtle()) turtle.circle(radius, 10) turtle.write("wait a moment...")
time.sleep(2)
while turtle.undobufferentries(): turtle.undo() turtle.reset() turtle.lt(90) turtle.colormode(255) laenge = 10 turtle.pencolor("green") turtle.pensize(3) turtle.lt(180) for i in range(-2, 16): print("当前i=", i) if i > 0: turtle.begin_fill() turtle.fillcolor(255-15*i, 0, 15*i) for _ in range(3): turtle.fd(laenge) turtle.lt(120) if i > 0: turtle.end_fill()
laenge += 10 turtle.lt(15)
turtle.speed((turtle.speed()+1) % 12)
turtle.lt(120) turtle.pu() turtle.fd(70) turtle.rt(30) turtle.pd() turtle.color("red", "yellow") turtle.speed(0) turtle.begin_fill() for _ in range(4): turtle.circle(50, 90) turtle.rt(90) turtle.fd(30) turtle.rt(90) turtle.end_fill() turtle.lt(90) turtle.pu() turtle.fd(30) turtle.pd() turtle.shape("turtle") tri = turtle.getturtle() tri.resizemode("auto") turtle1 = turtle.Turtle() turtle1.resizemode("auto") turtle1.shape("turtle") turtle1.reset() turtle1.lt(90) turtle1.speed(0) turtle1.pu() turtle1.goto(280, 40) turtle1.lt(30) turtle1.pd() turtle1.speed(6) turtle1.color("blue", "orange") turtle1.pensize(2) tri.speed(6) tri.setheading(tri.towards(turtle1)) count = 1 while tri.distance(turtle1) > 4: turtle1.fd(3.5) turtle1.lt(0.6) tri.setheading(tri.towards(turtle1)) tri.fd(4) if count % 20 == 0: turtle1.stamp() tri.stamp() switchpen(tri) count += 1 tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right") tri.pencolor("black") tri.pencolor("red")
time.sleep(2)
while turtle.undobufferentries(): tri.undo() turtle1.undo() tri.fd(50) tri.write(" Click anywhere to exit!", font=("Courier", 12, "bold")) screen = turtle.getscreen() screen.onclick(close_window)
def close_window(x, y): print("点击位置(", x, ",", y, ")") screen = turtle.getscreen() screen.clearscreen() screen.bye()
def my_main(): demo1() demo2() turtle.mainloop()
if __name__ == '__main__': my_main()
|