2012-11-03 138 views
0

所以,我希望我的玩家精靈在沒有真正移動的情況下爲動畫製作動畫,因爲我希望它保持在屏幕中間,但它不能正確動畫。無法移動的精靈動畫

import pygame, sys 
from pygame.locals import * 



# Game window setup 
pygame.init() 
screen=pygame.display.set_mode((0,0), FULLSCREEN) 



# Colour setup 
green=(0,255,0) 
white=(255,255,255) 
black=(0,0,0) 

# Speed in pixels per frame 
x_speed=0 
y_speed=0 


# Defining the character 
class Player(pygame.sprite.Sprite): 



    # This is a frame counter used to determing which image to draw 
    frame = 0 

    # Constructor 
    def __init__(self): 
     # Call the parent class (Sprite) constructor 
     pygame.sprite.Sprite.__init__(self) 


     # Lists that the character images will be saved in. 
     self.images_moveright=[] 
     self.images_moveleft=[] 
     self.images_movedown=[] 
     self.images_moveup=[] 

     # Load default character image 
     img=pygame.image.load("Player_images\character_default.png") 

     for i in range(1,3): 
      down = pygame.image.load("Player_images\character_movedown"+str(i)+".png").convert() 
      down.set_colorkey(white) 
      self.images_movedown.append(down) 

      left=pygame.image.load("Player_images\character_moveleft"+str(i)+".png").convert 
      #left.set_colorkey(white) 
      self.images_moveleft.append(left) 

      right=pygame.image.load("Player_images\character_moveright"+str(i)+".png").convert() 
      right.set_colorkey(white) 
      self.images_moveright.append(right) 

      up=pygame.image.load("Player_images\character_moveup"+str(i)+".png").convert() 
      up.set_colorkey(white) 
      self.images_moveup.append(up) 

     self.image=img 
     self.rect=self.image.get_rect() 

    def moveright(self): 
     if self.frame<10: 
      self.image=self.images_moveright[0] 
      self.frame+=1 
     if self.frame>=10: 
      self.image=self.images_moveright[1] 
      if self.frame==20: 
       self.frame=0 







player=Player() 


# Current position of player and background 
screen_width=screen.get_width() 
screen_height=screen.get_height() 

player_x_coord=screen_width/2 
player_y_coord=screen_height/2 

background_initial_coordx=0 
background_initial_coordy=0 

player.rect.x=player_x_coord 
player.rect.y=player_y_coord 


background=pygame.image.load("background0.png").convert() 
all_sprites_list=pygame.sprite.RenderPlain() 
all_sprites_list.add(player) 

# Used to manage how fast the screen updates 
clock=pygame.time.Clock() 

# Main game loop 
while 1: 
    for event in pygame.event.get(): 
     if event.type==QUIT: 
      pygame.quit() 
      sys.exit() 

     # Control definition 
     elif event.type == KEYDOWN:  
      if event.key == K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
      if event.key == pygame.K_LEFT:     
       x_speed=3  
      if event.key == pygame.K_RIGHT: 
       player.moveright() 
       x_speed=-3    
      if event.key == pygame.K_UP:     
       y_speed=3    
      if event.key == pygame.K_DOWN:     
       y_speed=-3 



     if event.type == pygame.KEYUP: 
      if event.key == pygame.K_LEFT:     
       x_speed=0     
      if event.key == pygame.K_RIGHT: 
       x_speed=0     
      if event.key == pygame.K_UP: 
       y_speed=0     
      if event.key == pygame.K_DOWN: 
       y_speed=0 




    # Update of game map 
    background_initial_coordx += x_speed 
    background_initial_coordy += y_speed 






    screen.fill(black) 
    time=pygame.time.get_ticks() 
    screen.blit(background, [background_initial_coordx, background_initial_coordy]) 
    all_sprites_list.draw(screen) 

    clock.tick(60) 


    print(time) 

    pygame.display.flip() 

到目前爲止,我只設置控件向右移動,我有兩個圖像動態顯示(,無論是在列表self.images_moveright player_moveright1和player_moveright2)。但是,它只顯示第二張圖片,但不是第一張,顯然,我希望兩張圖片都是每10張。

回答

0

在你的函數

def moveright(self): 
    if self.frame<10: 
     self.image=self.images_moveright[0] 
     self.frame+=1 
    if self.frame>=10: 
     self.image=self.images_moveright[1] 
     if self.frame==20: 
      self.frame=0 

你是不是你的遞增self.frame。

將self.frame + = 1移動到函數末尾以解決此問題。

+0

我做到了,但它表明只有第一個圖像, self.images_moveright [0],從來沒有第二。 – user1783738

+1

如何調試你的代碼?在兩個if語句中打印一些東西,並查看它們是否都被執行 –

0

我認爲你每次按右鍵時只會迭代self.frame一次。這意味着你必須重複點擊右鍵來改變它。我會做這樣的:

def moveRight(): 
    self.image_list = self.images_moveright 

// in your main loop call something like this, which is a method of your player 

def playerTick(self): 
    self.ticker += 1 
    if self.ticker >= 10: 
    self.ticker = 0 
    self.frame += 1 
    if self.frame >= 1: // i think you could use >= self.image_list.len() - 1 
     self.frame = 0 
     self.image = image_list[self.frame] 

我希望幫助,我沒有測試它,它只是我怎麼會做...