Pygame 无法在 Y 轴上射击

Pygame cant shoot in Y axis

我想在 Y 轴上拍摄时遇到问题。我不知道问题出在哪里,当我 运行 它时我只能水平射击(X 轴)。该程序没有显示任何错误。 我可以双向射击但是子弹随着角色改变方向,当我修复我只能朝一个方向射击时。

import pygame
import math
pygame.init()

win = pygame.display.set_mode((480, 320))
pygame.display.set_caption("Pandemic")

clock = pygame.time.Clock()

walkup = [pygame.image.load("Soldier-1up.png"), pygame.image.load("Soldier-2up.png"), 
pygame.image.load("Soldier-3up.png")]
walkdown = [pygame.image.load("Soldier-1down.png"), pygame.image.load("Soldier-2down.png"), 
pygame.image.load("Soldier-3down.png")]
walkright = [pygame.image.load("Soldier-1right.png"), pygame.image.load("Soldier-2right.png"), 
pygame.image.load("Soldier-3right.png")]
walkleft = [pygame.image.load("Soldier-1left.png"), pygame.image.load("Soldier-2left.png"), 
pygame.image.load("Soldier-3left.png")]
bg = pygame.image.load("bg.png")
ch = pygame.image.load("Soldier-1up.png")
bulletimg = pygame.image.load("bullet.png")


class player(object):
   def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 5
    self.walkcount = 0
    self.right = False
    self.left = False
    self.up = False
    self.down = False
    self.standing = True

def draw(self, win):
    if self.walkcount + 1 >= 60:
        self.walkcount = 0

    if not (self.standing):
        if self.left:
            win.blit(walkleft[self.walkcount // 20], (self.x, self.y))
            self.walkcount += 1
        elif self.right:
            win.blit(walkright[self.walkcount // 20], (self.x, self.y))
            self.walkcount += 1
        elif self.up:
            win.blit(walkup[self.walkcount // 20], (self.x, self.y))
            self.walkcount += 1
        elif self.down:
            win.blit(walkdown[self.walkcount // 20], (self.x, self.y))
            self.walkcount += 1
    else:
        if self.up:
            win.blit(walkup[0], (self.x, self.y))
        elif self.down:
            win.blit(walkdown[0], (self.x, self.y))
        elif self.right:
            win.blit(walkright[0], (self.x, self.y))
        elif self.left:
            win.blit(walkleft[0], (self.x, self.y))

    class Bullet(object):
        def __init__(self, bullet_x, bullet_y, radius, color, vertfacing, hortfacing):
        self.bullet_x = bullet_x
        self.bullet_y = bullet_y
        self.radius = radius
        self.color = color
        self.speed = 6
        self.vertfacing = vertfacing
        self.hortfacing = hortfacing

def draw(self, screen):
    pygame.draw.circle(screen, self.color, (self.bullet_x, self.bullet_y), self.radius)

def move(self):
    if self.hortfacing == -1:
        self.bullet_x -= self.speed
    elif self.hortfacing == 1:
        self.bullet_x += self.speed
    elif self.vertfacing == 1:
        self.bullet_y += self.speed
    elif self.vertfacing == -1:
        self.bullet_y -= self.speed

    def RedrawGame():
      win.blit(bg, (0, 0))
      man.draw(win)
      for bullet in bullets:
      bullet.draw(win)
      pygame.display.update()

     #Loop
     man = player(80, 100, 64, 64)
     bullets = []
     vertfacing = -1
     hortfacing = 1
     run = True
     while run:
     clock.tick(60)

     for event in pygame.event.get():
         if event.type == pygame.QUIT:
           run = False

    for bullet in bullets:
       if bullet.bullet_x < 480 and bullet.bullet_x > 0 and bullet.bullet_y < 320 and bullet.bullet_y > 0:
        bullet.move()
    else:
        bullets.remove(bullet)

keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE]:
    if man.left:
        hortfacing = -1
    elif man.right:
        hortfacing = 1
    elif man.up:
        vertfacing = -1
    elif man.down:
        vertfacing = 1

    if len(bullets) < 100:
        bullet = Bullet(round(man.x + man.width // 2),
                            round(man.y + man.height // 2), 6, (255, 165, 0),
                            vertfacing, hortfacing)
        bullets.append(bullet)



if keys[pygame.K_a] and man.x >= man.vel:
    man.x -= man.vel
    man.right = False
    man.left = True
    man.up = False
    man.down = False
    man.standing = False

elif keys[pygame.K_d] and man.x < 480 - man.width:
    man.x += man.vel
    man.right = True
    man.left = False
    man.up = False
    man.down = False
    man.standing = False

elif keys[pygame.K_s] and man.y < 320 - man.height:
    man.y += man.vel
    man.right = False
    man.left = False
    man.up = False
    man.down = True
    man.standing = False

elif keys[pygame.K_w] and man.y >= man.vel:
    man.y -= man.vel
    man.right = False
    man.left = False
    man.up = True
    man.down = False
    man.standing = False

else:
    man.standing = True
    man.walkcount = 0

RedrawGame()

pygame.quit()

您必须根据方向设置 hortfacingvertfacing。首先将hortfacingvertfacing设置为0,然后根据方向设置相应的变量:

if keys[pygame.K_SPACE]:
    hortfacing, vertfacing = 0, 0 
    if man.left:
        hortfacing = -1
    elif man.right:
        hortfacing = 1
    elif man.up:
        vertfacing = -1
    elif man.down:
        vertfacing = 1