# Pen

# pen.fill(color)

设置填充颜色,参数 color 支持多种传入方式。

from cpgzero import *
pen.fill('red')
pen.fill('#ff6600')
pen.fill((255, 255, 100))
pen.fill(100, 100, 100)

# pen.size(size)

设置画笔粗细。

from cpgzero import *
pen.size(3)

# pen.line(start, end, color)

画线,startend 分别为起点、终点坐标。

from cpgzero import *
pen.line((0, 0), (100, 100), "red")
pen.line((100, 100), (100, -100), "#ff6600")
pen.line((100, -100), (0, 0), (245, 45, 45))

# pen.circle(pos, radius, color)

绘制空心圆,pos 原点坐标,radius 半径。

from cpgzero import *
pen.circle((0, 0), 200, "red")
pen.circle((200, 200), 200, (255, 0, 0))

# pen.filled_circle(pos, radius, color)

绘制实心圆,pos 原点坐标,radius 半径。

from cpgzero import *
pen.filled_circle((0, 0), 200, "red")
pen.filled_circle((200, 200), 200, (255, 0, 0))

# pen.rect

绘制空心矩形,支持三种参数格式:

  • rect(rect, color)
  • rect(lefttop, width, height, color) lefttop 为左上角坐标
  • rect(left, top, width, height, color)
from cpgzero import *
pen.size(2)
box = Rect((100, 100), (200, 200))
pen.rect(box, "red")
pen.rect((300, 300), 200, 200, "red")
pen.rect(-100, -100, 200, 200, "red")

# pen.filled_rect

绘制实心矩形,支持三种参数格式,与 pen.rect 相同。

from cpgzero import *
box = Rect((100, 100), (200, 200))
pen.filled_rect(box, "red")
pen.filled_rect((300, 300), 200, 200, "red")
pen.filled_rect(-100, -100, 200, 200, "red")

# pen.text(str, pos, color, fontsize, fontname)

from cpgzero import *
pen.text("Text color", (-100, -100), (255, 0, 0), 80)
pen.text("Text color", (50, 30), color="orange")
pen.text("Font name and size", (20, 100), fontname="Boogaloo", fontsize=60, color="blue")

# pen.clear()

清除画笔绘制的 circlerecttext 等内容。

from cpgzero import *
box = Rect((0, 0), (150, 150))
pen.filled_rect(box, "red")
pen.filled_rect((200, 200), 150, 150, "red")

def clear():
    pen.clear()

clock.schedule(clear, 1)