因此,我们已经分配去制作一个小型视频游戏,到目前为止,我已经拥有播放器,背景知识,我可以跳跃和行走。
我的问题是,无论走什么方向,我都得到相同的动画,我正在尝试使用pygame.transform.flip翻转动画,但无法使其正常工作,我没有收到错误或什么也没有。但是我的玩家charecther仍然向后走。
我试过在屏幕上滑动之前也使用了flip命令,现在又尝试将整个动画移到一个函数中,两者都给了我相同的结果。
import pygame as pg
pg.init()
winHeight=600
winWidth = 1020
x=10
y=560
size = 37
bg = pg.image.load("scenes/Background/bg.png")
walkList = [pg.image.load("player/Run/run-00.png"),pg.image.load("player/Run/run-01.png"),pg.image.load("player/Run/run-02.png"),pg.image.load("player/Run/run-03.png"),pg.image.load("player/Run/run-04.png"),pg.image.load("player/Run/run-05.png")]
char = pg.image.load("player/idle/idle-00.png")
vel = 3
win = pg.display.set_mode((winWidth,winHeight))
pg.display.set_caption("SMAS 'EM By IKEA KID")
run = True
jumCount = 7
isJump = False
AnimationCounter = 0
def Animation():
direction = win.blit(walkList[AnimationCounter], (x, y))
if pg.event == pg.K_LEFT:
return direction
elif pg.event == pg.K_RIGHT:
direction=pg.transform.flip(direction,False,True)
return direction
def RedrawWindow():
global AnimationCounter
win.blit(bg, (0,0))
if AnimationCounter +1 >= 6:
AnimationCounter = 0
if walk:
Animation()
AnimationCounter += 1
elif not walk:
win.blit(char,(x,y))
pg.display.update()
while run:
pg.time.delay(15)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] and x > vel:
x -= vel
walk = True
elif keys[pg.K_RIGHT] and x+size < winWidth-vel:
x += vel
walk = True
flip = True
else:
AnimationCounter = 0
walk = False
if not(isJump):
if keys[pg.K_SPACE]:
isJump = True
else:
if jumCount >= -7:
neg = 1
if jumCount < 0:
neg = -1
y -= (jumCount**2)*0.5*neg
jumCount-=0.5
else:
isJump = False
jumCount = 7
RedrawWindow()
期望我的玩家角色也转身,以便我可以双向行走(我可以双向行走,但我正向一个方向行走)
最佳答案
永远不会调用函数pg.transform.flip
。pygame.event
是类,而不是事件类型。除此之外,pg.transform.flip
的第一个参数必须是pygame.Surface
对象,而不是pygame.Rect
对象。
根据变量flip
的状态翻转表面(在全局范围内):
def Animation():
surf = walkList[AnimationCounter]
if flip:
surf = pg.transform.flip(surf,False,True)
direction = win.blit(surf, (x, y))
根据关键事件更改
flip
的状态:flip = False
while run:
pg.time.delay(15)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] and x > vel:
x -= vel
walk = True
flip = False
elif keys[pg.K_RIGHT] and x+size < winWidth-vel:
x += vel
walk = True
flip = True
else:
AnimationCounter = 0
walk = False
# [...]
RedrawWindow()
关于python - pygame.transform.flip既不翻转也不给我一个错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55835485/