【问题标题】:Character Looks the Other way when jumping, and projectiles also come out the other way角色跳跃时向另一方向看,弹丸也从另一方向出来
【发布时间】:2020-12-01 16:58:52
【问题描述】:

如果有人愿意帮助我解决两个简单的问题(我很烦人地找不到解决方案),我将永远感激不尽。我已经尝试了几种来自互联网的解决方案,但它们似乎给我提供了更多错误。

基本上,我有一个 2d 平台游戏,可以让你左右移动、跳跃和射击。但是,当您向右移动然后跳跃时,它会以相反的方向面向您[向左走和跳跃都很好]。我认为它与“DeprecationWarning:需要一个整数(类型为浮点数)。不推荐使用 int 隐式转换为整数”错误有关,但我可能错了。一旦你向右移动并跳跃并且它以相反的方向面对你,子弹也会朝“右”方向移动,尽管精灵看起来面向窗口的左侧。如果任何可以帮助我的人在解决问题时遇到困难,请与我联系,我会尽我所能提供帮助,同时我也会自己解决问题。非常感谢提前。安东尼

如果有人可以查看我的代码并指导我如何解决这个问题,我将再次感激不尽。

完整代码: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

【问题讨论】:

    标签: python pygame pycharm projectile


    【解决方案1】:

    该问题是由按下 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
    

    【讨论】:

    • 哇...感谢一群朋友,由于我是初学者,仍然不确定这是如何工作的,但是您是救生员,感谢您的支持。
    • 随意忽略这一点,但如果你能帮助解决 DeprecationWarning 错误,那也意味着很多。每当我按空格时,它都会给我这个错误。我尝试将指定的值封装在“int”中,甚至尝试“round”,但它根本不起作用。任何指导都会很棒。干杯。出现在第 58、51、60 和 54 行,我尝试使用这行代码“screen.blit(self.walkRight[self.walkCount//3],(self.x,self .y))"
    • @AnthonyMead 见How to fix this DeprecationWarning。例如screen.blit(self.walkLeft[self.walkCount//3], (round(self.x),round(self.y)))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多