pygame.transform.flip既不翻转也不给我错误?

时间:2019-04-24 17:23:50

标签: python pygame

因此,我们已经派遣了一个制作小型视频游戏的人,到目前为止,我有一个有背景的玩家,我可以跳和走。 我的问题是,无论走什么方向,我都得到相同的动画,我正在尝试使用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()

预计我的玩家也可以转弯,这样我就可以双向走(我可以双向走,但我正向一个方向走)

1 个答案:

答案 0 :(得分:1)

永远不会调用函数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()