使用Pygame在Python中制作动画

时间:2017-06-21 17:42:50

标签: python pygame

我一直试图制作游戏,但我无法让动画发挥作用。当我启动游戏时,它会将所有图像加载到其自身之上并且它没有动画效果。 这是我的代码:

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()

由于某些原因它没有动画它只是加载彼此顶部的所有图像请帮助我。感谢。

1 个答案:

答案 0 :(得分:3)

是的,您的for img in img_names:循环只会显示所有图像。我的建议是将images / 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