no *_*DHC 1 python pygame effects
我正在创建一个太空射击游戏,其中每次用户单击鼠标左键时,宇宙飞船都会发射子弹,因为我希望子弹更漂亮,所以我希望它们在发射后在其后面有一条粒子轨迹。我对我的代码进行了一些尝试(最终成为我将在下面显示的代码),但结果并不好。
我应该怎么做才能解决这个问题?这是我的代码:
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]))
Run Code Online (Sandbox Code Playgroud)
您需要为每个子弹创建一个粒子列表。我建议为项目符号使用一个类:
self.particles = []
Run Code Online (Sandbox Code Playgroud)
将新粒子添加到列表中,并在子弹移动时为列表中的粒子设置动画:
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)
Run Code Online (Sandbox Code Playgroud)
循环绘制列表中的所有粒子:
for i, particle in enumerate(self.particles):
pygame.draw.circle(screen, particle[2], particle[0], (i//3+2))
Run Code Online (Sandbox Code Playgroud)
最小的例子
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()
Run Code Online (Sandbox Code Playgroud)
通过鼠标点击发射子弹的示例:
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()
Run Code Online (Sandbox Code Playgroud)