如何获得渲染的精灵图像(pygame)的连续运动

时间:2019-06-15 00:14:30

标签: python pygame rendering sprite continuous

我想运行该应用程序以连续移动字符,只要按下一个键,此代码仅在按下键时操纵一次渲染的图像。

我尝试了while循环,希望在按下键的同时执行操作,但是没有运气。程序一遍又一遍地运行循环,有效地使其崩溃。我一直在思考这个问题,这确实使我发疯。

链接查看正在发生的事情:https://youtu.be/iuNmwgUqH4c

我希望他不要只移动一次,只要按下键就可以继续移动。

#======
# Imports 
#======

import pygame 
import sys

#======
# Variables 
#======

pygame.init()

Game_Over = False 
WIDTH = 800
HEIGHT = 800

MSprites = [pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Still ( Down ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Down ) F1.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Down ) F2.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Standing Walking ( Left ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Left ) F1.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Left ) F2.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Still ( Right ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Right ) F1.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Up ) F1.png "), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Up ) F2.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Up ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Still ( Idle ).png")]

NAME = "Survival Game"
WHITE = (225,225,225)
CLOCK = pygame.time.Clock()
FPS = 60
Player_WIDTH = 150
Player_HEIGHT = 150
P_X = 400 - Player_WIDTH
P_Y = 400 - Player_HEIGHT
P_SPEED = 10

#======
# Initialization Code
#======

while not Game_Over:

    CLOCK.tick(2)
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption(NAME)
    screen.fill(WHITE)
    screen.blit(MSprites[0],(P_X,P_Y))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            # minor issue below ( f key for full screen )
            if event.key == pygame.K_f:
                pygame.display.toggle_fullscreen()

            if event.key == pygame.K_a:
                screen.fill(WHITE)
                screen.blit(MSprites[3],(P_X,P_Y))
                P_X = P_X - P_SPEED
                pygame.display.update()
                CLOCK.tick(FPS)
                screen.fill(WHITE)
                screen.blit(MSprites[4],(P_X,P_Y))
                P_X = P_X - P_SPEED
                pygame.display.update()

            if event.key == pygame.K_d:
                screen.fill(WHITE)
                screen.blit(MSprites[6],(P_X,P_Y))
                P_X = P_X + P_SPEED
                pygame.display.update()
                CLOCK.tick(FPS)
                screen.fill(WHITE)
                screen.blit(MSprites[7],(P_X,P_Y))
                P_X = P_X + P_SPEED
                pygame.display.update()

我没有收到任何错误,只是想知道如何解决该问题,即该字符仅在按键时移动一次,而不是在按键时移动一次。非常感谢您对我的帮助。我真的很感激。

1 个答案:

答案 0 :(得分:0)

pygame.KEYDOWN事件在按键时会发生一次,因为该精灵不会连续移动。释放密钥后,您将通过pygame.KEYUP事件得到一次通知。您可以使用此事件来设置状态变量,该状态变量指示是否按下了键。在pygame.KEYDOWN上设置变量,并在pygame.KEYUP上重置变量。
幸运的是pygame可以为您做到这一点。 pygame.key.get_pressed()返回布尔值列表,所有键均处于“按下”状态。由pygame.event.pump()pygame.event.get()处理事件时,将设置pygame.key.get_pressed()返回的状态。

使用pygame.key.get_pressed()获取事件循环后的键状态,并根据键状态计算移动量:

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(NAME)

while not Game_Over:

    CLOCK.tick(2)

    screen.fill(WHITE)
    screen.blit(MSprites[0],(P_X,P_Y))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            # minor issue below ( f key for full screen )
            if event.key == pygame.K_f:
                pygame.display.toggle_fullscreen()

    # get key states
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        screen.fill(WHITE)
        screen.blit(MSprites[3],(P_X,P_Y))
        P_X = P_X - P_SPEED
        pygame.display.update()
        CLOCK.tick(FPS)
        screen.fill(WHITE)
        screen.blit(MSprites[4],(P_X,P_Y))
        P_X = P_X - P_SPEED
        pygame.display.update()

    if keys[pygame.K_d]:
        screen.fill(WHITE)
        screen.blit(MSprites[6],(P_X,P_Y))
        P_X = P_X + P_SPEED
        pygame.display.update()
        CLOCK.tick(FPS)
        screen.fill(WHITE)
        screen.blit(MSprites[7],(P_X,P_Y))
        P_X = P_X + P_SPEED
        pygame.display.update()