Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Programming Art, Ch01~Ch03, _all_.py

Renyuan Lyu
November 20, 2014
500

Python Programming Art, Ch01~Ch03, _all_.py

Renyuan Lyu

November 20, 2014
Tweet

Transcript

  1. Python Programming Art Ch01 ~ Ch03 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 A B C ''' _all_.py reEdit by Renyuan, 2014/11/20 ''' #### 0006, Code Box 1.2 # hello_world.py introduces the turtle module from turtle import * # change line width pensize(5) # change to an actual turtle shape("turtle") # draw the letter H left(90) forward(100) back(50) right(90) forward(40) left(90) forward(50) back(100) # Move to start of next letter penup() right(90) forward(40) left(90) pendown() # Draw the letter i forward(50) penup() forward(25) # tell tkinter to stop waiting for turtle instructions done() #### 0001, Code Box 2.1, 2.2 1
  2. Python Programming Art Ch01 ~ Ch03 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 A B C # circle.py from turtle import * # Draw a circle with radius of 50pixels circle(50) # Draw a semicircle with a radius 100pixels circle(100, extent=180) # Draw a triangle which fits in a circle with a radius of 100pixels circle(100, steps=3) # Draw a pentagon which fits in a circle with a radius of 50pixels circle(50, steps=5) # Tell Python to stop waiting for turtle instructions done() #### 0003, Code Box 2.3 # colourful_circle.py from turtle import * pencolor("red") fillcolor("violet") begin_fill() circle(100) end_fill() pencolor("black") circle(50) # Tell Python to stop waiting for turtle instructions done() #### 0008, Code Box 2.4 # square.py from turtle import * # set up some variables side_length=50 n=0 2
  3. Python Programming Art Ch01 ~ Ch03 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 A B C while n < 4: forward(side_length) right(90) n = n+1 # Tell Python to stop waiting for turtle instructions done() #### 0010, Code Box 2.5 # squares.py from turtle import * def square(side_length): n=0 while n < 4: forward(side_length) right(90) n = n+1 # Draw the first square with 50pixel sides left(90) square(50) # move the turtle penup() right(90) forward(100) left(90) pendown() # draw the second square square(50) # Tell Python to stop waiting for turtle instructions done() #### 0009, Code Box 2.7 # square2.py import turtle 3
  4. Python Programming Art Ch01 ~ Ch03 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 A B C t=turtle def square(side_length): n=0 while n < 4: t.forward(side_length) t.right(90) n = n+1 # Draw a square square(50) # end t.done() #### 0007, Code Box 3.1 # many_circles.py import turtle t=turtle #set variables size=50 distance=50 angle=20 # speed things up t.speed(0) # 1=slow, 10=fast, 0=fastest # Draw the pattern n=0 while n < 18: # draw a circle t.pendown() t.circle(size) t.penup() # move start position t.forward(distance) t.left(angle) n=n+1 # hide the turtle when it has finished drawing 4
  5. Python Programming Art Ch01 ~ Ch03 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 A B C t.hideturtle() # end t.done() #### 0002, Code Box 3.2 # colourful.py import turtle t=turtle #set variables size=50 distance=50 angle=20 t.speed(0) t.shape("turtle") t.bgcolor("blue") n=0 while n < 18: # draw a square t.pendown() t.pensize(4) t.color("yellow") t.circle(size, steps=4) t.penup() # move start position t.forward(distance) t.left(angle) n=n+1 t.setposition(5,30) size=50 distance=40 n=0 while n < 18: # draw a circle t.pendown() t.pensize(4) t.color("orange") 5
  6. Python Programming Art Ch01 ~ Ch03 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 A B C t.circle(size) t.penup() # move start position t.forward(distance) t.left(angle) n=n+1 # move turtle t.setposition(22,144) t.color("red") # end t.done() #### 0004, Code Box 3.3 # flower.py # this is a really cool little program slightly adapted from: # http://docs.python.org/3.3/library/turtle.html import turtle t=turtle t.speed(8) # set the colour and fill values in one go t.color("green", "purple") # draw the star t.begin_fill() while True : t.forward(150) t.left(170) if t.distance(0,0)<1: break t.end_fill() # hide the turtle when it has finished drawing t.hideturtle() # end t.done() 6
  7. Python Programming Art Ch01 ~ Ch03 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 314 315 316 317 318 A B C #### 0005, Code Box 3.4 # fractal_tree.py import turtle t=turtle # set variables detail=12 #decrease for more branches length=80 # increase for larger tree thickness=20 # vary to see effect angle=20 # vary to see effect t.speed(0) # 1=slow, 10=fast, 0=fastest def draw_tree(branch_thickness, branch_length): if branch_length > 5: if branch_length < 20: t.color("green") else : t.color("brown") t.pensize(branch_thickness) t.forward(branch_length) t.right(angle) draw_tree(branch_thickness/1.5, branch_length-detail) t.left(2*angle) draw_tree(branch_thickness/1.5, branch_length-detail) t.right(angle) t.back(branch_length) t.color("brown") # move turtle down the screen and turn to face up t.left(90) t.penup() t.back(100) t.pendown() # set pen color and call the main function t.color("brown") draw_tree(thickness, length) t.done() 7