为子弹创建颗粒的踪迹
我正在创建一个太空射击游戏,每当用户单击左鼠标时,太空飞船都会发射子弹,并且由于我希望子弹更加谨慎,所以我希望他们在被解雇后在后面有一小块颗粒。我尝试了我的代码(最终是我下面将显示的代码),但结果不好。
- 颗粒不断粘在子弹上。
- 只有一个粒子出现。
我应该做什么来解决这个问题?这是我的代码:
bullet_surf = pygame.image.load('graphics for b/beam.png')
bullet_surf = pygame.transform.rotozoom(bullet_surf,0,0.08)
bullets = []
while True:
....
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
bullets.append([[*ship_rect.midright],6,[random.randint(-20,20)/10,random.randint(-20,20)/10]])
for b in range(len(bullets)):
bullets [b][0][0] +=20
for bullet in bullets[:]:
if bullet [0][0] > 1100:
bullets.remove(bullet)
for bullet in bullets:
bullet_rect = bullet_surf.get_rect(midleft = (bullet[0][0],bullet[0][1]))
screen.blit(bullet_surf,bullet_rect)
bullet_rect.centerx += bullet[2][0]
bullet_rect.centery += bullet[2][1]
bullet[1] -= 0.1
print(bullet_rect.center)
pygame.draw.circle(screen,(255,255,255),bullet_rect.center,int(bullet[1]))
I'm creating a space shooting game in which a spaceship will fire out bullets every time the user clicks the left mouse and since I want the bullets to be a bit fancier I want them to have a trail of particles behind them after being fired. I tried something to my code (which ended up to be the code that I'll show below) yet the result isn't good.
- The particles keep sticking to the bullets.
- Only one particle appeared.
What should I do to fix this? This is my code:
bullet_surf = pygame.image.load('graphics for b/beam.png')
bullet_surf = pygame.transform.rotozoom(bullet_surf,0,0.08)
bullets = []
while True:
....
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
bullets.append([[*ship_rect.midright],6,[random.randint(-20,20)/10,random.randint(-20,20)/10]])
for b in range(len(bullets)):
bullets [b][0][0] +=20
for bullet in bullets[:]:
if bullet [0][0] > 1100:
bullets.remove(bullet)
for bullet in bullets:
bullet_rect = bullet_surf.get_rect(midleft = (bullet[0][0],bullet[0][1]))
screen.blit(bullet_surf,bullet_rect)
bullet_rect.centerx += bullet[2][0]
bullet_rect.centery += bullet[2][1]
bullet[1] -= 0.1
print(bullet_rect.center)
pygame.draw.circle(screen,(255,255,255),bullet_rect.center,int(bullet[1]))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为每个子弹创建一个粒子列表。我建议使用a class> class for Bullets:
将新粒子添加到列表中并在子弹移动时对列表中的粒子进行动画:
在循环中绘制列表中的所有粒子:
最小示例
通过鼠标发射子弹的示例:
You need to create a list of particles for each bullet. I suggest using a Class for the bullets:
Add a new particle to the list and animate the particles in the list as the bullet moves:
Draw all the particles in the list in a loop:
Minimal example
Example of firing bullets by mouse click: