gpt4 book ai didi

python - 如何获得渲染的 Sprite 图像的连续移动(pygame)

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:00 25 4
gpt4 key购买 nike

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

我尝试过 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()

我没有收到任何错误,只是想知道如何解决角色仅在按键时移动一次而不是只要按下键就移动的问题。非常感谢你帮助我。我真的很感激。

最佳答案

pygame.KEYDOWN 事件仅在按下按键时发生一次,因为 Sprite 不会连续移动。当按键被释放时,您会收到 pygame.KEYUP 事件的通知。您可以使用此事件来设置一个状态变量来指示是否按下了某个键。在 pygame.KEYDOWN 上设置变量并在 pygame.KEYUP 上重置变量。
幸运的是 pygame 可以为你做到这一点。 pygame.key.get_pressed()返回 bool 值列表,其中包含所有键的“按下”状态。当 pygame.event.pump() 处理事件时,会设置 pygame.key.get_pressed() 返回的状态。或pygame.event.get() .

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

关于python - 如何获得渲染的 Sprite 图像的连续移动(pygame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56606359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com