繁体   English   中英

python pygame精灵动画

[英]Python pygame sprite animation

我正在尝试学习python的pygame库。

我目前正在使用Spritesheets学习Sprite动画。 我在“ javascript”中关注此教程: http : //gamedevelopment.tutsplus.com/tutorials/an-introduction-to-spritesheet-animation--gamedev-13099

我试图用python翻译代码,但是它不起作用。 它只显示完整的Sprite表,没有动画。

这是我的翻译代码:

import pygame,math
from pygame.locals import *
pygame.init()

screen = pygame.display.set_mode((400, 300))
done = False
img='jj.png'

class sprites:
    def sprite_sheet(self,img,frame_width,frame_height,frame_speed,end_frame):
        self.images=pygame.image.load(img)
        #calculate number of frames in a row
        self.frames_per_row=math.floor(self.images.get_rect().size[0]/frame_width)
        self.current_frame=0
        self.counter=0
        self.frame_speed=frame_speed
        self.end_frame=end_frame
    def update(self):
        if self.counter==self.frame_speed-1:
            self.current_frame=(self.current_frame+1) % self.end_frame
        self.counter=(self.counter+1) % self.frame_speed

character=sprites()
character.sprite_sheet(img,125,125,3,16)


clock=pygame.time.Clock()
while not done:
        #screen.fill((255,255,255))
        character.update()
        screen.blit(character.images,(30,100))

        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True


        #screen.blit(images,(0,0))#,(current_image*32,0,32,32))
        pygame.display.flip()
        clock.tick(30)

pygame.quit()

为什么不起作用? 如何通过上面的教程链接为Spritesheet制作动画?

您正在在屏幕上显示character.images:

screen.blit(character.images,(30,100))

这是该类变量包含的内容:

self.images=pygame.image.load(img)

此变量永远不会更新,因此自然不会显示任何动画。

这是我使用此图片作为Sprite工作表的实现:

import pygame, sys
from pygame.locals import *

sheet = 'sheet.png'

pygame.init()
screen = pygame.display.set_mode((500, 500))

FPS = 30
FPSCLOCK = pygame.time.Clock()

class Character(object):
    def __init__(self, sheet, width, height, end):
        self.sheet = pygame.image.load(sheet)
        self.width = width
        self.height = height
        self.end = end
        self.counterX = 0
        self.counterY = 0

        self.imageWidth = self.sheet.get_rect().size[0]
        self.imageHeight = self.sheet.get_rect().size[1]
        self.spritesPerRow = self.imageWidth / self.width
        self.spritesPerCol = self.imageHeight / self.height

        # These assertions will raise an exception if image size, sprite size
        # and sprite count don't add up as they should be. You can experiment
        # with this by changing the values when this object is instantiated.
        assert self.imageWidth % self.width == 0, 'Incorrect sprite width!'
        assert self.imageHeight % self.height == 0, 'Incorrect sprite height!'
        assert self.spritesPerRow * self.spritesPerCol == self.end, \
                                        'Incorrect number of sprites!'

    def update(self):
        # The counters keep track of which sprite to display from the
        # sprite sheet.
        self.spriteArea = (self.counterX * self.width, 
                             self.counterY * self.height,
                             self.width, self.height)
        self.counterX += 1
        if self.counterX == self.spritesPerRow:
            self.counterX = 0
            self.counterY += 1
        if self.counterY == self.spritesPerCol:
            self.counterX = 0
            self.counterY = 0

Test = Character(sheet, 125, 125, 16)

# Displays the sprite sheet for one second.
screen.blit(Test.sheet, (0, 0))
pygame.display.update()
pygame.time.wait(1000)

while True:
    for event in pygame.event.get(QUIT):
        pygame.quit()
        sys.exit()

    screen.fill((255, 255, 255))
    Test.update()
    screen.blit(Test.sheet, (188, 188), Test.spriteArea)
    pygame.display.update()
    FPSCLOCK.tick(FPS)

免责声明:

我也是Pygame / Python的新手,我不知道这种方法是否有效……但至少它能按预期工作!

另一个可能性是使用多个图像和一个计数器。

我的初始化文件(对于我自己的游戏中的玩家等级)如下所示(我放出了一些不重要的内容):

def init(self,x,y,...):
   self.x=x;self.y=y
   self.dir=0           #0==left,1==right

   self.img1_l=img1_l   #the image-files for walking left
   self.img2_l=img2_l   #

   self.img1_r=img1_r   #the image-files for walking right
   self.img2_r=img2_r   #

   self.img_r=img_r     #Idle-image right
   self.img_l=img_l     #Idle-image left

   self.image=self.img_r#the image for idle

   self.anim_counter=-1 #animations counter
   self.max_anim_counter=60 #in my chase 1 second means 60 FPS
   self.rect=self.image.get_rect()

在我的更新功能:

def update(self):       #this is called every frame
   self.anim_counter-=1 #I used this to track the passed time
   if self.anim_counter > 30:
      if self.dir=0:
         self.image=self.img1_l
      else:
         self.image=self.img1_r
   elif self.anim_counter >0:
      if self.dir=0:
         self.image=self.img2_l
      else:
         self.image=self.img2_r
  else:                 #this is called if you don't press any walking button
     if self.dir==0:
        self.image=self.img_l
     else:
        self.image=self.img_r
     self.rect=self.image.get_rect()

在我的绘图功能中(用于在屏幕上显示所有内容):

def draw(self):
   screen.blit(self.image,self.rect)

如果图像尺寸不同,则需要更改坐标。 我的游戏是用Python 3.6和Pygame 1.9.3编写的,但它也可以在其他所有Python 3.x版本中使用。

希望对您有所帮助。 尽管此代码效率不高,但对我来说效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM