-
Notifications
You must be signed in to change notification settings - Fork 4
/
playing_cards_io.py
219 lines (185 loc) · 4.6 KB
/
playing_cards_io.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
""" Generate images to use for a table on the playingcards.io web site. """
from pathlib import Path
from turtle import Turtle
from svg_diagram import SvgDiagram
class Tetromino:
shape_name = '?'
def __init__(self, t: Turtle, fill_colour: str = 'black', size: int = 40):
self.t = t
self.fill_colour = fill_colour
self.size = size
def prep(self):
self.t.pensize(self.size//10)
self.t.fillcolor(self.fill_colour)
def draw(self):
self.prep()
self.t.begin_fill()
self.draw_shape()
self.t.end_fill()
def draw_shape(self):
pass
class TetrominoO(Tetromino):
shape_name = 'o'
def draw_shape(self):
t = self.t
for _ in range(4):
t.forward(self.size*2)
t.right(90)
class TetrominoL(Tetromino):
shape_name = 'l'
def draw_shape(self):
t = self.t
size = self.size
t.forward(size*3)
t.right(90)
t.forward(size)
t.right(90)
t.forward(size*2)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size)
t.right(90)
t.forward(size*2)
t.right(90)
class TetrominoJ(Tetromino):
shape_name = 'j'
def draw_shape(self):
t = self.t
size = self.size
t.forward(size*3)
t.right(90)
t.forward(size*2)
t.right(90)
t.forward(size)
t.right(90)
t.forward(size)
t.left(90)
t.forward(size*2)
t.right(90)
t.forward(size)
t.right(90)
class TetrominoS(Tetromino):
shape_name = 's'
def draw_shape(self):
t = self.t
size = self.size
t.end_fill()
t.up()
t.forward(size)
t.begin_fill()
t.down()
for _ in range(2):
t.forward(2*size)
t.right(90)
t.forward(size)
t.right(90)
t.forward(size)
t.left(90)
t.forward(size)
t.right(90)
t.end_fill()
t.up()
t.back(size)
class TetrominoZ(Tetromino):
shape_name = 'z'
def draw_shape(self):
t = self.t
size = self.size
for _ in range(2):
t.forward(size*2)
t.right(90)
t.forward(size)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size)
t.right(90)
class TetrominoT(Tetromino):
shape_name = 't'
def draw_shape(self):
t = self.t
size = self.size
t.forward(size*3)
t.right(90)
t.forward(size)
t.right(90)
t.forward(size)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size)
t.right(90)
t.forward(size)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size)
t.right(90)
class TetrominoI(Tetromino):
shape_name = 'i'
def draw_shape(self):
t = self.t
size = self.size
for _ in range(2):
t.forward(size*4)
t.right(90)
t.forward(size)
t.right(90)
def demo():
t = Turtle()
white = 'darkgrey'
t.up()
size = 40
t.back(size*13.5)
t.down()
TetrominoO(t, white).draw()
t.up()
t.forward(size*3)
t.down()
TetrominoL(t, white).draw()
t.up()
t.forward(size*4)
t.down()
TetrominoJ(t, white).draw()
t.up()
t.forward(size*4)
t.down()
TetrominoS(t, white).draw()
t.up()
t.forward(size*4)
t.down()
TetrominoZ(t, white).draw()
t.up()
t.forward(size*4)
t.down()
TetrominoT(t, white).draw()
t.up()
t.forward(size*4)
t.down()
TetrominoI(t, white).draw()
def main():
out_path = Path('playing_cards_io')
out_path.mkdir(exist_ok=True)
shape_classes = [TetrominoT,
TetrominoZ,
TetrominoI,
TetrominoS,
TetrominoO,
TetrominoJ,
TetrominoL]
for cls in shape_classes:
for colour_name, colour_suffix in (('ivory', 'w'), ('darkgrey', 'b')):
diagram = SvgDiagram('160', '80')
diagram.turtle.up()
diagram.turtle.goto(-78, 38)
diagram.turtle.down()
t = cls(diagram.turtle, fill_colour=colour_name, size=38)
shape_name = t.shape_name
t.draw()
file_name = f'tetromino-{shape_name}{colour_suffix}.svg'
diagram.svg_drawing.saveas(out_path / file_name)
if __name__ == '__main__':
main()
else:
demo()