Skip to main content
 首页 » 编程设计

python之按住按钮时动画循环不起作用

2024年09月07日11me-sa

我一直在使用 Pygame,但遇到了这个问题。 我制作了 2 个动画列表。一个名为 rightImages 的图像包含我的角色向右行走的图像,另一个名为 leftImages 的图像正好相反。当我反复按下 D 键(我的程序的控件是 A 向左,D 向右等)时,动画会起作用,但是当我按住它时,动画不会运行。希望您能理解,如果您不理解,请运行该程序,然后也许您会明白我在说什么。这是我的代码:

def Main(): 
 
 
    x_change = 0 
    y_change = 0 
    x = 400 
    y = 400 
    counter = 0 
    counter2 = 0 
    player = pygame.image.load('knight_left.png') 
 
    while True: 
 
        rightImages = ['knight_right.png','knight_right1.png','knight_right2.png'] 
        leftImages =['knight_left.png', 'knight_left1.png', 'knight_left2.png'] 
        for event in pygame.event.get(): 
            if event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_d: 
                    x_change += 5 
                    counter += 1 
                    if counter >= len(rightImages): 
                        counter = 0 
                    player = pygame.image.load(rightImages[counter]) 
 
                elif event.key == pygame.K_a: 
                    x_change -= 5 
                    counter2 += 1 
                    if counter2 >= len(leftImages): 
                        counter2 = 0 
                    player = pygame.image.load(leftImages[counter2]) 
                elif event.key == pygame.K_w: 
                    y_change -= 5 
                elif event.key == pygame.K_s: 
                    y_change += 5 
            elif event.type == pygame.KEYUP: 
                x_change = 0 
                y_change = 0 
 
 
        x = x+x_change 
        y = y+y_change 
 
        gameDisplay.fill(white) 
        gameDisplay.blit(player,(x,y)) 
        pygame.display.update() 
 
        clock.tick(30) 
 
Main() 

这是向右行走时通过动画循环运行的代码。

counter += 1 
if counter >= len(rightImages): 
    counter = 0 
player = pygame.image.load(rightImages[counter]) 

这是在向左走的时候。

counter2 += 1 
if counter2 >= len(rightImages): 
    counter2 = 0 
player = pygame.image.load(rightImages[counter2]) 

如果你能告诉我如何在按住键的情况下循环浏览动画列表,那就太棒了!

注意:我没有包含我的所有代码。

请您参考如下方法:

我通常会做类似的事情:首先在主循环开始之前加载图像,因为从硬盘读取很慢。

将当前选择的图像分配给变量 active_images 并在玩家按下某个键时将它们换出,例如active_images = right_images。

在主循环中,您只需检查玩家是否移动,然后递增 counter(或 timer 变量)并将当前图像分配给 player 变量:player = active_images[counter]。

import pygame as pg 
 
 
def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 
    white = pg.Color('white') 
 
    x_change = 0 
    y_change = 0 
    x = 400 
    y = 400 
    # Load the images before the main loop starts. You could also 
    # do this in the global scope. 
    right_images = [] 
    for img_name in ['knight_right.png','knight_right1.png','knight_right2.png']: 
        right_images.append(pg.image.load(img_name).convert_alpha()) 
    left_images = [] 
    for img_name in ['knight_left.png', 'knight_left1.png', 'knight_left2.png']: 
        left_images.append(pg.image.load(img_name).convert_alpha()) 
 
    # This variable refers to the currently selected image list. 
    active_images = left_images 
    counter = 0  # There's no need for a second counter. 
    player = active_images[counter]  # Currently active animation frame. 
 
    while True: 
        for event in pg.event.get(): 
            if event.type == pg.QUIT: 
                return 
            elif event.type == pg.KEYDOWN: 
                if event.key == pg.K_d: 
                    x_change = 5 
                    active_images = right_images  # Swap the image list. 
                elif event.key == pg.K_a: 
                    x_change = -5 
                    active_images = left_images  # Swap the image list. 
                elif event.key == pg.K_w: 
                    y_change -= 5 
                elif event.key == pg.K_s: 
                    y_change += 5 
            elif event.type == pg.KEYUP: 
                if event.key == pg.K_d and x_change > 0: 
                    x_change = 0 
                    counter = 0  # Switch to idle pose. 
                    player = active_images[counter] 
                elif event.key == pg.K_a and x_change < 0: 
                    x_change = 0 
                    counter = 0  # Switch to idle pose. 
                    player = active_images[counter] 
                elif event.key == pg.K_w and y_change < 0: 
                    y_change = 0 
                elif event.key == pg.K_s and y_change > 0: 
                    y_change = 0 
 
        if x_change != 0:  # If moving. 
            # Increment the counter and keep it in the correct range. 
            counter = (counter+1) % len(active_images) 
            player = active_images[counter]  # Swap the image. 
 
        x += x_change 
        y += y_change 
 
        screen.fill(white) 
        screen.blit(player, (x, y)) 
        pg.display.flip() 
        clock.tick(30) 
 
 
if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit()