pygame绘制一个随机背景

import random
import pygame

# ----- 初始化 -----
pygame.init()
screen = pygame.display.set_mode((640, 480))
screenrect = screen.get_rect()  # 创建一个矩形
background = pygame.Surface(screen.get_size())
mainloop = True
dcirc = True
nocleanup = True
clock = pygame.time.Clock()
FPS = 60

# ------ 随机绘制圆 -----
def draw_random_circle(back):
    bally = random.randint(0, screenrect.height)
    ballx = random.randint(0, screenrect.width)
    color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
    radius = random.randint(0, (screenrect.height+screenrect.width)//4)
    pygame.draw.circle(back, color, (ballx, bally), radius)
# ---- mainloop ----
while mainloop:
    clock.tick(FPS)
    pygame.display.set_caption("FPS: %d" % clock.get_fps())     # 显示FPS
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                dcirc = not dcirc
                nocleanup = True
            elif event.key == pygame.K_ESCAPE:
                mainloop = False
            elif event.key == pygame.K_c:
                nocleanup = not nocleanup
                if nocleanup:
                    dcirc = True
                else:
                    dcirc = False

    # ----- 控制绘制背景和 清空背景
    if not nocleanup:
        background.fill((255, 255, 255))
    if dcirc:
        draw_random_circle(background)
    screen.blit(background, (0, 0))
    # ----- 刷新 ------
    pygame.display.flip()

 

转载于:https://www.cnblogs.com/xiyu714/p/9043599.html

你可能感兴趣的:(pygame绘制一个随机背景)