简体   繁体   English

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

[英]How to get continuous movement of rendered sprite image ( pygame )

I'd like to run the application to move the character continuously as long as a key is pressed, this code only manipulates the rendered image once upon key press. 我想运行该应用程序,以便只要按下一个键就可以连续移动字符,此代码仅在按下键时操作一次渲染的图像。

I've tried while loops in hopes that while the key is pressed, perform action, but no luck. 我尝试了while循环,希望在按下键的同时执行操作,但是没有运气。 the program runs the loop over and over effectively crashing it. 程序一遍又一遍地运行循环,有效地使其崩溃。 I've been stuck thinking about this and it's quite literally driving me crazy. 我一直在思考这个问题,这确实使我发疯。

link to see what is happening: https://youtu.be/iuNmwgUqH4c 链接以查看正在发生的情况: https : //youtu.be/iuNmwgUqH4c

I want him not to just move once but continue to move as long as the key is pressed. 我希望他不只是移动一次,而是只要按下键就继续移动。

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

I don't get any errors, Just wondering how to fix this problem of the character only moving once upon key press instead of as long as the key is pressed. 我没有任何错误,只是想知道如何解决该问题,即该字符仅在按键时移动一次,而不是在按键时移动一次。 Thank you so much for helping me out. 非常感谢您对我的帮助。 I really appreciate it. 我真的很感激。

The pygame.KEYDOWN event occurse onyl once when a key is pressed, because of that the sprite doesn't move continuously. pygame.KEYDOWN事件在按下某个键时发生一次onyl,因为该sprite不会连续移动。 When the key is released, then you get notified once by a pygame.KEYUP event. 释放键后,会通过pygame.KEYUP事件通知您一次。 You can use this events to set a state variable that indicates if a key is pressed. 您可以使用此事件来设置状态变量,该状态变量指示是否按下了键。 Set the variable on pygame.KEYDOWN and reset the varaible on pygame.KEYUP . 设置在可变pygame.KEYDOWN和复位的varaible pygame.KEYUP
Fortunately pygame can do that for you. 幸运的是pygame可以为您做到这一点。 pygame.key.get_pressed() returns a list of bools, with the "pressed" state of all the keys. pygame.key.get_pressed()返回pygame.key.get_pressed()值列表,所有键均处于“按下”状态。 The states which are returned by pygame.key.get_pressed() are set when the events are handled by pygame.event.pump() or pygame.event.get() . 当事件由pygame.event.pump()pygame.event.get()处理时,将设置pygame.key.get_pressed()返回的状态。

Use pygame.key.get_pressed() to get the state of the keys after the event loop and compute the movement dependent on the state of the keys: 使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM