【发布时间】:2017-07-10 14:08:31
【问题描述】:
由于我还是编程新手,我正在尝试提出一些基本程序来帮助我理解编码和学习。
我正在尝试使用 pygame 创建一个向上发射小粒子的对象。一切正常,但我找不到控制对象创建这些粒子的速率的方法。我有一个 Launcher 和 Particle 类,以及一个 Launcher 和粒子列表。你需要程序的所有行吗?这是基本设置:
particles = []
launchers = []
class Particle:
def __init__(self, x, y):
self.pos = np.array([x, y])
self.vel = np.array([0.0, -15])
self.acc = np.array([0.0, -0.5])
self.colors = white
self.size = 1
def renderParticle(self):
self.pos += self.vel
self.vel += self.acc
pygame.draw.circle(mainscreen, self.colors, [int(particles[i].pos[0]), int(particles[i].pos[1])], self.size, 0)
class Launcher:
def __init__(self, x):
self.width = 10
self.height = 23
self.ypos = winHeight - self.height
self.xpos = x
def drawLauncher(self):
pygame.draw.rect(mainscreen, white, (self.xpos, self.ypos, self.width, self.height))
def addParticle(self):
particles.append(Particle(self.xpos + self.width/2, self.ypos))
while True :
for i in range(0, len(launchers)):
launchers[i].drawLauncher()
launchers[i].addParticle()
# threading.Timer(1, launchers[i].addparticle()).start()
# I tried that thinking it could work to at least slow down the rate of fire, it didn't
for i in range(0, len(particles)):
particles[i].renderParticle()
我使用鼠标将新的启动器添加到数组中,并使用 while 循环来渲染所有内容。就像我说的,我想找到一种方法来控制我的 Launcher 在程序仍在运行时吐出这些粒子的速度(所以 sleep() 不能工作)
【问题讨论】: