【问题标题】:Python 2D Sprite AnimationPython 2D 精灵动画
【发布时间】:2018-11-22 10:10:52
【问题描述】:

我正在尝试使用循环为精灵设置动画,这样每次循环遍历数组中图像的位置编号时都会增加一。我不断收到“UnboundLocalError:分配前引用的局部变量'Antic'”。有

    Antic = 0
Antic = int(Antic)
# Global constants
StAnmtn = ["Images/PlayerVampireStanding1.png", " 
Images/PlayerVampireStanding2.png", 
"Images/PlayerVampireStanding3.png","Images/PlayerVampireStanding4.png", 
"Images/PlayerVampireStanding5.png", "Images/PlayerVampireStanding6.png", 
"Images/PlayerVampireStanding7.png", "Images/PlayerVampireStanding8.png"]

` 在开始和

    def main():
    """ Main Program """
    pygame.init()

    clock = pygame.time.Clock()     # creates clock to limit frames per 
    second

    FPS = 60  # sets max speed of min loop

    SCREENSIZE = SCREENWIDTH, SCREENHEIGHT = 1300, 700  # sets size of 
    screen/window
    screen = pygame.display.set_mode(SCREENSIZE)  # creates window and game 
    screen

    pygame.display.set_caption("Count Acheron")

    # Create the player
    player = Player()

    # Create all the levels
    level_list = []
    level_list.append( Level_01(player) )

    # Set the current level
    current_level_no = 0
    current_level = level_list[current_level_no]

    active_sprite_list = pygame.sprite.Group()
    player.level = current_level

    player.rect.x = 340
    player.rect.y = SCREEN_HEIGHT - player.rect.height
    active_sprite_list.add(player)

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # -------- Main Program Loop -----------
    while not done:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.go_left()
                if event.key == pygame.K_RIGHT:
                    player.go_right()
                if event.key == pygame.K_UP:
                    player.jump()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and player.change_x < 0:
                    player.stop()
                if event.key == pygame.K_RIGHT and player.change_x > 0:
                    player.stop()  

        if Antic > 6:
            Antic = 0
        else:
            Antic += 1
        # Update the player.
        active_sprite_list.update()

        # Update items in the level
        current_level.update()

        # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
        current_level.draw(screen)
        active_sprite_list.draw(screen)

        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT

        # Limit to 60 frames per second
        clock.tick(60)

        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
        # Be IDLE friendly. If you forget this line, the program will 'hang'
        # on exit.
        pygame.quit()

    if __name__ == "__main__":
        main()

作为循环。看起来不喜欢的是代码的sn-p

    if Antic > 6:
        Antic = 0
    else:
        Antic += 1

我该如何解决这个问题?

【问题讨论】:

  • 你用的是哪个版本的python?
  • 啊好的,刚刚看到标签
  • 现在我遇到了变量 Antic 的问题,它似乎没有根据 Antic 的值从数组中找到文件。我认为这是因为您不能在数组的 [] 中使用变量,但我不知道任何功能替代方案...

标签: python python-3.x animation pygame


【解决方案1】:

我个人从未使用过 pygame 的 sprite 模块。

import pygame

class character:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.sprites = [pygame.image.load("img1.png"), pygame.image.load("img2.png")]
        self.frame = 0

    def draw(self, surface):
        surface.blit(self.sprites[self.frame], (self.x, self.y))
        self.frame += 1
        if self.frame > len(self.sprites) - 1: self.frame = 0

pygame.init()
DS = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()

c = character(640, 360)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            pygame.quit()

    c.draw(DS)
    pygame.display.update()
    clock.tick(30)
    DS.fill((0, 0, 0))

【讨论】:

    【解决方案2】:

    需要对函数进行全球化(主循环中的“global Antic”)

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 2023-04-06
      • 1970-01-01
      • 2011-12-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多