如何在一定时间后更改精灵?

时间:2016-11-12 22:51:10

标签: python pygame python-3.5

我正在制作游戏,我希望在几帧之后将精灵从打孔图像更改为立场图像,但我可以弄明白。我试图找到一个解决方案,这是我的最后一招。提前谢谢。

import pygame
import os
import time

pygame.init()
# Screen
display_width = 1350
display_height = 700
screen = pygame.display.set_mode((display_width, display_height))

# Colors
white = (255, 255, 255)
frame = 0

# Constants
clock = pygame.time.Clock()

# Images
img = pygame.image.load("stand.png")
stand_img = pygame.image.load('stand.png')
crouch_img = pygame.image.load('crouch.png')
jump_img = pygame.image.load('jump.png')
punch_img = pygame.image.load('punch.png')

class Fighter:
    def __init__(self, x_pos, y_pos, image, health):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.image = image
        self.health = health

    def draw(self):
        screen.blit(self.image, (self.x_pos, self.y_pos))

    def move(self, accel_x, accel_y):
        self.x_pos += accel_x
        self.y_pos += accel_y

    def damage(self, damage_done):
        self.health -= damage_done

    def punch(self, direction):
        c = 0
        while c < 30:
            c += 1
            if direction == 'left':
                self.image = pygame.transform.flip(punch_img, True, False)
                self.x_pos -= 91
            if direction == 'right':
                self.image = punch_img
            pygame.display.update()

class MainRun:
    def __init__(self, displayw, displayh):
        self.dw = displayw
        self.dh = displayh

    def MainLoop(self):
        global frame
        playing = True
        time_up = 0
        frame_cycle = 0

        # Player 1 Info
        P1_x = display_width - 400
        P1_y = 400
        P1_direction = 'left'
        P1_accel_x = 0
        P1_accel_y = 0
        P1_health = 200
        player_1 = Fighter(P1_x, P1_y, img, P1_health)

        while playing:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    playing = False

                if event.type == pygame.KEYDOWN:
                    # Player 1 Event Handlers
                    if event.key == pygame.K_LEFT:
                        P1_accel_x = -10
                        P1_accel_y = 0
                        P1_direction = 'left'
                    elif event.key == pygame.K_RIGHT:
                        P1_accel_x = 10
                        P1_accel_y = 0
                        P1_direction = 'right'
                    elif event.key == pygame.K_UP:
                        player_1.y_pos = 300
                        player_1.image = jump_img
                    elif event.key == pygame.K_DOWN:
                        player_1.y_pos = 500
                        player_1.image = crouch_img
                    elif event.key == pygame.K_PERIOD:
                        player_1.punch(P1_direction)

                if event.type == pygame.KEYUP:
                    # Player 1 Event Handlers
                    if event.key == pygame.K_LEFT:
                        P1_accel_x = 0
                        P1_accel_y = 0
                    if event.key == pygame.K_RIGHT:
                        P1_accel_x = 0
                        P1_accel_y = 0
                    if event.key == pygame.K_UP:
                        player_1.y_pos = P1_y
                        player_1.image = stand_img
                    if event.key == pygame.K_DOWN:
                        player_1.y_pos = P1_y
                        player_1.image = stand_img
                    if event.key == pygame.K_PERIOD:
                        player_1.image = stand_img
                        if P1_direction == 'left':
                            player_1.x_pos += 91

            screen.fill(white)
            player_1.move(P1_accel_x, P1_accel_y)
            player_1.draw()
            pygame.display.update()
            frame += 1
            if frame == 29:
                frame = 0
                frame_cycle += 1

            #print(frame_cycle, frame)
            clock.tick(60)

        pygame.quit()
        quit()


run = MainRun(1000, 600)
run.MainLoop()

0 个答案:

没有答案