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

从 pygame 中的精灵表创建动画时遇到问题

如何解决从 pygame 中的精灵表创建动画时遇到问题

我对 Python 还比较陌生,几天前开始玩弄 pygame。我在 photoshop 中制作了一个精灵表,我试图用它来为游戏中的玩家设置动画。精灵表有 8 个图像,4 个用于向左行走,4 个用于向右行走。我想在正确的时间展示这些动画。我已经尝试了许多在网上找到的不同方法,但都没有成功,我仍然对此感到困惑。

这是控制角色移动和精灵的 Char 类。我尝试索引精灵并更改更新方法中的索引,但我知道这是不可能的,但我已经将其保留以显示我正在尝试做的事情。任何帮助将不胜感激!

import pygame,sys
from pygame.sprite import Sprite

class Char(Sprite):

    def __init__(self,pf_game):

        """initialise char and set its starting location"""
        self.screen = pf_game.screen
        self.settings = pf_game.settings
        self.screen_rect = pf_game.screen.get_rect()


        self.dog_sprite = pygame.image.load('images/dog_sprite_sheet.bmp').convert()
        self.current_sprite = 0
        self.dog_image = self.dog_sprite[self.current_sprite]
        self.rect = self.dog_image.get_rect()

        # start new char at the bottom of centre of the screen
        self.rect.midbottom = self.screen_rect.midbottom

        #store a decimal value for the ships horizontal position
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

        #movement flags
        self.moving_right = False
        self.moving_left = False
        self.is_jump = False

    def update(self):
        """Update the chars position based on movement flags"""
        #update the chars x value and create animation for moving right
        if self.moving_right and self.rect.right<self.screen_rect.right:
            if self.current_sprite == 7:
                self.current_sprite = 4
            self.dog_image = self.dog_sprite[self.current_sprite]
            self.current_sprite+=1
            self.x+= self.settings.char_speed

        #update the chars x value and create animation for moving left
        if self.moving_left and self.rect.left>0:
            if self.current_sprite == 3:
                self.current_sprite = 0
            self.dog_image = self.dog_sprite[self.current_sprite]
            self.current_sprite+=1
            self.x-= self.settings.char_speed

        

        #update rect object from self.x
        self.rect.x = self.x
        self.rect.y = self.y

    def blitme(self):
        """Draw the char at its curretn loaction"""
        self.screen.blit(self.dog_image,self.rect)

编辑: 我发现了一个 spritesheet 类,它似乎可以在 http://www.pygame.org/wiki/Spritesheet 上做我想做的事。但是当我尝试输入坐标时遇到了错误。我收到错误 images_at() got multiple values for argument 'colorkey'。当我删除颜色键时,我收到错误 images_at() takes from 2 to 3 positional arguments but 5 were given。 这是我正在使用的代码

        self.dog_sprite=spritesheet.spritesheet('dog_sprite_sheet.bmp')
        self.left_images=[]
        self.right_images=[]
        self.left_images=self.dog_sprite.images_at((0,19,19),(20,(39,(58,colorkey=(255,255,255))
        self.right_images=self.dog_sprite.images_at((77,(96,(115,(134,255))

解决方法

这可能会有所帮助:

def anim(anim_count):
    if anim_count < 6:
        screen.blit('anim1.png',player_position)
    if anim_count > 6 and anim_count < 12:
        screen.blit('anim2.png',player_position)

你应该在每一帧调用 anim,然后将 1 应用到 anim_count

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