zoukankan      html  css  js  c++  java
  • pygame 方块随机飞舞动画

    import pygame
    import random
    # default
    WIDTH=1280
    HEIGHT=1060
    FPS=60
    sum=0
    # set color
    WHITE=(255,255,255)
    BLACK=(0,0,0)
    RED=(255,0,0)
    GREEN=(0,255,0)
    BLUE=(0,0,255)
    color=[WHITE,RED,GREEN,BLUE]
    # set Block class
    class Block(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image=pygame.Surface((50,50))
            self.image.fill(random.choice(color))
            self.rect = self.image.get_rect()
            self.rect.center=(random.randint(1,WIDTH),random.randint(1,HEIGHT))
            self.ce=random.randint(1,2)
            self.speed=random.choice([i for i in range(-20,20) if not i in range(-5,5)])
        def update(self):
            if self.ce==1:
                self.rect.x+=self.speed
            else:
                self.rect.y+=self.speed
    # pygame init
    pygame.init()
    screen=pygame.display.set_mode((WIDTH,HEIGHT))
    pygame.display.set_caption('lowb cartoon')
    clock = pygame.time.Clock()
    #set group
    all_sprites = pygame.sprite.Group()
    block1=[Block() for i in range(random.randrange(20,40))]
    block2=[Block() for i in range(random.randrange(20,40))]
    #block=Block()
    #print(block.ce,block.speed)
    all_sprites.add(block1)
    all_sprites.add(block2)
    # main loop
    running=True
    while running:
        sum+=1
        if sum%100==0:
            block1=[Block() for i in range(random.randrange(20,40))]
            all_sprites.add(block1)
        if sum%150==0:
            block2=[Block() for i in range(random.randrange(20,40))]
            all_sprites.add(block2)
        #set FPS
        clock.tick(FPS)
        #set event
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running=False
        #set update
        all_sprites.update()
        #set draw/render
        screen.fill(BLACK)
        all_sprites.draw(screen)
        #flip time
        pygame.display.flip()
    pygame.quit()
    
  • 相关阅读:
    Dungeon Master(BFS)
    Shuffle'm Up(串)
    分解素因子
    Snowflake Snow Snowflakes(查找)
    求素数(素数筛选+打表)
    A simple problem(并查集判环)
    Eqs(枚举+ hash)
    Squares(枚举+set 查找)
    从控制台输入10个人的年龄放入数组,将十个人的年龄求总和
    数组
  • 原文地址:https://www.cnblogs.com/baccano-acmer/p/10255057.html
Copyright © 2011-2022 走看看