Search code examples
pythonimagefile-uploadpygamepygame-surface

why is my character bliting two different images at the same time


im making a game and a the character is ment to show a gun animation depending on what position he was facing here is the code the determines that

class Player(object):

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.velo = 8
        self.left = False
        self.right = False
        self.walkcount = 0
        self.Idlecount = 0
        self.guncount = 0
        self.gunisfired = False
        self.isIdle = False
        self.standing = True

    def draw(self, screen):
        #draws all the animations to the screen
        if self.walkcount + 1 >= 27:
            self.walkcount = 0
        elif self.Idlecount + 1 >= 27:
            self.Idlecount = 0
        if not(self.standing):
            if self.left:
                screen.blit(walkLeft[self.walkcount//3], (self.x,self.y))
                self.walkcount += 1
                if self.gunisfired == True:
                    if self.guncount//5 >= len(ShootR):
                        self.guncount = 0
                        self.gunisfired == False
                    screen.blit(ShootR[self.guncount//5], (self.x,self.y))
                    self.guncount += 1

            if self.right:
                screen.blit(walkRight[self.walkcount//3], (self.x,self.y))
                self.walkcount += 1
                if self.gunisfired == True:
                    if self.guncount//5 >= len(ShootR):
                        self.guncount = 0
                        self.gunisfired == False
                    screen.blit(ShootR[self.guncount//5], (self.x,self.y))
                    self.guncount += 1
        else:
            if self.left:
                if self.isIdle == True:
                    if self.Idlecount//3 >= len(IdleL):
                        self.Idlecount = 0
                        self.isIdle == False
                    screen.blit(IdleL[self.Idlecount//3], (self.x,self.y))
                    self.Idlecount += 1
                if self.gunisfired == True:
                    if self.guncount//5 >= len(ShootL):
                        self.guncount = 0
                        self.gunisfired == False
                    screen.blit(ShootL[self.guncount//5], (self.x,self.y))
                    self.guncount += 1
            else:
                if self.Idlecount//3 >= len(IdleR):
                    self.Idlecount = 0
                    self.isIdle == False
                screen.blit(IdleR[self.Idlecount//3], (self.x,self.y))
                self.Idlecount += 1
                if self.gunisfired == True:
                    if self.guncount//5 >= len(ShootR):
                        self.guncount = 0
                        self.gunisfired == False
                    screen.blit(ShootR[self.guncount//5], (self.x,self.y))
                    self.guncount += 1

        pygame.display.update()

and here is the code for the keyboard bindings

man = Player(300, 508, 64, 64)
run = True
while run:
    clock.tick(27)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    
    if keys[pygame.K_LEFT] and man.x > man.velo:
        man.x -= man.velo
        man.right = False
        man.left = True
        man.Idlecount = 0
        man.guncount = 0
        man.gunisfired = False
        man.standing = False
    elif keys[pygame.K_RIGHT] and man.x < 600 - man.width - man.velo:
        man.x += man.velo
        man.right = True
        man.left = False
        man.Idlecount = 0
        man.guncount = 0
        man.gunisfired = False
        man.standing = False
    elif keys[pygame.K_SPACE]:
        man.gunisfired = True
        man.isIdle = False
        man.standing = True
        man.Idlecount = 0
    else:
        man.walkcount = 0
        man.isIdle = True
        man.guncount = 0
        man.gunisfired = False
        man.standing = True

The ting is that the gun image is working when he is facing left but when it is right it seems to blit it with the forst image of the idle animation. can someone please help me.


Solution

  • if self.isIdle == True is missing in the else case of the method draw:

    class Player(object):
        # [...]
    
        def draw(self, screen):
            # [...]
    
            if not(self.standing):
                # [...]
    
            else:
                if self.left:
                   # [...]
    
                else:
    
                    if self.isIdle == True:  # <---- this is missing
                        if self.Idlecount//3 >= len(IdleR):
                            self.Idlecount = 0
                            self.isIdle == False
                        screen.blit(IdleR[self.Idlecount//3], (self.x,self.y))
                        self.Idlecount += 1
                    if self.gunisfired == True:
                        if self.guncount//5 >= len(ShootR):
                            self.guncount = 0
                            self.gunisfired == False
                        screen.blit(ShootR[self.guncount//5], (self.x,self.y))
                        self.guncount += 1