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

最新文章

  1. Bash 中 SHLVL 变量为 1000 的时候
  2. PHP字符串函数
  3. SSH框架简化
  4. JS判断是否已经到达页面底部
  5. BC之Run
  6. 用Ant实现Java项目的自动构建和部署(转)
  7. iOS动画篇:UIView动画
  8. POJ3613 Cow Relays [矩阵乘法 floyd类似]
  9. ThinkPHP函数详解:L方法
  10. svn强制添加注释脚本
  11. frameset常用属性
  12. sqlite增删改查
  13. 二叉树最近公共祖先(LeetCode)
  14. bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
  15. 壁虎书7 Ensemble Learning and Random Forests
  16. android默认开启adb调试方法分析
  17. mariadb(第一章)
  18. OnCheckedChangeListener和setChecked之间冲突问题解决
  19. matlab学习(2) sort、sortrows
  20. emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel HAXM is properly installed and usable.CPU acceleration status: HAX kernel module is not installed!

热门文章

  1. 8-2 canvas专题-线条样式
  2. 【POJ 1330】 Nearest Common Ancestors
  3. JeePlus:目录
  4. poj 2409 Let it Bead【polya定理+burnside引理】
  5. bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡【dp+高斯消元】
  6. golang——database/sql包学习
  7. Asp.Net 开发实战技术
  8. [C++ STL] vector使用详解
  9. C#封装访问修饰符
  10. SQL server 查询语句 练习题