Python 2D精灵动画

2024-10-03 00:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用循环来设置精灵的动画,这样每次循环穿过数组中图像的位置编号都会增加1。我不断得到“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()

作为循环。它似乎不喜欢的是代码片段

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

我该怎么解决这个问题?你知道吗


Tags: thekeyeventifpngcurrentlevelscreen
2条回答

就我个人而言,我从未使用过pygame的精灵模块。你知道吗

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

需要全球化功能(主循环中的“全球滑稽动作”)

相关问题 更多 >