Search code examples
pythonpygamepython-modulepygame-surface

how to show two different animation on same screen in pygame


I am having a problem that when i run this program the only animation that is shown on the screen is of enemy and not the player and when I fire a bullet(uses for loop) then only the player and the bullet is shown. I am unable to put both on the screen. I tried putting player.draw() and enemy.ENEMY() directly in main while loop but that results were same.

import pygame 

pygame.init()
clock=pygame.time.Clock()

class player:
    def __init__(self):
        self.x,self.y=0,228
        self.vel=4
        self.xb=self.x+64
        self.velb=20
        self.isshot=False
        self.left=False
        self.right=False
        self.walkcount=0
        self.direction='R'

    def draw(self):
        win.blit(bg,(0,0))
        
        if self.walkcount+1>=27:
            self.walkcount=0
        if self.left:
            win.blit(walkleft[self.walkcount//3],(self.x,self.y))
            self.walkcount+=1
        elif self.right:
            win.blit(walkright[self.walkcount//3],(self.x,self.y))
            self.walkcount+=1
        elif self.direction=='L':
            win.blit(pygame.image.load('L1.png'),(self.x,self.y))
        else:
            win.blit(pygame.image.load('R1.png'),(self.x,self.y))
        pygame.display.update()
        
            
        
class enemy():
    def __init__(self):
        self.x=400
        self.y=228
        self.walkcount=3
        self.vel=10
        self.walkright = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png')]
        self.walkleft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png')]
        
    def ENEMY(self):
        self.move()
        win.blit(bg,(0,0))
        
        if self.walkcount+1>=27:
            self.walkcount=0
        if self.vel<0:
            win.blit(self.walkleft[self.walkcount//3],(self.x,self.y))
            self.walkcount+=1
        elif self.vel>0:
            win.blit(self.walkright[self.walkcount//3],(self.x,self.y))
            self.walkcount+=1
        
        pygame.display.update()
        clock.tick(30)
        
    def move(self):
        self.x+=self.vel
        if self.x>500 or self.x<300:
            self.vel*=-1
            
        
win=pygame.display.set_mode((626,313))
pygame.display.set_caption('game')





walkright = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkleft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
bg = pygame.image.load('bg.jpg')
char = pygame.image.load('standing.png')

player=player()
enemy=enemy()
def characters():
    win.blit(bg,(0,0))
    player.draw()
    enemy.ENEMY()
    pygame.display.update()

true=True
while true:
    clock.tick(27)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            true=False 

    key=pygame.key.get_pressed()
        
    if key[pygame.K_LEFT]and player.x>0:
        player.x-=player.vel
        player.xb=player.x
        player.left=True
        player.right=False
        player.direction='L'
    elif key[pygame.K_RIGHT]and player.x<582:
        player.x+=player.vel
        player.xb=player.x
        player.left=False
        player.right=True
        player.direction='R'
    else:
        player.left=False
        player.right=False
        player.walkcount=0

    if key[pygame.K_SPACE]:
        for i in range(626):
            win.blit(bg,(0,0))

            if player.direction=='L':
                win.blit(walkleft[0],(player.x,player.y))
                pygame.draw.rect(win,('black'),(player.xb,player.y+32,5,7))
                pygame.display.update()
                player.xb-=player.velb
            elif player.direction=='R':
                win.blit(walkright[0],(player.x,player.y))
                pygame.draw.rect(win,('black'),(player.xb+44,player.y+32,5,7))
                pygame.display.update()
                player.xb+=player.velb
            clock.tick(27)
            if player.xb>600 or player.xb<0:
                break
            
        player.xb=player.x+10
    characters()

pygame.quit()

Solution

  • It's because you are blitting the window background in both enemy.ENEMY() and player.draw(). This clears the window, or covers the other image up. It seems you should remove those, because characters() is also blitting the background.