Pygame(四)画椭圆,弧

Pygame(四)画椭圆,弧

前情提要:

image.png

作业答案

  • 正方形与内切圆
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()

效果图:
image.png

  • 圆与内接正方形
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()

效果图:
image.png

本节提要

image.png

内容详情

画椭圆

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()

效果图:
image.png

说明一下:
这里的椭圆是用矩形框起来的.是矩形的内切椭圆.因此,在实际使用的时候,要根据椭圆的中心位置以及椭圆的宽度与高度来计算具体的参数值
读者可以自行将其转化公式写一下,体会一下.

画弧

pygame.draw.arc(Surface,color, rect, start_angle, end_angle, width)

参数说明:

Surface: Surface对象
color: 线条颜色
rect:弧所在的圆(椭圆)的外切矩形
width: 线粗(width=0时看不见)
start_angle:起始角度
end_angle:终止角度

备注:角度的问题

  • 角度以中心为起点,向右方向为0度(3点角度为0,逆时针方向为正, 12点为90度,9点为180度, 6点为270度
  • 角度是弧度制表示的.pi = 180度, pi/2 = 90度.
  • pi这个常数来自math库
  • 当角度超过360度时
  • 角度取负值时,顺时针方向旋转计算.
  • 弧的选取一定是从起点到终点逆时针方向截取.

####示例代码

    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()

效果图:
image.png

作业:

画出如下图形:
image.png

猜你喜欢

转载自blog.csdn.net/weixin_41810846/article/details/112120453