forked from MartinSova/Python-Exercises-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexturtle.py
1854 lines (1426 loc) · 54.6 KB
/
exturtle.py
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Do NOT edit this file directly. It is created by boilturtle.py
#
"""
===========================
Turtle functions for Exeter
===========================
Procedural interface to the turtle module allowing more than one turtle.
Copyright (c) Richard Everson, University of Exeter, 2014
Generated at 2014-10-12 12:03
"""
import turtle as _turtle
from turtle import Turtle
from turtle import addshape, bgcolor, bgpic, bye, clearscreen, colormode, delay, exitonclick, getcanvas, getshapes, listen, mainloop, mode, numinput, onkey, onkeypress, onkeyrelease, onscreenclick, ontimer, register_shape, resetscreen, screensize, setup, setworldcoordinates, textinput, title, tracer, turtles, update, window_height, window_width
def back(turtle, distance):
"""Move the turtle backward by distance.
Aliases: back | backward | bk
Arguments:
turtle -- the turtle
distance -- a number
Move the turtle backward by distance ,opposite to the direction the
turtle is headed. Do not change the turtle's heading.
Example:
>>> position(turtle)
(0.00, 0.00)
>>> backward(turtle, 30)
>>> position(turtle)
(-30.00, 0.00)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to back is not a valid turtle"))
turtle.back(distance)
def backward(turtle, distance):
"""Move the turtle backward by distance.
Aliases: back | backward | bk
Arguments:
turtle -- the turtle
distance -- a number
Move the turtle backward by distance ,opposite to the direction the
turtle is headed. Do not change the turtle's heading.
Example:
>>> position(turtle)
(0.00, 0.00)
>>> backward(turtle, 30)
>>> position(turtle)
(-30.00, 0.00)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to backward is not a valid turtle"))
turtle.backward(distance)
def begin_fill(turtle):
"""Called just before drawing a shape to be filled.
Argument:
turtle -- the turtle
Example:
>>> color(turtle, "black", "red")
>>> begin_fill(turtle)
>>> circle(turtle, 60)
>>> end_fill(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to begin_fill is not a valid turtle"))
turtle.begin_fill()
def begin_poly(turtle):
"""Start recording the vertices of a polygon.
Argument:
turtle -- the turtle
Start recording the vertices of a polygon. Current turtle position
is first point of polygon.
Example:
>>> begin_poly(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to begin_poly is not a valid turtle"))
turtle.begin_poly()
def bk(turtle, distance):
"""Move the turtle backward by distance.
Aliases: back | backward | bk
Arguments:
turtle -- the turtle
distance -- a number
Move the turtle backward by distance ,opposite to the direction the
turtle is headed. Do not change the turtle's heading.
Example:
>>> position(turtle)
(0.00, 0.00)
>>> backward(turtle, 30)
>>> position(turtle)
(-30.00, 0.00)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to bk is not a valid turtle"))
turtle.bk(distance)
def circle(turtle, radius, extent=None, steps=None):
""" Draw a circle with given radius.
Arguments:
turtle -- the turtle
radius -- a number
extent (optional) -- a number
steps (optional) -- an integer
Draw a circle with given radius. The center is radius units left
of the turtle; extent - an angle - determines which part of the
circle is drawn. If extent is not given, draw the entire circle.
If extent is not a full circle, one endpoint of the arc is the
current pen position. Draw the arc in counterclockwise direction
if radius is positive, otherwise in clockwise direction. Finally
the direction of the turtle is changed by the amount of extent.
As the circle is approximated by an inscribed regular polygon,
steps determines the number of steps to use. If not given,
it will be calculated automatically. Maybe used to draw regular
polygons.
call: circle(turtle, radius) # full circle
--or: circle(turtle, radius, extent) # arc
--or: circle(turtle, radius, extent, steps)
--or: circle(turtle, radius, steps=6) # 6-sided polygon
Example:
>>> circle(turtle, 50)
>>> circle(turtle, 120, 180) # semicircle
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to circle is not a valid turtle"))
turtle.circle(radius, extent, steps)
def clear(turtle):
"""Delete the turtle's drawings from the screen. Do not move
Argument:
turtle -- the turtle
Delete the turtle's drawings from the screen. Do not move
State and position of the turtle as well as drawings of other
turtles are not affected.
Examples:
>>> clear(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to clear is not a valid turtle"))
turtle.clear()
def clearstamp(turtle, stampid):
"""Delete stamp with given stampid
Arguments:
turtle -- the turtle
stampid - an integer, must be return value of previous stamp(turtle) call.
Example:
>>> color(turtle, "blue")
>>> astamp = stamp(turtle)
>>> fd(turtle, 50)
>>> clearstamp(turtle, astamp)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to clearstamp is not a valid turtle"))
turtle.clearstamp(stampid)
def clearstamps(turtle, n=None):
"""Delete all or first/last n of turtle's stamps.
Optional argument:
n -- an integer
If n is None, delete all of pen's stamps,
else if n > 0 delete first n stamps
else if n < 0 delete last n stamps.
Example:
>>> for i in range(8):
... stamp(turtle); fd(turtle, 30)
...
>>> clearstamps(turtle, 2)
>>> clearstamps(turtle, -2)
>>> clearstamps(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to clearstamps is not a valid turtle"))
turtle.clearstamps(n)
def clone(turtle):
"""Create and return a clone of the
Argument:
turtle -- the turtle
Create and return a clone of the turtle with same position, heading
and turtle properties.
Example (for a Turtle instance named mick):
mick = Turtle()
joe = mick.clone()
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to clone is not a valid turtle"))
return turtle.clone()
def color(turtle, *args):
"""Return or set the pencolor and fillcolor.
Arguments:
turtle -- the turtle
Several input formats are allowed.
They use 0, 1, 2, or 3 arguments as follows:
color(turtle)
Return the current pencolor and the current fillcolor
as a pair of color specification strings as are returned
by pencolor and fillcolor.
color(turtle, colorstring), color(turtle, (r,g,b)), color(turtle, r,g,b)
inputs as in pencolor, set both, fillcolor and pencolor,
to the given value.
color(turtle, colorstring1, colorstring2),
color(turtle, (r1,g1,b1), (r2,g2,b2))
equivalent to pencolor(turtle, colorstring1) and fillcolor(turtle, colorstring2)
and analogously, if the other input format is used.
If turtleshape is a polygon, outline and interior of that polygon
is drawn with the newly set colors.
For mor info see: pencolor, fillcolor
Example:
>>> color(turtle, 'red', 'green')
>>> color(turtle)
('red', 'green')
>>> colormode(255)
>>> color(turtle, (40, 80, 120), (160, 200, 240))
>>> color(turtle)
('#285078', '#a0c8f0')
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to color is not a valid turtle"))
return turtle.color(*args)
def degrees(turtle, fullcircle=360.0):
""" Set angle measurement units to degrees.
Optional argument:
fullcircle - a number
Set angle measurement units, i. e. set number
of 'degrees' for a full circle. Dafault value is
360 degrees.
Example:
>>> left(turtle, 90)
>>> heading(turtle)
90
Change angle measurement unit to grad (also known as gon,
grade, or gradian and equals 1/100-th of the right angle.)
>>> degrees(turtle, 400.0)
>>> heading(turtle)
100
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to degrees is not a valid turtle"))
turtle.degrees(fullcircle)
def distance(turtle, x, y=None):
"""Return the distance from the turtle to (x,y) in turtle step units.
Arguments:
turtle -- the turtle
x -- a number or a pair/vector of numbers or a turtle instance
y -- a number None None
call: distance(turtle, x, y) # two coordinates
--or: distance(turtle, (x, y)) # a pair (tuple) of coordinates
--or: distance(turtle, vec) # e.g. as returned by pos(turtle)
--or: distance(turtle, mypen) # where mypen is another turtle
Example:
>>> pos(turtle)
(0.00, 0.00)
>>> distance(turtle, 30,40)
50.0
>>> pen = Turtle()
>>> pen.forward(77)
>>> distance(turtle, pen)
77.0
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to distance is not a valid turtle"))
return turtle.distance(x, y)
def dot(turtle, size=None, *color):
"""Draw a dot with diameter size, using color.
Optional arguments:
size -- an integer >= 1 (if given)
color -- a colorstring or a numeric color tuple
Draw a circular dot with diameter size, using color.
If size is not given, the maximum of pensize+4 and 2*pensize is used.
Example:
>>> dot(turtle)
>>> fd(turtle, 50); dot(turtle, 20, "blue"); fd(turtle, 50)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to dot is not a valid turtle"))
turtle.dot(size, *color)
def down(turtle):
"""Pull the pen down -- drawing when moving.
Aliases: pendown | pd | down
Argument:
turtle -- the turtle
Example:
>>> pendown(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to down is not a valid turtle"))
turtle.down()
def end_fill(turtle):
"""Fill the shape drawn after the call begin_fill(turtle).
Argument:
turtle -- the turtle
Example:
>>> color(turtle, "black", "red")
>>> begin_fill(turtle)
>>> circle(turtle, 60)
>>> end_fill(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to end_fill is not a valid turtle"))
turtle.end_fill()
def end_poly(turtle):
"""Stop recording the vertices of a polygon.
Argument:
turtle -- the turtle
Stop recording the vertices of a polygon. Current turtle position is
last point of polygon. This will be connected with the first point.
Example:
>>> end_poly(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to end_poly is not a valid turtle"))
turtle.end_poly()
def fd(turtle, distance):
"""Move the turtle forward by the specified distance.
Aliases: forward | fd
Arguments:
turtle -- the turtle
distance -- a number (integer or float)
Move the turtle forward by the specified distance, in the direction
the turtle is headed.
Example:
>>> position(turtle)
(0.00, 0.00)
>>> forward(turtle, 25)
>>> position(turtle)
(25.00,0.00)
>>> forward(turtle, -75)
>>> position(turtle)
(-50.00,0.00)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to fd is not a valid turtle"))
turtle.fd(distance)
def fillcolor(turtle, *args):
""" Return or set the fillcolor.
Arguments:
turtle -- the turtle
Four input formats are allowed:
- fillcolor(turtle)
Return the current fillcolor as color specification string,
possibly in hex-number format (see example).
May be used as input to another color/pencolor/fillcolor call.
- fillcolor(turtle, colorstring)
s is a Tk color specification string, such as "red" or "yellow"
- fillcolor(turtle, (r, g, b))
*a tuple* of r, g, and b, which represent, an RGB color,
and each of r, g, and b are in the range 0..colormode,
where colormode is either 1.0 or 255
- fillcolor(turtle, r, g, b)
r, g, and b represent an RGB color, and each of r, g, and b
are in the range 0..colormode
If turtleshape is a polygon, the interior of that polygon is drawn
with the newly set fillcolor.
Example:
>>> fillcolor(turtle, 'violet')
>>> col = pencolor(turtle)
>>> fillcolor(turtle, col)
>>> fillcolor(turtle, 0, .5, 0)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to fillcolor is not a valid turtle"))
return turtle.fillcolor(*args)
def filling(turtle):
"""Return fillstate (True if filling, False else).
Argument:
turtle -- the turtle
Example:
>>> begin_fill(turtle)
>>> if filling(turtle):
... pensize(turtle, 5)
... else:
... pensize(turtle, 3)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to filling is not a valid turtle"))
return turtle.filling()
def forward(turtle, distance):
"""Move the turtle forward by the specified distance.
Aliases: forward | fd
Arguments:
turtle -- the turtle
distance -- a number (integer or float)
Move the turtle forward by the specified distance, in the direction
the turtle is headed.
Example:
>>> position(turtle)
(0.00, 0.00)
>>> forward(turtle, 25)
>>> position(turtle)
(25.00,0.00)
>>> forward(turtle, -75)
>>> position(turtle)
(-50.00,0.00)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to forward is not a valid turtle"))
turtle.forward(distance)
def get_poly(turtle):
"""Return the lastly recorded polygon.
Argument:
turtle -- the turtle
Example:
>>> p = get_poly(turtle)
>>> register_shape("myFavouriteShape", p)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to get_poly is not a valid turtle"))
return turtle.get_poly()
def get_shapepoly(turtle):
"""Return the current shape polygon as tuple of coordinate pairs.
Argument:
turtle -- the turtle
Examples:
>>> shape(turtle, "square")
>>> shapetransform(turtle, 4, -1, 0, 2)
>>> get_shapepoly(turtle)
((50, -20), (30, 20), (-50, 20), (-30, -20))
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to get_shapepoly is not a valid turtle"))
return turtle.get_shapepoly()
def getpen(turtle):
"""Return the Turtleobject itself.
Argument:
turtle -- the turtle
Only reasonable use: as a function to return the 'anonymous turtle':
Example:
>>> pet = getturtle(turtle)
>>> pet.fd(50)
>>> pet
<Turtle object at 0x0187D810>
>>> turtles()
[<Turtle object at 0x0187D810>]
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to getpen is not a valid turtle"))
return turtle.getpen()
def getscreen(turtle):
"""Return the TurtleScreen object, the turtle is drawing on.
Argument:
turtle -- the turtle
Return the TurtleScreen object, the turtle is drawing on.
So TurtleScreen-methods can be called for that object.
Example:
>>> ts = getscreen(turtle)
>>> ts
<TurtleScreen object at 0x0106B770>
>>> ts.bgcolor("pink")
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to getscreen is not a valid turtle"))
return turtle.getscreen()
def getturtle(turtle):
"""Return the Turtleobject itself.
Argument:
turtle -- the turtle
Only reasonable use: as a function to return the 'anonymous turtle':
Example:
>>> pet = getturtle(turtle)
>>> pet.fd(50)
>>> pet
<Turtle object at 0x0187D810>
>>> turtles()
[<Turtle object at 0x0187D810>]
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to getturtle is not a valid turtle"))
return turtle.getturtle()
def goto(turtle, x, y=None):
"""Move turtle to an absolute position.
Aliases: setpos | setposition | goto:
Arguments:
turtle -- the turtle
x -- a number or a pair/vector of numbers
y -- a number None
call: goto(turtle, x, y) # two coordinates
--or: goto(turtle, (x, y)) # a pair (tuple) of coordinates
--or: goto(turtle, vec) # e.g. as returned by pos(turtle)
Move turtle to an absolute position. If the pen is down,
a line will be drawn. The turtle's orientation does not change.
Example:
>>> tp = pos(turtle)
>>> tp
(0.00, 0.00)
>>> setpos(turtle, 60,30)
>>> pos(turtle)
(60.00,30.00)
>>> setpos(turtle, (20,80))
>>> pos(turtle)
(20.00,80.00)
>>> setpos(turtle, tp)
>>> pos(turtle)
(0.00,0.00)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to goto is not a valid turtle"))
turtle.goto(x, y)
def heading(turtle):
""" Return the turtle's current heading.
Argument:
turtle -- the turtle
Example:
>>> left(turtle, 67)
>>> heading(turtle)
67.0
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to heading is not a valid turtle"))
return turtle.heading()
def hideturtle(turtle):
"""Makes the turtle invisible.
Aliases: hideturtle | ht
Argument:
turtle -- the turtle
It's a good idea to do this while you're in the
middle of a complicated drawing, because hiding
the turtle speeds up the drawing observably.
Example:
>>> hideturtle(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to hideturtle is not a valid turtle"))
turtle.hideturtle()
def home(turtle):
"""Move turtle to the origin - coordinates (0,0).
Argument:
turtle -- the turtle
Move turtle to the origin - coordinates (0,0) and set its
heading to its start-orientation (which depends on mode).
Example:
>>> home(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to home is not a valid turtle"))
turtle.home()
def ht(turtle):
"""Makes the turtle invisible.
Aliases: hideturtle | ht
Argument:
turtle -- the turtle
It's a good idea to do this while you're in the
middle of a complicated drawing, because hiding
the turtle speeds up the drawing observably.
Example:
>>> hideturtle(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to ht is not a valid turtle"))
turtle.ht()
def isdown(turtle):
"""Return True if pen is down, False if it's up.
Argument:
turtle -- the turtle
Example:
>>> penup(turtle)
>>> isdown(turtle)
False
>>> pendown(turtle)
>>> isdown(turtle)
True
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to isdown is not a valid turtle"))
return turtle.isdown()
def isvisible(turtle):
"""Return True if the Turtle is shown, False if it's hidden.
Argument:
turtle -- the turtle
Example:
>>> hideturtle(turtle)
>>> print isvisible(turtle):
False
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to isvisible is not a valid turtle"))
return turtle.isvisible()
def left(turtle, angle):
"""Turn turtle left by angle units.
Aliases: left | lt
Arguments:
turtle -- the turtle
angle -- a number (integer or float)
Turn turtle left by angle units. (Units are by default degrees,
but can be set via the degrees(turtle) and radians(turtle) functions.)
Angle orientation depends on mode. (See this.)
Example:
>>> heading(turtle)
22.0
>>> left(turtle, 45)
>>> heading(turtle)
67.0
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to left is not a valid turtle"))
turtle.left(angle)
def lt(turtle, angle):
"""Turn turtle left by angle units.
Aliases: left | lt
Arguments:
turtle -- the turtle
angle -- a number (integer or float)
Turn turtle left by angle units. (Units are by default degrees,
but can be set via the degrees(turtle) and radians(turtle) functions.)
Angle orientation depends on mode. (See this.)
Example:
>>> heading(turtle)
22.0
>>> left(turtle, 45)
>>> heading(turtle)
67.0
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to lt is not a valid turtle"))
turtle.lt(angle)
def onclick(turtle, fun, btn=1, add=None):
"""Bind fun to mouse-click event on this turtle on canvas.
Arguments:
turtle -- the turtle
fun -- a function with two arguments, to which will be assigned
the coordinates of the clicked point on the canvas.
num -- number of the mouse-button defaults to 1 (left mouse button).
add -- True or False. If True, new binding will be added, otherwise
it will replace a former binding.
Example for the anonymous turtle, i. e. the procedural way:
>>> def turn(x, y):
... left(turtle, 360)
...
>>> onclick(turtle, turn) # Now clicking into the turtle will turn it.
>>> onclick(turtle, None) # event-binding will be removed
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to onclick is not a valid turtle"))
turtle.onclick(fun, btn, add)
def ondrag(turtle, fun, btn=1, add=None):
"""Bind fun to mouse-move event on this turtle on canvas.
Arguments:
turtle -- the turtle
fun -- a function with two arguments, to which will be assigned
the coordinates of the clicked point on the canvas.
num -- number of the mouse-button defaults to 1 (left mouse button).
Every sequence of mouse-move-events on a turtle is preceded by a
mouse-click event on that
Example:
>>> ondrag(turtle, goto)
Subsequently clicking and dragging a Turtle will move it
across the screen thereby producing handdrawings (if pen is
down).
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to ondrag is not a valid turtle"))
turtle.ondrag(fun, btn, add)
def onrelease(turtle, fun, btn=1, add=None):
"""Bind fun to mouse-button-release event on this turtle on canvas.
Arguments:
turtle -- the turtle
fun -- a function with two arguments, to which will be assigned
the coordinates of the clicked point on the canvas.
num -- number of the mouse-button defaults to 1 (left mouse button).
Example (for a MyTurtle instance named joe):
>>> class MyTurtle(Turtle):
... def glow(self,x,y):
... self.fillcolor("red")
... def unglow(self,x,y):
... self.fillcolor("")
...
>>> joe = MyTurtle()
>>> joe.onclick(joe.glow)
>>> joe.onrelease(joe.unglow)
Clicking on joe turns fillcolor red, unclicking turns it to
transparent.
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to onrelease is not a valid turtle"))
turtle.onrelease(fun, btn, add)
def pd(turtle):
"""Pull the pen down -- drawing when moving.
Aliases: pendown | pd | down
Argument:
turtle -- the turtle
Example:
>>> pendown(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to pd is not a valid turtle"))
turtle.pd()
def pen(turtle, pen=None):
"""Return or set the pen's attributes.
Arguments:
turtle -- the turtle
pen -- a dictionary with some or all of the below listed keys.
**pendict -- one or more keyword-arguments with the below
listed keys as keywords.
Return or set the pen's attributes in a 'pen-dictionary'
with the following key/value pairs:
"shown" : True/False
"pendown" : True/False
"pencolor" : color-string or color-tuple
"fillcolor" : color-string or color-tuple
"pensize" : positive number
"speed" : number in range 0..10
"resizemode" : "auto" or "user" or "noresize"
"stretchfactor": (positive number, positive number)
"shearfactor": number
"outline" : positive number
"tilt" : number
This dictionary can be used as argument for a subsequent
pen(turtle)-call to restore the former pen-state. Moreover one
or more of these attributes can be provided as keyword-arguments.
This can be used to set several pen attributes in one statement.
Examples:
>>> pen(turtle, fillcolor="black", pencolor="red", pensize=10)
>>> pen(turtle)
{'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
>>> penstate=pen()
>>> color(turtle, "yellow","")
>>> penup(turtle)
>>> pen(turtle)
{'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
>>> p.pen(penstate, fillcolor="green")
>>> p.pen()
{'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to pen is not a valid turtle"))
return turtle.pen(pen)
def pencolor(turtle, *args):
""" Return or set the pencolor.
Arguments:
turtle -- the turtle
Four input formats are allowed:
- pencolor(turtle)
Return the current pencolor as color specification string,
possibly in hex-number format (see example).
May be used as input to another color/pencolor/fillcolor call.
- pencolor(turtle, colorstring)
s is a Tk color specification string, such as "red" or "yellow"
- pencolor(turtle, (r, g, b))
*a tuple* of r, g, and b, which represent, an RGB color,
and each of r, g, and b are in the range 0..colormode,
where colormode is either 1.0 or 255
- pencolor(turtle, r, g, b)
r, g, and b represent an RGB color, and each of r, g, and b
are in the range 0..colormode
If turtleshape is a polygon, the outline of that polygon is drawn
with the newly set pencolor.
Example:
>>> pencolor(turtle, 'brown')
>>> tup = (0.2, 0.8, 0.55)
>>> pencolor(turtle, tup)
>>> pencolor(turtle)
'#33cc8c'
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to pencolor is not a valid turtle"))
return turtle.pencolor(*args)
def pendown(turtle):
"""Pull the pen down -- drawing when moving.
Aliases: pendown | pd | down
Argument:
turtle -- the turtle
Example:
>>> pendown(turtle)
"""
if type(turtle) != _turtle.Turtle:
raise(TypeError("turtle argument to pendown is not a valid turtle"))
turtle.pendown()
def pensize(turtle, width=None):
"""Set or return the line thickness.
Aliases: pensize | width