如何修复我的子弹代码,以便当我从不同方向射击第二颗子弹时,不会改变第一颗子弹的方向?



我遇到了一个问题,当我向某个方向发射一颗子弹时,然后我向不同的方向发射第二颗子弹,前一颗子弹也会改变方向。

if bullet.x < 1919 and bullet.x > 0 and facing==-1 or bullet.x < 1919 and bullet.x > 0 and facing==1 :#boundry for bullets made small change
bullet.x += bullet.vel
elif bullet.y < 803 and bullet.y> 0 and facing==2:#boundry for bullets made small change needs added to  main stage
bullet.y -= bullet.vel    
elif bullet.y < 803 and bullet.y > 0 and facing==-2 :#needs added to main stage
bullet.y -= bullet.vel 
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
movemana=25#the cost when using that move need ot make function for  that later
if keys[pygame.K_SPACE] and mana>movemana and shootloop==0:
#print(mana)
mana-=movemana
#print(mana)
if man.up==True:
facing=2
if man.left==True:
facing = -1
if man.right==True:
facing = 1
if man.down==True:
facing=-2
print(facing)
if len(bullets) < 5:
bullets.append(projectile(round(man.x + man.width //2), round(man.y + man.height//2), 6, (0,0,0), facing))
shootloop=1#cooldownrate

看起来只有部分代码存在,但我会尽力使用可用的代码。

似乎所有项目符号都共享相同的全局facing变量。facing设置在代码的底部。然后,我假设,在每个项目符号上调用顶部,这意味着它们都将使用相同的facing值(无论创建最后一个项目符号时设置什么)。

但是,看起来您还将该facing变量存储在创建的每个项目符号中;请参阅底部附近以bullets.append(projectile(...开头的行 该值当前未被使用。因此,在检查项目符号在上半部分的位置时,请尝试使用bullet.facing(或任何该成员的名称)而不仅仅是facing

最新更新