|
| 1 | +<h2> Turtle Module </h2> |
| 2 | + |
| 3 | +**Turtle Graphics** |
| 4 | + |
| 5 | +``` |
| 6 | +import turtle |
| 7 | +scrn = turtle.Screen() #creates a graphics window |
| 8 | +sponge = turtle.Turtle() #creates a turtle whose name is sponge |
| 9 | +sponge.forward(200) #object.method(parameter) |
| 10 | +sponge.left(90) |
| 11 | +sponge.forward(100) |
| 12 | +sponge.right(90) |
| 13 | +sponge.forward(100) |
| 14 | +sponge.left(90) |
| 15 | +sponge.backward(30) |
| 16 | +``` |
| 17 | + |
| 18 | +``` |
| 19 | +#import turtle defines the module turtle which will allow you to create a Turtle object and draw with it. |
| 20 | +#turtle.Turtle; here "turtle" tells Python that we are referring to the turtle module, which is where the object "Turtle" is found |
| 21 | +``` |
| 22 | + |
| 23 | +**Creating a Rectangle** |
| 24 | + |
| 25 | +``` |
| 26 | +import turtle #here loads a module named turtle |
| 27 | + #This module brings two new types: the Turtle type, and the Screen type. |
| 28 | +scrn = turtle.Screen() #creates a graphics window |
| 29 | + #scrn is an instance of Screen class |
| 30 | +ciri = turtle.Turtle() #means the Turtle type that is defined within the turtle module |
| 31 | + #ciri is an instance of Turtle class |
| 32 | +ciri.forward(180) #object.method(parameter) |
| 33 | +ciri.left(90) |
| 34 | +ciri.forward(75) |
| 35 | +ciri.left(90) |
| 36 | +ciri.forward(180) |
| 37 | +ciri.left(90) |
| 38 | +ciri.forward(75) |
| 39 | +``` |
| 40 | +**Creating a triangle** |
| 41 | + |
| 42 | +``` |
| 43 | +import turtle |
| 44 | +scrn = turtle.Screen() |
| 45 | +mini = turtle.Turtle() |
| 46 | +mini.forward(180) |
| 47 | +mini.left(150) |
| 48 | +mini.forward(100) #object.method(parameter) |
| 49 | +mini.left(60) |
| 50 | +mini.forward(100) |
| 51 | +``` |
| 52 | +**Creating rectangle and triangle together** |
| 53 | + |
| 54 | +``` |
| 55 | +import turtle |
| 56 | +scrn = turtle.Screen() |
| 57 | +ciri = turtle.Turtle() |
| 58 | +ciri.forward(180) #object.method(parameter) |
| 59 | +ciri.left(90) |
| 60 | +ciri.forward(75) |
| 61 | +ciri.left(90) |
| 62 | +ciri.forward(180) |
| 63 | +ciri.left(90) |
| 64 | +ciri.forward(75) |
| 65 | + |
| 66 | +mini = turtle.Turtle() |
| 67 | +mini.forward(180) |
| 68 | +mini.left(150) |
| 69 | +mini.forward(100) #object.method(parameter) |
| 70 | +mini.left(60) |
| 71 | +mini.forward(100) |
| 72 | +``` |
| 73 | + |
| 74 | +**Using properties** |
| 75 | + |
| 76 | +``` |
| 77 | +import turtle |
| 78 | +scrn = turtle.Screen() |
| 79 | +scrn.bgcolor("lavender") |
| 80 | +#the object scrn has color property(which we write as bgcolor) |
| 81 | +arin = turtle.Turtle() |
| 82 | +arin.color("blue") |
| 83 | +arin.pensize(3) |
| 84 | +#the object arin has property/attribute - color,pensize |
| 85 | +arin.forward(100) |
| 86 | +arin.right(90) #name.right(90) goes downward |
| 87 | +arin.forward(90) |
| 88 | + |
| 89 | +arina = turtle.Turtle() |
| 90 | +arina.color("hot pink") |
| 91 | +arin.pensize(4) |
| 92 | +arina.forward(100) |
| 93 | +arina.left(90) #name.left(90) goes upward |
| 94 | +arina.forward(90) |
| 95 | + |
| 96 | +#name.right(value)/name.left(value) works for defining angles(degrees). |
| 97 | +``` |
| 98 | +**Mutliple objects with properties** |
| 99 | + |
| 100 | +``` |
| 101 | +import turtle |
| 102 | +scrn = turtle.Screen() |
| 103 | +scrn.bgcolor("lavender") |
| 104 | +#the object scrn has color property(which we write as bgcolor) |
| 105 | +arin = turtle.Turtle() |
| 106 | +arin.color("blue") |
| 107 | +arin.pensize(3) |
| 108 | +#the object arin has property/attribute - color,pensize |
| 109 | +arin.forward(100) |
| 110 | +arin.right(90) #name.right(90) goes downward |
| 111 | +arin.forward(90) |
| 112 | + |
| 113 | +arina = turtle.Turtle() |
| 114 | +arina.color("hot pink") |
| 115 | +arin.pensize(4) |
| 116 | +arina.forward(100) |
| 117 | +arina.left(90) #name.left(90) goes upward |
| 118 | +arina.forward(90) |
| 119 | + |
| 120 | +#name.right(value)/name.left(value) works for defining angles(degrees). |
| 121 | +ciri = turtle.Turtle() |
| 122 | +ciri.color("yellow") |
| 123 | +ciri.forward(180) #object.method(parameter) |
| 124 | +ciri.left(90) |
| 125 | +ciri.forward(75) |
| 126 | +ciri.left(90) |
| 127 | +ciri.forward(180) |
| 128 | +ciri.left(90) |
| 129 | +ciri.forward(75) |
| 130 | + |
| 131 | +mini = turtle.Turtle() |
| 132 | +mini.forward(180) |
| 133 | +mini.left(150) |
| 134 | +mini.forward(100) #object.method(parameter) |
| 135 | +mini.left(60) |
| 136 | +mini.forward(100) |
| 137 | + |
| 138 | +prity = turtle.Turtle() |
| 139 | +prity.color("green") |
| 140 | +arin.pensize(2) |
| 141 | +prity.right(45) |
| 142 | +prity.forward(60) |
| 143 | +prity.left(90) |
| 144 | +prity.forward(100) |
| 145 | + |
| 146 | +zina = turtle.Turtle() |
| 147 | +zina.color("red") |
| 148 | +zina.pensize(3) |
| 149 | +zina.left(180) #notice this |
| 150 | +zina.forward(150) |
| 151 | + |
| 152 | +scrn.exitonclick() # wait for a user click on the canvas |
| 153 | +#we invoke its exitonclick method of scrn object, the program pauses execution |
| 154 | +#and waits for the user to click the mouse somewhere in the window |
| 155 | + |
| 156 | +``` |
0 commit comments