pygame: 精灵的动作没有改变

pygame: sprite's action not changing

我一直在尝试制作一款类似于pygame中的塔防游戏。本质上,有僵尸在路上行走,你必须射击它们(十字准线是你的鼠标)。我有 3 个文件:crosshair.py、spritesheet.py 和 main.py。我可以从我的 sprite sheet 中获取行走动画,但是,当我想让它向上或向下行走时,它看向的方向不会改变。

这是精灵 sheet 的样子:

这是我的代码:

spritesheet.py:

import pygame

class SpriteSheet(pygame.sprite.Sprite):
    def __init__(self, width, height, sheet):
        super().__init__()
        self.action = 2
        self.width = width
        self.height = height
        self.sheet = sheet
    
    def get_image(self):
        self.animation_list = []

        for _ in range(4):
            temp_list = []
            for count in range(4):
                self.image = pygame.Surface((self.width, self.height))
                self.image.blit(self.sheet, (0, 0), (count * self.width, self.action * self.height, self.width, self.height))
                self.image.set_colorkey('Black')

                temp_list.append(self.image)

            self.animation_list.append(temp_list)

main.py:

import pygame
from crosshair import Crosshair
from spritesheet import SpriteSheet

pygame.init()

# Clock/Time 
clock = pygame.time.Clock()
FPS = 60

# Make Screen
screen_w = 1000
screen_h = 650
screen = pygame.display.set_mode((screen_w, screen_h))

crosshair = Crosshair('crosshair_white.png', screen)

# Load background
bg = pygame.image.load('background.jpg').convert_alpha()
bg = pygame.transform.scale(bg, (screen_w, screen_h))

# Handle Zombie Sheet
zombie_sheet = pygame.image.load('zombie_img.png').convert_alpha()
sprite_sheet = SpriteSheet(32, 48, zombie_sheet)
sprite_sheet.get_image()

frame = 0

run = True

zombie_x = 0
zombie_y = 400

while run:
    screen.fill('Black')

    screen.blit(bg, (0, 0))

    # Zombie Management
    screen.blit(sprite_sheet.animation_list[sprite_sheet.action][int(frame)], (zombie_x, zombie_y))
    if frame > 3.9:
        frame = 0
    else:
        frame += 0.07

    dx = 0
    dy = 0

    if zombie_x < 300:
        dx = 0.5
    elif 300 <= zombie_x < 390:
        dx = 0.5
        dy = -0.25
    elif 390 <= zombie_x < 460 and zombie_y > 210:
        dy = -0.5
    elif zombie_x < 460 and zombie_y <= 210:
        dx = 0.5
        dy = -0.25
    elif 460 <= zombie_x < 510:
        dx = 0.5
    elif 510 <= zombie_x < 570:
        dx = 0.5
        dy = 0.25
    elif zombie_y < 270:
        dy = 0.5
    elif 570 <= zombie_x < 630 and zombie_y >= 270:
        dx = 0.5
        dy = 0.5
    elif 630 <= zombie_x < 760:
        dx = 0.5
    elif 760 <= zombie_x < 830:
        dx = 0.5
        dy = 0.5
    elif zombie_x >= 830:
        dx = 0.5

    zombie_x += dx
    zombie_y += dy

    crosshair.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()

    pygame.display.update()
    clock.tick(FPS)

pygame.quit()

我不会 post crosshair.py 的代码,因为我认为这不是必需的。

基本上,我想通过写 sprite_sheet.action = 3:

来改变这里的动作
elif 390 <= zombie_x < 460 and zombie_y > 210:
------->
        dy = -0.5

然而,它并没有改变,而且我知道这个问题不是因为我制作的曲面不正确,因为如果我手动将 self.action 更改为不同的数字,它就会起作用。

如果需要任何其他信息,我很乐意提供,但请注意,除了 crosshair.py.

之外,这是我仅有的两个文件

提前致谢。

我没有测试,但我认为你在 get_image() 中为所有方向创建了相同的图像,因为它们都使用相同的 self.action

for _ in range(4):

    # ... code ...

    self.image.blit(self.sheet, (0, 0), 
                 (count * self.width, 
                  self.action * self.height,   # <--- self.action
                  self.width, 
                  self.height))

但他们应该使用 for _ in range(4)

中的值
for direction in range(4):   # <--- direction

    # ... code ...

    self.image.blit(self.sheet, (0, 0), 
                 (count * self.width, 
                  direction * self.height,   # <--- direction
                  self.width, 
                  self.height))

全功能:

    def get_image(self):
        self.animation_list = []

        for direction in range(4):   # <--- direction

            temp_list = []

            for count in range(4):
                self.image = pygame.Surface((self.width, self.height))

                self.image.blit(self.sheet, (0, 0), 
                     (count * self.width, 
                      direction * self.height,   # <--- direction from `for 
                      self.width, 
                      self.height))

                self.image.set_colorkey('Black')

                temp_list.append(self.image)

            self.animation_list.append(temp_list)