pygame 方块随机飞舞动画
2024-09-30 11:36:33
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()
最新文章
- Bash 中 SHLVL 变量为 1000 的时候
- PHP字符串函数
- SSH框架简化
- JS判断是否已经到达页面底部
- BC之Run
- 用Ant实现Java项目的自动构建和部署(转)
- iOS动画篇:UIView动画
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
- ThinkPHP函数详解:L方法
- svn强制添加注释脚本
- frameset常用属性
- sqlite增删改查
- 二叉树最近公共祖先(LeetCode)
- bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
- 壁虎书7 Ensemble Learning and Random Forests
- android默认开启adb调试方法分析
- mariadb(第一章)
- OnCheckedChangeListener和setChecked之间冲突问题解决
- matlab学习(2) sort、sortrows
- 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!
热门文章
- 8-2 canvas专题-线条样式
- 【POJ 1330】 Nearest Common Ancestors
- JeePlus:目录
- poj 2409 Let it Bead【polya定理+burnside引理】
- bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡【dp+高斯消元】
- golang——database/sql包学习
- Asp.Net 开发实战技术
- [C++ STL] vector使用详解
- C#封装访问修饰符
- SQL server 查询语句 练习题