# TurtleTutorial.py (Source)

  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 #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Mar 23 13:04:59 2017 @author: Vincent Gregoire # Turtle tutorial Prepared by [Vincent Grégoire](http://www.vincentgregoire.com), Department of Finance, The University of Melbourne. This is a sample code to illustrate some basic features of the Python language. This notebook was created as supplemental material to a Python for financial research bootcamp for finance honours and PhD students at the University of Melbourne in March of 2017. Last update: March 24, 2017. **Contact**: Latest version: """ import turtle # Let's create our turtle. We'll call it Bob bob = turtle.Turtle() # He doesn't look much like a turtle, but we can fix that. bob.shape(name='turtle') # We can move Bob different ways. bob.forward(100) bob.backward(100) bob.left(45) # We can draw with Bob bob.pendown() bob.pencolor('red') bob.forward(100) # Let's start again bob.reset() # Let's draw a square the hard way. bob.forward(100) bob.right(90) bob.forward(100) bob.right(90) bob.forward(100) bob.right(90) bob.forward(100) bob.right(90) # That's no good. We can use loops for that (Exercise!) bob.reset() for i in range(4): bob.forward(100) bob.right(90) # Now let's write a function to draw a square of any size def drawSquare(size): for i in range(4): bob.forward(size) bob.right(90) drawSquare(50) drawSquare(200) # We can even add an optional color argument, and take any turtle! def drawSquare(turt, size, color=None): if color is not None: # Save current pen details old_color = turt.getpen().color()[0] turt.pencolor(color) for i in range(4): turt.forward(size) turt.right(90) if color is not None: # Reset pen details turt.pencolor(old_color) drawSquare(bob, 200) drawSquare(bob, 200, 'red') # Say we want to draw squares of squares size (!?!?!) bob.reset() squares = [x*x for x in range(1,21)] for x in squares: drawSquare(bob, x) # Say we wanted a function to draw any Shape? def drawShape(turt, size, sides): # First, we need the angle. A full loop is 360 degrees, each angle is # a fraction of that. We have the same number of angles as sides. angle = 360.0/sides for i in range(sides): turt.forward(size) turt.right(angle) drawShape(bob, 100,10) drawShape(bob, 10,30) bob.reset() # We can draw more complex shapes by combining them # Say we want a house def drawHouse(turt, size): drawSquare(turt, size) turt.left(60) drawShape(turt, size, 3) turt.right(60) turt.penup() turt.right(45) turt.forward(size/5.0) turt.left(45) turt.pendown() drawSquare(turt, size/2.5) turt.penup() turt.right(45) turt.backward(size/5.0) turt.left(45) turt.pendown() drawHouse(bob, 100) # Now we can draw a full neighborhood... but it's a lot of work deciding where # to place the house. Let's leave it to chance. bob.reset() from numpy.random import random # random() will return a number between 0 and 1. random() # We can generate many at a time random(5) def drawNeigborhood(turt, houses=10): # In this case, we need 3 numbers: size, x and y coordinates. # But it looks better if size is a function of the y coordinate # (close == looks bigger). rnd = random((houses, 2)) # Size is between 10 and 80, x and y between -200 and +200 rnd = (rnd * (200 - (-200))) -200 for i in range(houses): turt.penup() turt.setposition(rnd[i,0], rnd[i, 1]) turt.pendown() y = rnd[i, 1] # For size, we want to smooth it, bigger at -200 (80) at smallest at # back (10) size = 80 - (((y + 200) / 400) * (80 - 10)) drawHouse(turt, size) drawNeigborhood(bob, 5) turtle.done()