【问题标题】:why is my character bliting two different images at the same time为什么我的角色会同时闪烁两个不同的图像
【发布时间】:2021-03-30 13:06:39
【问题描述】:

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

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。谁能帮帮我。

【问题讨论】:

标签: python image file-upload pygame pygame-surface


【解决方案1】:

if self.isIdle == True在方法draw的else情况下丢失:

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

【讨论】:

  • 谢谢你说我应用了它并且它有效
猜你喜欢
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 2015-05-30
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
相关资源
最近更新 更多