如何获得渲染精灵图像的连续移动(pygame)
How to get continuous movement of rendered sprite image ( pygame )
我想 运行 只要按下一个键,应用程序就可以连续移动角色,此代码仅在按键时操纵渲染图像一次。
我试过 while 循环,希望在按下键时执行操作,但没有成功。程序 运行 一遍又一遍地循环有效地使它崩溃。我一直在思考这个问题,这简直让我发疯。
link 看看发生了什么: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
事件仅在按下某个键时发生一次,因此精灵不会连续移动。释放键后,您会收到一次 pygame.KEYUP
事件通知。您可以使用此事件来设置一个状态变量,指示是否按下了某个键。在 pygame.KEYDOWN
上设置变量并在 pygame.KEYUP
.
上重置变量
幸运的是 pygame 可以为您做到这一点。 pygame.key.get_pressed()
returns a list of bools, with the "pressed" state of all the keys. 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.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()
我想 运行 只要按下一个键,应用程序就可以连续移动角色,此代码仅在按键时操纵渲染图像一次。
我试过 while 循环,希望在按下键时执行操作,但没有成功。程序 运行 一遍又一遍地循环有效地使它崩溃。我一直在思考这个问题,这简直让我发疯。
link 看看发生了什么: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
事件仅在按下某个键时发生一次,因此精灵不会连续移动。释放键后,您会收到一次 pygame.KEYUP
事件通知。您可以使用此事件来设置一个状态变量,指示是否按下了某个键。在 pygame.KEYDOWN
上设置变量并在 pygame.KEYUP
.
上重置变量
幸运的是 pygame 可以为您做到这一点。 pygame.key.get_pressed()
returns a list of bools, with the "pressed" state of all the keys. 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.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()