This repository was archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtle_sk.py
More file actions
133 lines (102 loc) · 3.9 KB
/
turtle_sk.py
File metadata and controls
133 lines (102 loc) · 3.9 KB
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
from pyscript import window
window.turtle.setup()
turtles = []
screens = []
def get_color(args):
if len(args) == 3:
return "#%02x%02x%02x" % (args[0], args[1], args[2])
else:
return args[0]
class Turtle:
def __init__(self):
length = len(turtles)
if length == 1:
self.reference = turtles[0]
turtles.append(f"turtle{length+1}")
else:
self.reference = f"turtle{length+1}"
turtles.append(self.reference)
window.turtle.execute(f'{self.reference} = Turtle()')
async def forward(self, distance):
return await window.turtle.execute(f'{self.reference}.forward({distance})')
async def left(self, angle):
return await window.turtle.execute(f'{self.reference}.left({angle})')
async def backward(self, distance):
return await window.turtle.execute(f'{self.reference}.backward({distance})')
async def right(self, angle):
return await window.turtle.execute(f'{self.reference}.right({angle})')
async def goto(self, x, y):
return await window.turtle.execute(f'{self.reference}.goto({x}, {y})')
async def circle(self, radius):
return await window.turtle.execute(f'{self.reference}.circle({radius})')
async def width(self, width):
return await window.turtle.execute(f'{self.reference}.width({width})')
async def color(self, *args):
return await window.turtle.execute(f'{self.reference}.color("{get_color(args)}")')
async def pencolor(self, *args):
return await window.turtle.execute(f'{self.reference}.pencolor("{get_color(args)}")')
async def fillcolor(self, *args):
return await window.turtle.execute(f'{self.reference}.fillcolor("{get_color(args)}")')
async def begin_fill(self):
return await window.turtle.execute(f'{self.reference}.begin_fill()')
async def end_fill(self):
return await window.turtle.execute(f'{self.reference}.end_fill()')
async def penup(self):
return await window.turtle.execute(f'{self.reference}.penup()')
async def pendown(self):
return await window.turtle.execute(f'{self.reference}.pendown()')
async def speed(self, speed):
return await window.turtle.execute(f'{self.reference}.speed({speed})')
async def shape(self, shape):
return await window.turtle.execute(f'{self.reference}.shape("{shape}")')
async def write(self, text, move=False, align="left", font=("Arial", 8, "normal")):
return await window.turtle.execute(f'{self.reference}.write("{text}", move={move}, align="{align}", font=("{font[0]}", {font[1]}, "{font[2]}"))')
class Screen:
def __init__(self):
length = len(screens)
if length == 1:
self.reference = screens[0]
turtles.append(f"screen{length+1}")
else:
self.reference = f"screen{length+1}"
screens.append(self.reference)
window.turtle.execute(f'{self.reference} = Screen()')
async def bgcolor(self, *args):
return await window.turtle.execute(f'{self.reference}.bgcolor("{get_color(args)}")')
async def setup(self, w, h):
return await window.turtle.execute(f'{self.reference}.setup({w}, {h})')
_classes = [
"Turtle",
"Screen"
]
_turtle_functions = [
"forward",
"left",
"backward",
"right",
"goto",
"circle",
"width",
"color",
"pencolor",
"fillcolor",
"begin_fill",
"end_fill",
"penup",
"pendown",
"speed",
"shape",
"write"
]
_screen_functions = [
"bgcolor",
"setup"
]
default_turtle = Turtle()
default_screen = Screen()
__all__ = _classes + _turtle_functions + _screen_functions
def _make_global_funcs(functions, cls):
for func in functions:
globals()[func] = getattr(cls, func)
_make_global_funcs(_turtle_functions, default_turtle)
_make_global_funcs(_screen_functions, default_screen)