2015-04-19 125 views
0

我想學習python的pygame庫。Python pygame sprite動畫

我目前正在使用spritesheets學習精靈動畫。我在「javascript」中關注本教程:http://gamedevelopment.tutsplus.com/tutorials/an-introduction-to-spritesheet-animation--gamedev-13099

我試圖翻譯python的代碼,但它不工作;它只顯示完整的精靈表並且沒有動畫。

這裏是我翻譯的代碼:

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製作動畫?

回答

0

你在屏幕上阻擊器character.images:

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

下面是這個類變量包含:

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

這個變量是從來沒有更新,所以自然也不會顯示任何動畫。

下面是一個使用this image作爲精靈表我的實現:

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的新手,以及,我沒有一個線索,這是否方法是有效的......但至少它按預期工作!

0

另一個可能性是使用多個圖像和一個計數器。

我的初始化文件(在我自己的遊戲我的播放器類)看起來是這樣的(我讓一些不重要的事情了):

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) 

你需要改變,如果IMAG座標es的尺寸不一樣。 我的遊戲是用Python 3.6和Pygame 1.9.3編寫的,但它也適用於其他所有Python 3.x版本。

我希望它能幫助你。雖然這段代碼效率不高,但對我來說工作正常。