试图让我的播放器达到 transfrom.flip python
Trying to get my player to transfrom.flip python
我正在努力 pygame。当我分别按向右或向左键时,我希望我的播放器移动并面向右或向左。相反,它只是翻转。不知道怎么让它指向左边,或者指向右边然后走路。
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
swordsman = pg.transform.flip(swordsman, True, False)
x += speed
if event.key == pg.K_LEFT:
swordsman = pg.transform.flip(swordsman, True, False)
x -= speed
swordsman
是 pygame.Sprite
对象吗?如果是,它本身应该包含一个 pygame.Rect
对象。变量x
只是一个单独的变量,它的变化对你的swordsman
没有影响。尝试 swordsman.rect.x = x
您可以使用 pg.key.get_pressed()
检查按键状态,看看 ← 或 → 是否被按下(如下例所示)或者,在事件循环中将 speed
变量更改为另一个值,然后在 while 循环中的每一帧中更改 x
位置(x += speed
)事件循环(释放按键时将速度重置为 0)。
关于图像翻转,保留对原始图像的引用并将其分配给 swordsman
变量。当玩家想要向另一个方向移动时,翻转原图并赋值(如果他们向原方向移动,只需赋值原图)。
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
# This is the original swordsman image/surface (just a
# rectangular surface with a dark topleft corner).
SWORDSMAN_ORIGINAL = pg.Surface((30, 50))
SWORDSMAN_ORIGINAL.fill((50, 140, 200))
pg.draw.rect(SWORDSMAN_ORIGINAL, (10, 50, 90), (0, 0, 13, 13))
# Assign the current swordsman surface to another variable.
swordsman = SWORDSMAN_ORIGINAL
x, y = 300, 200
speed = 4
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
# When the player presses right, flip the original
# and assign it to the current swordsman variable.
swordsman = pg.transform.flip(SWORDSMAN_ORIGINAL, True, False)
elif event.key == pg.K_LEFT:
# Here you can just assign the original.
swordsman = SWORDSMAN_ORIGINAL
# Check the state of the keys each frame.
keys = pg.key.get_pressed()
# Move if the left or right keys are held.
if keys[pg.K_LEFT]:
x -= speed
elif keys[pg.K_RIGHT]:
x += speed
screen.fill(BG_COLOR)
screen.blit(swordsman, (x, y))
pg.display.flip()
clock.tick(60)
pg.quit()
我正在努力 pygame。当我分别按向右或向左键时,我希望我的播放器移动并面向右或向左。相反,它只是翻转。不知道怎么让它指向左边,或者指向右边然后走路。
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
swordsman = pg.transform.flip(swordsman, True, False)
x += speed
if event.key == pg.K_LEFT:
swordsman = pg.transform.flip(swordsman, True, False)
x -= speed
swordsman
是 pygame.Sprite
对象吗?如果是,它本身应该包含一个 pygame.Rect
对象。变量x
只是一个单独的变量,它的变化对你的swordsman
没有影响。尝试 swordsman.rect.x = x
您可以使用 pg.key.get_pressed()
检查按键状态,看看 ← 或 → 是否被按下(如下例所示)或者,在事件循环中将 speed
变量更改为另一个值,然后在 while 循环中的每一帧中更改 x
位置(x += speed
)事件循环(释放按键时将速度重置为 0)。
关于图像翻转,保留对原始图像的引用并将其分配给 swordsman
变量。当玩家想要向另一个方向移动时,翻转原图并赋值(如果他们向原方向移动,只需赋值原图)。
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
# This is the original swordsman image/surface (just a
# rectangular surface with a dark topleft corner).
SWORDSMAN_ORIGINAL = pg.Surface((30, 50))
SWORDSMAN_ORIGINAL.fill((50, 140, 200))
pg.draw.rect(SWORDSMAN_ORIGINAL, (10, 50, 90), (0, 0, 13, 13))
# Assign the current swordsman surface to another variable.
swordsman = SWORDSMAN_ORIGINAL
x, y = 300, 200
speed = 4
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
# When the player presses right, flip the original
# and assign it to the current swordsman variable.
swordsman = pg.transform.flip(SWORDSMAN_ORIGINAL, True, False)
elif event.key == pg.K_LEFT:
# Here you can just assign the original.
swordsman = SWORDSMAN_ORIGINAL
# Check the state of the keys each frame.
keys = pg.key.get_pressed()
# Move if the left or right keys are held.
if keys[pg.K_LEFT]:
x -= speed
elif keys[pg.K_RIGHT]:
x += speed
screen.fill(BG_COLOR)
screen.blit(swordsman, (x, y))
pg.display.flip()
clock.tick(60)
pg.quit()