如何在pygame中移动时射击

问题描述 投票:1回答:1

我一直在我所有的游戏中都注意到一个奇怪的事情:如果我移动,则玩家停止射击,无论是我移动时已经在射击,还是我尝试射击时已经在移动(如果我移动,射击停止。这是我在最近制作的一款游戏中用来移动玩家的代码:

def move(self):
    keys = pygame.key.get_pressed()

    if keys[pygame.K_d]:
        self.x += self.speed
    if keys[pygame.K_a]:
        self.x -= self.speed

为了拍摄,我这样做:

     def handle_events():
          if event.type ==  pygame.MOUSEBUTTONDOWN:
          game.bullets.append(Bullet())

任何帮助将不胜感激。还有一个旁注:如果有人知道一般代码中可能导致这种情况的东西(因为它在我的所有游戏中都发生过),请用plaeses注释,以便我可以使用相关事件代码来更新问题,因为现在我不知道可能会导致什么这个。

编辑:我最近注意到的是,当玩家被重力(技术上仍在移动)下拉时,它仍然可以射击。所以我想我在按下按键时会发生某些事情(任何按键,而不仅仅是我代码中包含的音调),这会使玩家无法射击。

我的handle_evnets()函数实际上只是处理关闭游戏和按下鼠标键。反正这是代码:

   def handle_events():
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type ==  pygame.MOUSEBUTTONDOWN:
                game.bullets.append(Bullet())

这里是如果game.bullets会添加到MOUSEBUTTONDOWN的项目符号类>

class Bullet:
        def __init__(self):
            self.pos = [(player.x - 10 - scroll[0]), (player.y + 15 - scroll[1])]
            self.height = 3
            self.width = 20
            self.bullet = pygame.Surface((self.width, self.height)).convert_alpha()
            self.bullet.fill((255, 255, 255))
            self.dir = [(mouse.x - (player.x - 10 - scroll[0])),  (mouse.y - (player.y + 15 - scroll[1]))]
            self.distance = math.hypot(self.dir[0], self.dir[1])
            self.dir = [(self.dir[0]/ self.distance), (self.dir[1]/ self.distance)]
            angle = math.degrees(math.atan2((-self.dir[1]), self.dir[0]))
            self.bullet = pygame.transform.rotate(self.bullet, (angle))
            self.speed = 10

这里是Game类,将子弹添加到具有shoot_bullet()功能的类中后会发生什么。

 class Game:
    def __init__(self):
    self.bullets = []


    def shoot_bullet(self):
        for bullet in self.bullets:
            bullet.pos[0] += bullet.dir[0] * bullet.speed
            bullet.pos[1] += bullet.dir[1] * bullet.speed
            D.blit(bullet.bullet, (bullet.pos[0], bullet.pos[1]))

我在我所有的游戏中都注意到一个奇怪的事情,即如果我移动时玩家停止射击,无论是我移动时已经射击,还是尝试时玩家已经在移动...]]

python pygame
1个回答
0
投票

[我认为,如果按下任何键,它的真正含义是“ pygame.MOUSEBUTTONDOWN”增强功能。

© www.soinside.com 2019 - 2024. All rights reserved.