def rect_circle():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255,255,255))
# 画正方形
rect = pygame.Rect(300, 200, 200, 200)
pygame.draw.rect(screen, (0, 0, 255), rect, width = 1)
# 画内切圆, 半径因为正方形的线宽占了一个,所以半径要相应的少一个
pos = 400, 300
radius = 99
pygame.draw.circle(screen, (255, 0, 0), pos, radius, )
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
def circle_rect():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255,255,255))
# 画外面的圆
pos = 400, 300
radius = 101
pygame.draw.circle(screen, (255, 0, 0), pos, radius, )
# 画内接正方形
width = height = 100*1.41 # 计算内接正方形的宽高
left, top = 400 - width/2, 300-height/2 # 计算内接正方形的左上角坐标
rect = pygame.Rect(left, top, width, height)
pygame.draw.rect(screen, (0, 0, 255), rect)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
pygame.draw.ellipse(Surface, color, rect, width)
Surface: Surface对象
color: 线条颜色
rect:椭圆的外切矩形
width: 线粗(width=0 是实心椭圆)
备注:
当矩形是一个正方形的时候,画出来的椭圆其实是一个圆_.
# 画椭圆
def draw_ellipse():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
# 画椭圆
rect = (100,100,200,100)
pygame.draw.ellipse(screen, (0,255,255), rect, 1)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
说明一下:
这里的椭圆是用矩形框起来的.是矩形的内切椭圆.因此,在实际使用的时候,要根据椭圆的中心位置以及椭圆的宽度与高度来计算具体的参数值
读者可以自行将其转化公式写一下,体会一下.
pygame.draw.arc(Surface,color, rect, start_angle, end_angle, width)
Surface: Surface对象
color: 线条颜色
rect:弧所在的圆(椭圆)的外切矩形
width: 线粗(width=0时看不见)
start_angle:起始角度
end_angle:终止角度
备注:角度的问题
####示例代码
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
# 画椭圆
rect = (100,100,200,100)
pygame.draw.arc(screen, (0,255,255), rect, 1.5, -1, width=1)
rect = (300, 100, 100, 100)
pygame.draw.arc(screen, (0,0,255), rect, 0, pi/2*3, width=2)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()