python - 使用 Pygame 使粒子连续生成
问题描述
我正在使用 pygame 开展一个学校项目,主要目标是让很多物体出现并随机移动。所以我想让一些粒子产生并随机移动,我想出了这个。
import pygame, random
# -- Variables --
pygame.init()
clock = pygame.time.Clock()
fps_limit = 60.0
resx = 1600
resy = 900
screen = pygame.display.set_mode([resx,resy])
# -- Classes --
class Particule():
def __init__ (self,color,radius):
pygame.sprite.Sprite.__init__(self)
self.color = color
self.radius = radius
self.position_x = resx/2
self.position_y = resy/2
def update(self):
pygame.draw.circle(screen, self.color, (self.position_x,self.position_y),self.radius)
def random_movement(self):
self.position_x += random.randint(0,10)
self.position_y -= random.randint(0,5)
def life(self):
self.radius -= 1
# -- Objects --
particule1 = Particule((255,0,0),100)
# -- Game loop --
running = True
while running:
clock.tick(fps_limit)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
particule1.update()
particule1.random_movement()
particule1.life()
pygame.display.set_caption("Baguette")
pygame.display.flip()
pygame.quit()
我尝试做一些 while/if/for 循环,但只出现一个对象,然后没有
解决方案
为粒子创建一个列表:
particles = []
用于pygame.time.get_ticks()
获取自pygame.init()
调用以来的毫秒数。定义第一个粒子必须出现的时间。如果时间超过创建一个粒子并设置要创建下一个粒子的时间:
next_particle_time = 0
running = True
while running:
# [...]
current_time = pygame.time.get_ticks()
if current_time > next_particle_time:
particles.append(Particule((255,0,0), 100))
next_particle_time = current_time + 200 # 0.2 second interval
在循环中绘制和移动所有粒子:
running = True
while running:
# [...]
for p in particles[:]:
p.update()
p.random_movement()
p.life()
if p.radius < 1:
particles.remove(p)
完整示例
import pygame, random
# -- Variables --
pygame.init()
clock = pygame.time.Clock()
fps_limit = 60.0
resx = 1600
resy = 900
screen = pygame.display.set_mode([resx,resy])
pygame.display.set_caption("Baguette")
# -- Classes --
class Particule():
def __init__ (self,color,radius):
pygame.sprite.Sprite.__init__(self)
self.color = color
self.radius = radius
self.position_x = random.randint(radius, 1600-radius)
self.position_y = random.randint(radius, 900-radius)
def update(self):
pygame.draw.circle(screen, self.color, (self.position_x,self.position_y),self.radius)
def random_movement(self):
self.position_x += random.randint(-10,10)
self.position_y -= random.randint(-5,5)
def life(self):
self.radius -= 1
# -- Objects --
particles = []
next_particle_time = 0
# -- Game loop --
running = True
while running:
clock.tick(fps_limit)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
current_time = pygame.time.get_ticks()
if current_time > next_particle_time:
particles.append(Particule((255,0,0), 100))
next_particle_time = current_time + 200 # 0.2 second interval
screen.fill((0,0,0))
for p in particles[:]:
p.update()
p.random_movement()
p.life()
if p.radius < 1:
particles.remove(p)
print(len(particles))
pygame.display.flip()
pygame.quit()
推荐阅读
- javascript - 来自数组的动态 HTML 行
- amazon-s3 - AWS 胶水将动态帧写入内存不足 (OOM)
- haskell - 是否有一个 Monad 收集结果并将其映射到它们?
- delphi - 未声明的标识符 soAllDirectories
- reactjs - 反应路线不渲染特定组件
- javascript - 为什么我的 javascript 选框不能顺利移动?
- python - 为什么我的 tkinter 标签突然消失了?
- r - 使用 dplyr 对不同长度个体的时间序列组进行平均
- python - model.compile() 在 keras tensorflow 中做了什么?
- mongodb - MongoDB:当 Primary 关闭时,MongoClient 无法连接到辅助副本集