2016-02-13 84 views
-1

我需要創建一個格鬥遊戲,通過文本(如原始輸入)提供提示並接受輸入,然後執行動畫,同時還保留人物動畫,例如動畫。來回奔跑,準備戰鬥。我會怎麼做呢?通過文本輸入控制pygame動畫

+0

如果您需要平行動畫和文本輸入,我建議使用線程。 – xXliolauXx

+0

Whoa whoa whoa ...不需要線程和並行動畫...我假設OP是含義Fire Emblem風格的東西? – KodyVanRy

+0

如果您希望字符在輸入時有空閒動畫,則需要線程。 – Fredrik

回答

0

請注意,這不會是你典型的答案。當你被困時,StackOverflow可以幫助你完成所有你可以做的事情,但這並不意味着可以作爲代碼的地方,但是因爲我認爲其他人對編程不熟悉也會對這些事情感到困惑。因此,我將編寫一些代碼和一些僞代碼,以便在這種情況下得到恰到好處的結果。

# TODO put your imports up here 

pygame.init() 

clock = pygame.time.Clock() 
gameSurface = pygame.display.set_mode((600, 400)) # 2/3 aspect ratio 
FPS = 40 # Set to your own Frames per second 

class Animator: 
    def __init__(self, surface, rows, cols, time_between_frames, on_finish): 
     self.images = [] 
     self.current_image = 0 
     self.time_between_frames = time_between_frames # time animator waits before changing surface 
     self.current_time # tracks time for frames to change 
     self.on_finish = on_finish # function to call when animation finishes 
     surf_width = (surface.get_width()/cols) # calculate width 
     surf_height = (surface.get_height()/rows) # calculate height 
     for x in range(cols): 
      for y in range(rows): 
       surf = pygame.Surface(surface.get_size()) # temp surface 
       from_rect = pygame.Rect(x * surf_width, y * surf_height, surf_width, surf_height) # rect to blit from 
       surf.blit(surface, (0,0), from_rect) # draw to temp surface 
       self.images.append(surf) # add temp surface to the images list 

    def update(delta): 
     self.current_time += delta # update current time 
     if (self.current_time >= self.time_between_frames): # if time to switch surfaces 
      self.current_time -= self.time_between_frames # take away time from current time 
      self.current_image += 1 # change image 
      if self.current_image >= len(self.images): # if current image would throw an out of bounds exception 
       self.current_image = 0 # reset the current image to the first 

    def get_frame(self): 
     return self.images[self.current_image] 

class Player: 
    resting = 0 
    abdomenKick = 1 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 
     self.action = Player.resting 
     self.restingAnimation = Animation(pygame.image.load("resting.png"), 2, 3, 500) 
     self.abdomenKickAnimation = Animation(pygame.image.load("abdomenKick.png"), 4, 6, 50) 
     self.currentAnimation = self.restingAnimation 

    def update(self, delta): 
     self.currentAnimation.update(delta) 

    def draw(self, surface): 
     surface.blit(self.currentAnimation.get_frame(), (self.x, self.y)) 

    def abdomenKick(self): 
     self.currentAnimation = self.restingAnimation 

class Game: 
    def __init__(self): 
     self.player = Player() 

    def update(self, delta): 
     self.player.update(delta) 

    def draw_screen(self, surface): 
     self.player.draw(surface) 

def gameLoop(): 
    game = Game() 
    while True: 
     for event in pygame.event.get(): 
      if event.type == KEYDOWN: 
       if event.key == A: 
        game.player.abdomenKick() #Or whatever move you have 

     game.update(clock.get_rawtime()) 
     game.draw_screen() 

     clock.tick(FPS) 

所以這裏只是一個簡短的展示,你可以稱它爲什麼可能看起來像。