绘制一个移动的圆

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
# -----设置背景-----
background = pygame.Surface(screen.get_size())
background.fill((255, 240, 245)) # fill LavenderBlush

# 绘制一个移动的圆

# ------- 设置常量------
clock = pygame.time.Clock()
FPS = 60
mainloop = True

cradius = 40
ccolor = (255, 106,106)
cx = cradius
cy = 480//2
cdr = -1          # 移动方向

# ----- mainloop ----
while mainloop:
    clock.tick(FPS)     # 控制framerate
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False
    background.fill((255, 240, 245))    # 刷新背景
    # ----- 控制圆的位置 ------
    if cx == 640-cradius:
        cdr = -cdr
        cx += cdr
    if cx == cradius:
        cdr = -cdr
    if cx <= 640-cradius-cdr:
        cx += cdr
    # ----- 绘制圆 -----
    pygame.draw.circle(background, ccolor, (cx, cy), cradius)
    # ------ 显示 -----
    screen.blit(background, (0, 0))
    pygame.display.flip()

猜你喜欢

转载自www.cnblogs.com/xiyu714/p/9031760.html