首页 > 解决方案 > 为什么我的角色会同时闪烁两个不同的图像

问题描述

我正在制作游戏,并且角色要根据他所面临的位置来显示枪支动画,这是确定的代码

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

这是键盘绑定的代码

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

值得注意的是,当他面向左侧时,枪图像正在工作,但当它面向右侧时,它似乎用空闲动画的第一个图像对其进行了 blit。有人可以帮帮我吗。

标签: pythonimagefile-uploadpygamepygame-surface

解决方案


if self.isIdle == True在方法的 else 情况下丢失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

推荐阅读