pygame 如何在同一屏幕上显示两个不同的动画

how to show two different animation on same screen in pygame

我有一个问题,当我 运行 这个程序时,屏幕上显示的唯一动画是敌人而不是玩家,当我发射子弹(使用 for 循环)时,只有播放器和子弹显示。我无法将两者都放在屏幕上。我尝试将 player.draw()enemy.ENEMY() 直接放在主 while 循环中,但结果是一样的。

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()

这是因为你在 enemy.ENEMY()player.draw() 中都 blitting window 背景。这会清除 window,或覆盖其他图像。看来你应该删除那些,因为 characters() 也在 blitting 背景。