2017-06-21 111 views
3

我一直在嘗試製作一款遊戲,但無法使動畫工作。當我啓動遊戲時,它會將所有圖像加載到它的頂部,並且不會生成動畫。 這裏是我的代碼:用Pygame在Python中製作動畫

import pygame 
import os 

pygame.init() 

width = 800 
height = 600 

ship_width = 56 
ship_height = 64 

disp = pygame.display.set_mode((width,height)) 

pygame.display.set_caption("space_game") 

clock = pygame.time.Clock() 

background = pygame.image.load(os.path.join("Backgrounds", "Space.png")) 

img_names = ["sprite_00.png", "sprite_01.png", "sprite_02.png", "sprite_03.png", "sprite_04.png", "sprite_05.png", "sprite_06.png", "sprite_07.png", "sprite_08.png", "sprite_09.png"] #i load all the images here 

all_imgs = {} 
for img in img_names: 
    all_imgs[img] = pygame.image.load(img) 

def gameLoop(): 
    x = (width * 0.45) 
    y = (height * 0.8) 

    x_ch = 0 
    y_ch = 0 

    x_bg = 0 

    gameExit = False 

    while not gameExit: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 

      if event.type == pygame.KEYDOWN: 
       if event.key == ord("a"): 
        x_ch = -5 

       elif event.key == ord("d"): 
        x_ch = 5 

       elif event.key == ord("w"): 
        y_ch = -5 

       elif event.key == ord("s"): 
        y_ch = 5 

      if event.type == pygame.KEYUP: 
       if event.key == ord("a") or event.key == ord("d"): 
        x_ch = 0 

       if event.key == ord("w") or event.key == ord("s"): 
        y_ch = 0 

     x += x_ch 
     y += y_ch 

     if x > width - ship_width or x < 0: 
      x_ch = 0 

     if y > height - ship_height or y < 0: 
      y_ch = 0 

     x_loop = x_bg % background.get_rect().height 
     disp.blit(background, (0, x_loop - background.get_rect().height)) 

     if x_loop < height: 
      disp.blit(background, (0, x_loop)) 

     x_bg += 5 

     for img in img_names: 
      disp.blit(all_imgs[img], (x, y)) #but this part doesnt work it blits 

              #all the images on top of eachother 
     pygame.display.update() 

     clock.tick(60) 

gameLoop() 
pygame.quit() 
quit() 

出於某種原因,它沒有動畫,它只是加載所有的海誓山盟頂部的圖片,請幫助我。謝謝。

回答

3

是的,你的for img in img_names:循環只是blits所有圖像。我的建議是將圖像/ pygame.Surfaces存儲在列表中,然後使用index變量來跟蹤當前圖像。

所以大致爲:

images = [image1, image2, etc.] 
index = 0 

while True: 
    index += 1 
    # Modulo to keep the index in the correct range. 
    index %= len(images) 
    current_image = images[index] 
    disp.blit(current_image, position) 

注意,這個例子是幀速率的約束,我建議遞增一段時間間隔之後index代替。

附錄:減慢動畫下來,比如,你可以計算幀和增量索引只有在frame_count大於3

images = [image1, image2, etc.] 
index = 0 
frame_count = 0 

while True: 
    frame_count += 1 
    if frame_count > 3: 
     frame_count = 0 
     index += 1 
     index %= len(images) 
     current_image = images[index] 

    disp.blit(current_image, position) 

但是,這仍然是幀速率的約束。

做正確的方式,是使用timer變量,並添加時間clock.tick返回到它,如果定時器高於一些任意的門檻,增加了指數和改變形象。

images = [image1, image2, etc.] 
index = 0 
current_image = images[index] 
timer = 0 
dt = 0 

while True: 
    timer += dt 
    # If 1 second has passed. 
    if timer > 1: 
     timer = 0 
     index += 1 
     index %= len(images) 
     current_image = images[index] 

    screen.blit(current_image, position) 

    # dt is the time that has passed since the last clock.tick call. 
    # Divide by 1000 to convert milliseconds to seconds. 
    dt = clock.tick(FPS)/1000 
+0

我可以告訴你一個幀率獨立版本以及如果你想。 – skrx

+0

當我lauch我得到這個錯誤:UnboundLocalError:在分配之前引用的局部變量'索引' –

+0

這意味着你還沒有在你的函數中定義變量'index'。 – skrx