【Dison夏令营 Day 21】用Python编写绘图

绘画 - 在屏幕上绘制线条和形状。单击标记形状的起点,再次单击标记形状的终点。可使用键盘选择不同的形状和颜色。

在这里插入图片描述

"""Paint, for drawing shapes.

Exercises

1. Add a color.
2. Complete circle.
3. Complete rectangle.
4. Complete triangle.
5. Add width parameter.
"""

from turtle import *

from freegames import vector


def line(start, end):
    """Draw line from start to end."""
    up()
    goto(start.x, start.y)
    down()
    goto(end.x, end.y)


def square(start, end):
    """Draw square from start to end."""
    up()
    goto(start.x, start.y)
    down()
    begin_fill()

    for count in range(4):
        forward(end.x - start.x)
        left(90)

    end_fill()


def circle(start, end):
    """Draw circle from start to end."""
    pass  # TODO


def rectangle(start, end):
    """Draw rectangle from start to end."""
    pass  # TODO


def triangle(start, end):
    """Draw triangle from start to end."""
    pass  # TODO


def tap(x, y):
    """Store starting point or draw shape."""
    start = state['start']

    if start is None:
        state['start'] = vector(x, y)
    else:
        shape = state['shape']
        end = vector(x, y)
        shape(start, end)
        state['start'] = None


def store(key, value):
    """Store value in state at key."""
    state[key] = value


state = {'start': None, 'shape': line}
setup(420, 420, 370, 0)
onscreenclick(tap)
listen()
onkey(undo, 'u')
onkey(lambda: color('black'), 'K')
onkey(lambda: color('white'), 'W')
onkey(lambda: color('green'), 'G')
onkey(lambda: color('blue'), 'B')
onkey(lambda: color('red'), 'R')
onkey(lambda: store('shape', line), 'l')
onkey(lambda: store('shape', square), 's')
onkey(lambda: store('shape', circle), 'c')
onkey(lambda: store('shape', rectangle), 'r')
onkey(lambda: store('shape', triangle), 't')
done()

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 05:10:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 05:10:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 05:10:01       57 阅读
  4. Python语言-面向对象

    2024-07-17 05:10:01       68 阅读

热门阅读

  1. 掌握Conda环境管理:使用conda env remove命令的精要

    2024-07-17 05:10:01       20 阅读
  2. 【python】Request简单使用

    2024-07-17 05:10:01       18 阅读
  3. Redis端口开启防火墙报错

    2024-07-17 05:10:01       22 阅读
  4. KITTI 3D 数据可视化

    2024-07-17 05:10:01       28 阅读
  5. 口令爆破基础学习

    2024-07-17 05:10:01       25 阅读
  6. 基于单片机的直流电机控制

    2024-07-17 05:10:01       25 阅读