码迷,mamicode.com
首页 > 其他好文 > 详细

pygame绘制一个随机背景

时间:2018-05-16 00:33:25      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:import   cape   main   isp   surf   escape   and   --   cleanup   

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

 

pygame绘制一个随机背景

标签:import   cape   main   isp   surf   escape   and   --   cleanup   

原文地址:https://www.cnblogs.com/xiyu714/p/9043599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!