如何在pygame中创建子弹?

4

我知道有几个话题涉及到这个问题,但我仍然不知道如何让我的飞船发射子弹。 我想通过MOUSEBUTTONDOWN事件在播放声音效果时从飞船上发射子弹。 谢谢您的帮助!

import sys, pygame, pygame.mixer
from pygame.locals import *

pygame.init()

size = width, height = 800, 600
screen = pygame.display.set_mode(size)

clock = pygame.time.Clock()

background = pygame.image.load("bg.png")
ship = pygame.image.load("ship.png")
ship = pygame.transform.scale(ship,(64,64))

shot = pygame.mixer.Sound("shot.wav")
soundin = pygame.mixer.Sound("sound.wav")

soundin.play()

while 1:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      sys.exit()

    elif event.type == MOUSEBUTTONDOWN:
      shot.play()

  clock.tick(60)

  mx,my = pygame.mouse.get_pos()

  screen.blit(background,(0,0))
  screen.blit(ship,(mx-32,500))
  pygame.display.flip()
2个回答

9
有几个步骤需要完成。您需要一张子弹的图片,一种存储子弹位置的方法,一种创建子弹的方法,一种呈现子弹的方法和一种更新子弹的方法。您似乎已经知道如何导入图片,因此我将跳过该部分。
有几种存储信息的方式。我将使用一个列表来存储子弹左上角的位置。在最终循环之前的任何位置创建列表,使用bullets = []
要创建子弹,您需要使用鼠标的位置。在shot.play()之后添加bullets.append([event.pos[0]-32, 500]),缩进相同。
要呈现子弹,您需要在游戏循环中添加一个for循环。在screen.blit(background, (0, 0))之后,添加以下代码:
for bullet in bullets:
    screen.blit(bulletpicture, pygame.Rect(bullet[0], bullet[1], 0, 0)

要更新子弹,您需要在游戏循环中添加类似以下代码的内容:
for b in range(len(bullets)):
    bullets[b][0] -= 10

最后,当子弹到达屏幕顶部时,您需要将它们移除。在刚才创建的for循环之后添加以下内容(迭代一个切片副本,因为在迭代过程中不应修改列表):

for bullet in bullets[:]:
    if bullet[0] < 0:
        bullets.remove(bullet)

在将所有内容加入您的代码后,它应该看起来像这样:
import sys, pygame, pygame.mixer
from pygame.locals import *

pygame.init()

size = width, height = 800, 600
screen = pygame.display.set_mode(size)

clock = pygame.time.Clock()

bullets = []

background = pygame.image.load("bg.png").convert()
ship = pygame.image.load("ship.png").convert_alpha()
ship = pygame.transform.scale(ship, (64, 64))
bulletpicture = pygame.image.load("You know what to do").convert_alpha()

shot = pygame.mixer.Sound("shot.wav")
soundin = pygame.mixer.Sound("sound.wav")

soundin.play()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == MOUSEBUTTONDOWN:
            shot.play()
            bullets.append([event.pos[0]-32, 500])

    clock.tick(60)

    mx, my = pygame.mouse.get_pos()

    for b in range(len(bullets)):
        bullets[b][0] -= 10

    # Iterate over a slice copy if you want to mutate a list.
    for bullet in bullets[:]:
        if bullet[0] < 0:
            bullets.remove(bullet)

    screen.blit(background, (0, 0))

    for bullet in bullets:
        screen.blit(bulletpicture, pygame.Rect(bullet[0], bullet[1], 0, 0))

    screen.blit(ship, (mx-32, 500))
    pygame.display.flip()

如果你和我都做得正确,这应该会给你带来有效的子弹。如果你不明白发生了什么或某些东西不起作用,请随时向我提问。
注意,图像/pygame.Surfaces 应使用 convertconvert_alpha 方法进行转换以提高性能。

经过一段时间的研究,我现在已经基本理解了代码的大部分内容,但是我只能让子弹向左或向右射击,而我想让它们朝屏幕顶部射击。此外,由于某种原因,子弹没有被删除,在一两分钟后它们会飞回来。再次感谢! - user3273323
@user3273323 子弹的位置被存储在一个列表[x,y]中,所以你只需要将bullets[b][0]-=10更改为bullets[b][1]-=10。请注意,您可以将-=更改为+=以使其飞向不同的方向,并且可以更改10以更改速度。在那行代码之后的for循环中,if语句将根据刚才讨论的内容进行更改。[0]应更改为在前一行的[b]之后的任何内容,并且仅在使用-=时应使用<。否则,请使用>。 - ThisIsAQuestion
在审查完我的代码后,我意识到你可能已经发现了: 在这一行 bullets.append([event.pos[0]-32, 500]) 中,-32 是不必要的。将其删除后,子弹会在飞船中心而不是边缘创建。只是想让你知道。 - ThisIsAQuestion
非常感谢,现在我更好地理解了它的工作原理。我知道我需要从x改为y,但是我无论如何都想不出来,但我确实明白了+=和-=如何左右变化。希望我能够变得足够好,以回报社会。另外,您有任何想法为什么子弹最终会从屏幕底部返回,就像它们从一侧滚动到另一侧一样,而不是从列表中删除?顺便说一句,我已将“event.pos [0] -32”更改为“mx”,这是我的鼠标x变量。 - user3273323
@user3273323 你确定在代码行 if bullet[0]<0: 中,如果你改变了 y 坐标,< 变成了 >,0 变成了屏幕在子弹移动维度上的尺寸,你已经将 [0] 改为 [1] 了吗?如果你已经这样做了但仍然无法正常工作,请将 for 循环中的所有新代码发送给我,我会帮你查看。 - ThisIsAQuestion
注意,在迭代列表时不应修改它们。在这个例子中,这并不是非常重要,因为子弹仍然会被移除,但有时候可能会晚几帧。更好的方法是迭代列表的副本 for bullet in bullets[:]: - skrx

5
这是一个示例,演示了如何使用 pygame.sprite.Sprite pygame.sprite.Group 创建子弹。它还展示了如何借助计时器变量和 clock.tick 返回的 dt (时间差)连续射击。 pygame.sprite.groupcollide 用于检测子弹与敌人组之间的碰撞。我遍历 hits 字典中的项目以减少敌人的健康点数。
import random
import pygame as pg


pg.init()

BG_COLOR = pg.Color('gray12')
PLAYER_IMG = pg.Surface((30, 50), pg.SRCALPHA)
pg.draw.polygon(PLAYER_IMG, pg.Color('dodgerblue'), [(0, 50), (15, 0), (30, 50)])
ENEMY_IMG = pg.Surface((50, 30))
ENEMY_IMG.fill(pg.Color('darkorange1'))
BULLET_IMG = pg.Surface((9, 15))
BULLET_IMG.fill(pg.Color('aquamarine2'))


class Player(pg.sprite.Sprite):

    def __init__(self, pos, all_sprites, bullets):
        super().__init__()
        self.image = PLAYER_IMG
        self.rect = self.image.get_rect(center=pos)
        self.all_sprites = all_sprites
        self.add(self.all_sprites)
        self.bullets = bullets
        self.bullet_timer = .1

    def update(self, dt):
        self.rect.center = pg.mouse.get_pos()

        mouse_pressed = pg.mouse.get_pressed()
        self.bullet_timer -= dt  # Subtract the time since the last tick.
        if self.bullet_timer <= 0:
            self.bullet_timer = 0  # Bullet ready.
            if mouse_pressed[0]:  # Left mouse button.
                # Create a new bullet instance and add it to the groups.
                Bullet(pg.mouse.get_pos(), self.all_sprites, self.bullets)
                self.bullet_timer = .1  # Reset the timer.


class Enemy(pg.sprite.Sprite):

    def __init__(self, pos, *sprite_groups):
        super().__init__(*sprite_groups)
        self.image = ENEMY_IMG
        self.rect = self.image.get_rect(center=pos)
        self.health = 30

    def update(self, dt):
        if self.health <= 0:
            self.kill()


class Bullet(pg.sprite.Sprite):

    def __init__(self, pos, *sprite_groups):
        super().__init__(*sprite_groups)
        self.image = BULLET_IMG
        self.rect = self.image.get_rect(center=pos)
        self.pos = pg.math.Vector2(pos)
        self.vel = pg.math.Vector2(0, -450)
        self.damage = 10

    def update(self, dt):
        # Add the velocity to the position vector to move the sprite.
        self.pos += self.vel * dt
        self.rect.center = self.pos  # Update the rect pos.
        if self.rect.bottom <= 0:
            self.kill()


class Game:

    def __init__(self):
        self.clock = pg.time.Clock()
        self.screen = pg.display.set_mode((800, 600))

        self.all_sprites = pg.sprite.Group()
        self.enemies = pg.sprite.Group()
        self.bullets = pg.sprite.Group()
        self.player = Player((0, 0), self.all_sprites, self.bullets)

        for i in range(15):
            pos = (random.randrange(30, 750), random.randrange(500))
            Enemy(pos, self.all_sprites, self.enemies)

        self.done = False

    def run(self):
        while not self.done:
            # dt = time since last tick in milliseconds.
            dt = self.clock.tick(60) / 1000
            self.handle_events()
            self.run_logic(dt)
            self.draw()

    def handle_events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.done = True

    def run_logic(self, dt):
        self.all_sprites.update(dt)

        # hits is a dict. The enemies are the keys and bullets the values.
        hits = pg.sprite.groupcollide(self.enemies, self.bullets, False, True)
        for enemy, bullet_list in hits.items():
            for bullet in bullet_list:
                enemy.health -= bullet.damage

    def draw(self):
        self.screen.fill(BG_COLOR)
        self.all_sprites.draw(self.screen)
        pg.display.flip()


if __name__ == '__main__':
    Game().run()
    pg.quit()

要了解精灵、精灵组和类的工作原理,请查看Program Arcade Games(第12章是关于类的)。 - skrx
如果你想要在任意方向上射击,我建议使用向量。这里有一个例子:https://stackoverflow.com/a/42281315/6220679 - skrx
一个更好的方法。 - Was'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接