pythonpygamepycharmprojectile

Character Looks the Other way when jumping, and projectiles also come out the other way


if there is anyone who would be willing to help me with two simple problems (which I annoyingly can't find the solution to), I would be forever grateful. I've tried several solutions from the internet already but they seem to provide me with more errors.

Essentially, I have a 2d Platformer that allows you to move left and right, jump and shoot. However, when you move right, then jump, it faces you in the opposite direction [walking left and jumping works perfectly fine]. I assume it has something to do with a "DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using int is deprecated" error, but I may be wrong. Once you have moved right and jumped and it faces you in the opposite direction, the bullets also go in the 'right' direction, despite the sprite appearing to face the left of the window. If anyone who would help me is having difficulty addressing the issue please contact me and I will help as much as I can while I try to figure it out myself as well. Much Thanks in advance. Anthony

Please, if anybody could look at my code and guide me to how to fix this I would, again, be forever grateful.

Full Code: https://pastebin.pl/view/0bb0b42c

Suspected-error part of code:

    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

Solution

  • The issue is caused by setting player.right = False and player.left = False when SPACE is pressed. Since the direction information is stored in player.right and player.left, the information will be lost. Keep the state of player.right and player.left when jumping:

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