Pygame(三)画圆

Pygame(三)画圆

作业代码

  1. 画一个矩形:要求,左上角在(100,100), 宽为200, 高为60,边框颜色为蓝色
    作业代码:
def homework01():
    '''画一个矩形'''
    '''
    要求:
    1  画一个矩形:要求,左上角在(100,100), 宽为200, 高为60,边框颜色为蓝色
    '''
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(100,100, 200, 60)
    pygame.draw.rect(screen, (0, 0, 255), rect, 1)
    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    pygame.quit()
  1. 用矩形画一个10层的高楼
    要求:
  2. 层高:50
  3. 层宽:逐层递减,最宽(1层)200, 每层减8
  4. 颜色:灰色(190, 190, 190)

作业代码

def homework02():
    '''画一个矩形'''
    '''   
      要求:用矩形画一个10层的高楼
      1. 层高:50
      2. 层宽:逐层递减,最宽(1层)200, 每层减8
      3. 颜色:灰色(190, 190, 190)
    '''
    pygame.init()
    screen = pygame.display.set_mode((800, 600))

    for i in range(10):
        rect = pygame.Rect(300+4*i, 500-50*i, 200-8 * i, 50)
        pygame.draw.rect(screen, (190, 190, 190), rect, 1)
    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    pygame.quit()

作业2效果图

前情提要

第二课思维导图

本课提要

image.png

内容详解

画圆

pygame.draw.circle(Surface, color, pos, radius, width=0)

参数说明:

  • Surface : 同上节课,一个Surface类型对象, 一般screen
  • color: 线的颜色
  • pos:圆心坐标
  • radius: 半径
  • width:线宽
    说明:
    1. 当设置为0或者不设置时,画出来的是实心的圆
    2. 线宽是算在半径之内的,即当半径是5的时候,如果设置width = 5,画出来的是一个实心圆

示例代码

def drawcircle():
    '''函数说明'''
    '''
    画一个圆心在中心,半径=高一半的红色圆
    '''
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    print(dir(pygame.Surface))
    x = screen.get_width()/2
    y = screen.get_height()/2
    radius = screen.get_height()/2
    pygame.draw.circle(screen, (255,0,0), (x,y), radius, width = 1)
    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    pygame.quit()

要点说明:
这里用到了两个Surface对象的方法函数,一个是get_width(),一个是get_height()
顾名思议
get_width:获取的是屏幕的宽度
get_height:获取的是屏幕的高度
要想同时获取宽度与高度,可以用get_size()来实现,返回的是一个二元组

示例2:奥运五环

def draw_a5():
    '''函数说明'''
    '''
    画奥云五环
    画法说明:
    圆环内圈半径为单位1,外圈半径为1.2;相邻圆环圆心水平距bai离为2.6;两排圆环圆心垂直距离为1.1
    颜色第一行为蓝 黑 红
    第二行为 黄 绿 
    '''
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    screen.fill((255,255,255)) # 背景色设置为白色
    # 画第一个圆,颜色蓝色:(0,0,255)
    pos = x1, y1=  275, 200  # 圆心
    r = 50  # 半径
    c = (0, 0, 255)  # 蓝色
    pygame.draw.circle(screen, c, pos, r + 10, width = 10)

    # 画第二个圆, 圆心在第一个圆往右2.5半径
    pos = x2, y2 = x1 + 2.6 * r, y1
    c = (0,0,0)  # 黑色
    pygame.draw.circle(screen, c, pos, r + 10, width = 10)

    # 画第三个圆, 圆心再次往右2.5半径
    pos =x3, y3 = x2 + 2.6 * r, y1
    c = (255, 0, 0)  # 红色
    pygame.draw.circle(screen, c, pos, r + 10, width=10)

    # 第四个圆, 圆心在第一与第二个圆圆心中间向下
    pos = x1 + 1.3 * r, y1 + 1.1 * r
    c = (255, 255, 0)
    pygame.draw.circle(screen, c, pos, r + 10, width=10)

    # 第五个圆,圆心在第二与第三个圆的中间向下
    pos = x2 + 1.3 * r,y1 + 1.1 * r
    c = (0, 255, 0)
    pygame.draw.circle(screen, c, pos, r + 10, width=10)

    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    pygame.quit()

注意:

  1. 因为线宽是算在半径内的.因此,画一个内圆是50, 外圆是60,所以实际画的时候要画半径为60的圆.
  2. 背景色默认为黑色,会影响第二个圆环的显示,所以将背景色设置为白色,用到了screen.fill()方法.参数为RGB元组

五环效果图

后记:

  1. 矩形与圆是两个基本的图形.因此,我们要熟练掌握使用方法.
    2.无论是画矩形,还是画圆,还是后面的其他图形,画出来的东西会返回一个矩形对象.以用来后续的操作.比如移动什么的.

练习:

  1. 画一个边长为100的正方形,线宽为1, 颜色为蓝色.再画一个内切圆,实心红色
  2. 画一个半径为200的圆,再画一个内接正方形
    备注:计算根号2取1.41

猜你喜欢

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