角色在跳跃时看起来是另一种方式,投射物也会从另一种方向射出



如果有人愿意帮助我解决两个简单的问题(令人恼火的是,我找不到解决方案(,我将永远感激。我已经从互联网上尝试了几种解决方案,但它们似乎给我带来了更多的错误。

从本质上讲,我有一个2d Platformer,可以让你左右移动,跳跃和射击。然而,当你向右移动,然后跳跃时,它会朝相反的方向面向你[向左移动和跳跃效果非常好]。我认为这与";DeprecationWarning:需要一个整数(获取的类型为float(。不赞成使用int隐式转换为整数"错误,但我可能错了。一旦你向右移动并跳跃,它朝着相反的方向,子弹也会朝着"右"方向移动,尽管精灵看起来面向窗口的左侧。如果任何愿意帮助我的人在解决这个问题时遇到困难,请联系我,我会尽我所能提供帮助,同时我也会自己解决。非常感谢。Anthony

请,如果有人能看看我的代码并指导我如何解决这个问题,我将再次永远感激。

完整代码:https://pastebin.pl/view/0bb0b42c

代码的可疑错误部分:

def draw(self,screen):
if self.walkCount + 1 >= 21:
self.walkCount = 0
if not(self.standing):
if self.left:
screen.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
screen.blit(self.walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount += 1
else:
if self.right:
screen.blit(self.walkRight[0], (self.x, self.y))
else:
screen.blit(self.walkLeft[0], (self.x, self.y))
def move(self,x,y): #creating the move object so we can call it later and move our sprite
if x != 0:
self.move_single_axis(x,0)
if y != 0:
self.move_single_axis(0,y)
def move_single_axis(self, x, y):
self.rect.x += x
def update(self):
if self.y > H - height:
self.y += 10

&

for bullet in bullets:
if bullet.x < 800 and bullet.x > 0: #Making sure the bullet isn't off the screen.
bullet.x += bullet.vel #move our bullet according to velocity
else:
bullets.pop(bullets.index(bullet)) #Deleting our bullet by popping (removing) from list.

keys = pygame.key.get_pressed() # creating a variable to check for key presses
if keys [pygame.K_UP]:
if player.left:
facing = -1 #determining direction of bullet (left = -1)
else:
facing = 1 #determining direction of bullet (right = +1)                
if len(bullets) < 6: #no. of bullets on screen at once.
bullets.append(Bullet(round(player.x + player.width // 2), round(player.y + player.height // 2), 6, (0,0,0), facing))

if keys[pygame.K_LEFT] and player.x > player.vel:
player.x -= player.vel
player.right = False  
player.left = True
player.standing = False

elif keys[pygame.K_RIGHT]:
player.x += player.vel
player.right = True
player.left = False
player.standing = False
if player.x >= W-width:
player.x = W-width
else:
player.standing = True
player.walkCount = 0

if not (player.isJump):
if keys[pygame.K_SPACE]:
player.isJump = True
player.right = False
player.left = False
player.walkCount = 0
else:
if player.jumpCount >= -11.5:
neg = 1
if player.jumpCount < 0:
neg = -1
player.y -= (player.jumpCount ** 2) * 0.5 * neg
player.jumpCount -= 1
else:
player.isJump = False
player.jumpCount = 11.5

此问题是由按下SPACE时设置player.right = Falseplayer.left = False引起的。由于方向信息存储在player.rightplayer.left中,因此信息将丢失。跳转时保持player.rightplayer.left的状态:

if not (player.isJump):
if keys[pygame.K_SPACE]:
player.isJump = True
# player.right = False <--- DELETE
# player.left = False  <--- DELETE
player.walkCount = 0

最新更新