|
| 1 | +"""Paint, for drawing shapes. |
| 2 | +
|
| 3 | +Exercises |
| 4 | +
|
| 5 | +1. Add a color. |
| 6 | +2. Complete circle. |
| 7 | +3. Complete rectangle. |
| 8 | +4. Complete triangle. |
| 9 | +5. Add width parameter. |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from turtle import * |
| 14 | + |
| 15 | +from freegames import vector |
| 16 | + |
| 17 | + |
| 18 | +def line(start, end): |
| 19 | + "Draw line from start to end." |
| 20 | + up() |
| 21 | + goto(start.x, start.y) |
| 22 | + down() |
| 23 | + goto(end.x, end.y) |
| 24 | + |
| 25 | + |
| 26 | +def square(start, end): |
| 27 | + "Draw square from start to end." |
| 28 | + up() |
| 29 | + goto(start.x, start.y) |
| 30 | + down() |
| 31 | + begin_fill() |
| 32 | + |
| 33 | + for count in range(4): |
| 34 | + forward(end.x - start.x) |
| 35 | + left(90) |
| 36 | + |
| 37 | + end_fill() |
| 38 | + |
| 39 | + |
| 40 | +def circle(start, end): |
| 41 | + "Draw circle from start to end." |
| 42 | + pass # TODO |
| 43 | + |
| 44 | + |
| 45 | +def rectangle(start, end): |
| 46 | + "Draw rectangle from start to end." |
| 47 | + pass # TODO |
| 48 | + |
| 49 | + |
| 50 | +def triangle(start, end): |
| 51 | + "Draw triangle from start to end." |
| 52 | + pass # TODO |
| 53 | + |
| 54 | + |
| 55 | +def tap(x, y): |
| 56 | + "Store starting point or draw shape." |
| 57 | + start = state['start'] |
| 58 | + |
| 59 | + if start is None: |
| 60 | + state['start'] = vector(x, y) |
| 61 | + else: |
| 62 | + shape = state['shape'] |
| 63 | + end = vector(x, y) |
| 64 | + shape(start, end) |
| 65 | + state['start'] = None |
| 66 | + |
| 67 | + |
| 68 | +def store(key, value): |
| 69 | + "Store value in state at key." |
| 70 | + state[key] = value |
| 71 | + |
| 72 | + |
| 73 | +state = {'start': None, 'shape': line} |
| 74 | +setup(420, 420, 370, 0) |
| 75 | +onscreenclick(tap) |
| 76 | +listen() |
| 77 | +onkey(undo, 'u') |
| 78 | +onkey(lambda: color('black'), 'K') |
| 79 | +onkey(lambda: color('white'), 'W') |
| 80 | +onkey(lambda: color('green'), 'G') |
| 81 | +onkey(lambda: color('blue'), 'B') |
| 82 | +onkey(lambda: color('red'), 'R') |
| 83 | +onkey(lambda: store('shape', line), 'l') |
| 84 | +onkey(lambda: store('shape', square), 's') |
| 85 | +onkey(lambda: store('shape', circle), 'c') |
| 86 | +onkey(lambda: store('shape', rectangle), 'r') |
| 87 | +onkey(lambda: store('shape', triangle), 't') |
| 88 | +done() |
0 commit comments