程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从 pygame 中的精灵表创建动画时遇到问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到从 pygame 中的精灵表创建动画时遇到问题的问题如何解决?下面主要结合日常开发的经验,给出你关于从 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.bR_896_11845@p').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.bR_896_11845@p')
        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

大佬总结

以上是大佬教程为你收集整理的从 pygame 中的精灵表创建动画时遇到问题全部内容,希望文章能够帮你解决从 pygame 中的精灵表创建动画时遇到问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:pygame