微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 pygame 显示错误的图像?

如何解决为什么 pygame 显示错误的图像?

我正在制作一款守护者游戏,您可以在其中向敌人发射子弹并进行进化。当您按 r 键时,您的图像会发生变化。当玩家按下 r 键时,我希望玩家转向的图像是一个带有红色轮廓的橙色三角形。在您按下 r 键之前播放器的图像是一个带有橙色轮廓的黄色三角形。但是,当我按下 r 键时,不是变成带有红色轮廓的橙色三角形,而是变成带有橙色轮廓的黄色大三角形。

这是播放器类(名为 polygon):

class polygon(Sprite):
    """Is it going to be a triangle,a square,a pentagon,or something else?"""

    def __init__(self,sides,window,x,y,size=None):
        self.size = size
        self.sides = sides
        self.window = window
        self.x = x
        self.y = y
        self.check_image()

        Sprite.__init__(self,self.window,self.x,self.y,self.image,self.size)

    def check_for_movement(self,move_amount=1):
        """Let's get moving!"""

        key = pygame.key.get_pressed()

        if key[K_w]:
            self.y -= move_amount
        if key[K_s]:
            self.y += move_amount
        if key[K_a]:
            self.x -= move_amount
        if key[K_d]:
            self.x += move_amount

    def check_for_power_up(self):
        key = pygame.key.get_pressed()

        if key[K_r]:
            self.upgrade()

    def evolve(self):
        self.sides += 1
        self.check_image()

    def check_image(self):
        if self.sides == 3:  # Is it a triangle?
            self.image = "triangle.png"
        elif self.sides == 4:  # A square?
            self.image = "square.png"
        elif self.sides == 5:  # Or even a pentagon?
            self.image = "pentagon.png"
        elif self.sides == 6:  # A hexagon?
            self.image = "hexagon.png"

    @staticmethod
    def check_bullet_type():
        """Who would win? A bullet or a bomb?"""
        global bullet_type,bullet_type_word

        key = pygame.key.get_pressed()

        if key[K_2]:
            bullet_type = 2
            bullet_type_word = "Bomb"
        if key[K_1]:
            bullet_type = 1
            bullet_type_word = "Bullet"

    def upgrade(self):
        if self.sides == 3:
            self.image = "triangle_plus.png"

        self.check_image()

这是我的循环:

while running:
    """Event loop"""
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        """These lines creates a bullet object when the player clicks the screen"""
        if event.type == MOUSEBUTTONDOWN:
            if bullet_type == 1:
                if player.sides == 6:
                    bullet_group.add(Bullet(6,screen,player.x + 3,player.y + 5,size=(270,140)))
                elif player.sides == 5:
                    bullet_group.add(Bullet(5,140)))
                elif player.sides == 4:
                    bullet_group.add(Bullet(4,player.x + 185,player.y + 30,size=(320,190)))
                elif player.sides == 3:
                    bullet_group.add(Bullet(3,player.x + 4,player.y - 15,size=(170,100)))

            elif bombs > 0 and bullet_type == 2:
                """This is for bomb firing detection"""
                if player.sides == 3:
                    bullet_group.add(Bomb(screen,player.y + 2,size=(370,240)))
                elif player.sides == 4:
                    bullet_group.add(Bomb(screen,240)))
                elif player.sides == 5:
                    bullet_group.add(Bomb(screen,player.x + 5,240)))
                elif player.sides == 6:
                    bullet_group.add(Bomb(screen,player.x - 10,240)))

                """This will teach the player not to spam bombs!"""
                bombs -= 1

        """This creates a enemy every 2 seconds"""
        if event.type == enemy_event:
            if player.sides == 3:
                enemy_group.add(Enemy(3,random.randint(0,900),50,size=(230,160)))
            elif player.sides == 4:
                enemy_given_score = 20
                enemy_group.add(Enemy(4,size=(550,350)))
            elif player.sides == 5:
                enemy_given_score = 30
                enemy_group.add(Enemy(5,size=(390,215)))
            elif player.sides == 6:
                enemy_given_score = 40
                enemy_group.add(Enemy(6,215)))

    screen.fill(THECOLORS["skyblue"])

    """Checks the bullet type"""
    player.check_bullet_type()

    """Updates the player position and drawing the player"""
    player.update()
    player.check_for_movement(9)

    """Allows the bullets to appear and fire towards the top of the screen"""
    bullet_group.draw(screen)
    bullet_group.update()

    """
    Updates the enemy sprite(s) and allows the enemy to fly 
    toward the bottem of the screen
    """
    enemy_group.draw(screen)
    enemy_group.update()

    """Draws the CollectableBombs"""
    bomb_group.draw(screen)
    bomb_group.update()

    """Updates and blits the score text"""
    score_text_surface = font.render("score: {:,}".format(score),True,(0,0))
    screen.blit(score_text_surface,(650,10))

    """Updates and blits the lives text"""
    lives_text_surface = font.render("Lives: {:,}".format(lives),0))
    screen.blit(lives_text_surface,60))

    """Updates and blits the bombs text"""
    bombs_text_surface = font.render("Bombs: {:,}".format(bombs),0))
    screen.blit(bombs_text_surface,110))

    """Updates and blits the bullet type text"""
    bullet_type_text_surface = font.render("Bullet Type: {0}".format(bullet_type_word),0))
    if bullet_type == 1:
        screen.blit(bullet_type_text_surface,(550,160))
    elif bullet_type == 2:
        screen.blit(bullet_type_text_surface,(555,160))

    """This blits the game over image if it's game over"""
    if lives <= 0:
        game_over_bg = Image(screen,390,300,"game_over.png")
        screen.blit(game_over_bg.image,game_over_bg.rect)

    """
    If a bullet and a enemy collide,delete them both and increase score by 1.
    If you are lucky enough,you may even get a bomb!
    """
    for i in enemy_group:
        if pygame.sprite.spritecollide(i,bullet_group,True):
            score += enemy_given_score
            if random.choice([0,1]) == 1:
                bombs += 1
            i.kill()

    """POWER UPPPPPPPPPP!"""
    player.check_for_power_up()

    """If a enemy and the player collide,decrease lives by 1"""
    if pygame.sprite.spritecollide(player,enemy_group,True):
        lives -= 1

    """Evolves the player into a square if score is in the 100's"""
    if keep_checking_for_square and 100 <= score < 201:
        player.evolve()
        keep_checking_for_square = False

    """Evolves the player into a pentagon if score is in the 200's"""
    if keep_checking_for_pentagon and 200 <= score < 301:
        player.evolve()
        player.__init__(5,player.x,player.y,size=(385,230))  # Yeah,this line is for changing the size of the player
        keep_checking_for_pentagon = False

    """Evolves the player into a hexagon if score is from 300 to 410"""
    if keep_checking_for_hexagon and 300 <= score < 411:
        player.evolve()
        player.__init__(6,size=(475,280))  # This line also changes the size of the player
        keep_checking_for_hexagon = False

    """If score is 1000 or more,victory!"""
    if score >= 1000:
        cake = Sprite(screen,SCREEN_X // 2 + 10,SCREEN_Y // 2 + 30,"cake.png")
        screen.fill(Color(71,181,245))
        screen.blit(cake.image,cake.rect)

        win_text_surface_upper = font.render("CONGRATULATIONS!",0))  # WINNER WINNER
        win_text_surface_lower = font.render("YOU SAVED THE INFINITE SIDED polyGON!",0))  # CHICKEN DINNER

        screen.blit(win_text_surface_upper,(SCREEN_X // 2 - 155,SCREEN_Y // 2 - 165))
        screen.blit(win_text_surface_lower,(SCREEN_X // 2 - 275,SCREEN_Y // 2 + 75))

    pygame.display.flip()
    clock.tick(40)

解决方法

在您的 Sprite.upgrade() 函数中,您首先分配 self.image = "triangle_plus.png",但随后立即调用 check_image(),然后覆盖 self.image,撤消您之前的更改。更新图像后,您可能想要return

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。