用Python的pygame模块在窗口中心绘制一个圆

实现的代码并不难,重点是要关注pygame.draw.circle()里面的参数。代码以下:python

import pygame,sys

def run_game():
    '''调用这个函数就能实如今游戏窗口中绘制一个绿黄色的圆'''

    #初始化pygame
    pygame.init()
    #窗口的尺寸设置为宽900,高600
    screen=pygame.display.set_mode((900,600))
    #窗口的标题设置为Draw a Circle
    pygame.display.set_caption('Draw a Circle')
    #获取窗口矩形
    screen_rect=screen.get_rect()
    #要绘制的圆形的半径设置为100
    my_radius=100
    #圆形的颜色设置为绿黄色
    my_color=(173,255,47)
    #窗口的颜色设置为白色
    bg_color=(255,255,255)

    while True:
        #检验键鼠事件,这里只设置了一个事件就是点击右上角的叉号就能退出程序
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
        #绘制窗口的颜色
        screen.fill(bg_color)
        #绘制圆形。第一个参数是放置圆的平面,第二个参数是圆的颜色,第三个参数是一个元组,
        # 里面存放了圆心的坐标,第四个参数是圆的半径,第五个参数是圆形轮廓的宽度,是一个默认
        #参数,默认值是0,我没有修改它,因此绿黄色会填充整个圆形
        pygame.draw.circle(screen,my_color,
        (screen_rect.centerx,screen_rect.centery),my_radius)
        #把屏幕上的全部内容展现出来
        pygame.display.flip()

run_game()

效果展现:函数