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