通过文本输入控制pygame动画

时间:2016-02-13 16:24:42

标签: python pygame

我需要创建一个格斗游戏,通过文本提供提示和接受输入,例如原始输入,然后执行动画,同时仍然有动画的字符,例如在准备战斗的姿态来回移动。我该怎么做呢?

1 个答案:

答案 0 :(得分: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)

所以这里只是一个简短的展示,你可以称之为它的样子。