为子弹创建颗粒的踪迹

发布于 2025-02-07 20:47:36 字数 1200 浏览 2 评论 0原文

我正在创建一个太空射击游戏,每当用户单击左鼠标时,太空飞船都会发射子弹,并且由于我希望子弹更加谨慎,所以我希望他们在被解雇后在后面有一小块颗粒。我尝试了我的代码(最终是我下面将显示的代码),但结果不好。

  1. 颗粒不断粘在子弹上。
  2. 只有一个粒子出现。

我应该做什么来解决这个问题?这是我的代码:

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.

  1. The particles keep sticking to the bullets.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2025-02-14 20:47:36

您需要为每个子弹创建一个粒子列表。我建议使用a class> class for Bullets:

self.particles = []

将新粒子添加到列表中并在子弹移动时对列表中的粒子进行动画:

for particle in self.particles:
    particle[0][0] -= 2
    particle[0][1] += particle[1]
particle = [list(self.rect.midleft), random.uniform(-2, 2), pygame.Color(255, random.randrange(255), 0)]
self.particles.append(particle)
if len(self.particles) > 30:
    self.particles.pop(0)

在循环中绘制列表中的所有粒子:

for i, particle in enumerate(self.particles):
    pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))

最小示例

“”

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x, y):
        self.image = pygame.image.load("rocket.png")
        self.rect = self.image.get_rect(center = (x, y))
        self.particles = []

    def move(self):
        for particle in self.particles:
            particle[0][0] -= 2
            particle[0][1] += particle[1]
        particle = [list(self.rect.midleft), random.uniform(-2, 2), pygame.Color(255, random.randrange(255), 0)]
        self.particles.append(particle)
        if len(self.particles) > 30:
            self.particles.pop(0)
        self.rect.centerx += 3

    def draw(self, screen):
        for i, particle in enumerate(self.particles):
            pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))
        screen.blit(self.image, self.rect)
        

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles] 

bullet = Bullet(0, 100)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        
    bullet.move()
    if bullet.rect.left >= 400:
        bullet.rect.right = 0

    window.blit(background, (0, 0))
    bullet.draw(window)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()

通过鼠标发射子弹的示例:

“

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x, y):
        self.image = pygame.image.load("rocket.png")
        self.rect = self.image.get_rect(center = (x, y))
        self.particles = []

    def move(self):
        for particle in self.particles:
            particle[0][0] -= 2
            particle[0][1] += particle[1]
        particle = [list(self.rect.midleft), random.uniform(-2, 2), pygame.Color(255, random.randrange(255), 0)]
        self.particles.append(particle)
        if len(self.particles) > 30:
            self.particles.pop(0)
        self.rect.centerx += 3

    def draw(self, screen):
        for i, particle in enumerate(self.particles):
            pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))
        screen.blit(self.image, self.rect)
        

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles] 

bullets = []    

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullets.append(Bullet(*event.pos))

    for bullet in bullets[:]:     
        bullet.move()
        if bullet.rect.left >= 400:
            bullets.remove(bullet)

    window.blit(background, (0, 0))
    for bullet in bullets[:]:    
        bullet.draw(window)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()

You need to create a list of particles for each bullet. I suggest using a Class for the bullets:

self.particles = []

Add a new particle to the list and animate the particles in the list as the bullet moves:

for particle in self.particles:
    particle[0][0] -= 2
    particle[0][1] += particle[1]
particle = [list(self.rect.midleft), random.uniform(-2, 2), pygame.Color(255, random.randrange(255), 0)]
self.particles.append(particle)
if len(self.particles) > 30:
    self.particles.pop(0)

Draw all the particles in the list in a loop:

for i, particle in enumerate(self.particles):
    pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))

Minimal example

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x, y):
        self.image = pygame.image.load("rocket.png")
        self.rect = self.image.get_rect(center = (x, y))
        self.particles = []

    def move(self):
        for particle in self.particles:
            particle[0][0] -= 2
            particle[0][1] += particle[1]
        particle = [list(self.rect.midleft), random.uniform(-2, 2), pygame.Color(255, random.randrange(255), 0)]
        self.particles.append(particle)
        if len(self.particles) > 30:
            self.particles.pop(0)
        self.rect.centerx += 3

    def draw(self, screen):
        for i, particle in enumerate(self.particles):
            pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))
        screen.blit(self.image, self.rect)
        

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles] 

bullet = Bullet(0, 100)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        
    bullet.move()
    if bullet.rect.left >= 400:
        bullet.rect.right = 0

    window.blit(background, (0, 0))
    bullet.draw(window)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()

Example of firing bullets by mouse click:

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x, y):
        self.image = pygame.image.load("rocket.png")
        self.rect = self.image.get_rect(center = (x, y))
        self.particles = []

    def move(self):
        for particle in self.particles:
            particle[0][0] -= 2
            particle[0][1] += particle[1]
        particle = [list(self.rect.midleft), random.uniform(-2, 2), pygame.Color(255, random.randrange(255), 0)]
        self.particles.append(particle)
        if len(self.particles) > 30:
            self.particles.pop(0)
        self.rect.centerx += 3

    def draw(self, screen):
        for i, particle in enumerate(self.particles):
            pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))
        screen.blit(self.image, self.rect)
        

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles] 

bullets = []    

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullets.append(Bullet(*event.pos))

    for bullet in bullets[:]:     
        bullet.move()
        if bullet.rect.left >= 400:
            bullets.remove(bullet)

    window.blit(background, (0, 0))
    for bullet in bullets[:]:    
        bullet.draw(window)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文