python - pygame.transform.flip 不翻转也不给我一个错误?

标签 python pygame

所以我们接到了制作一个小型视频游戏的任务,到目前为止我已经有了玩家,背景,我可以跳跃和行走。 我的问题是,无论我朝哪个方向走,我都会得到相同的动画,我也尝试用 pygame.transform.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/

相关文章:

python - 如何在 Pygame 中将 'blit' 图像显示到屏幕上

基于Python图 block 的游戏(二维数组)玩家移动

python - Python 中的 Apache Avro 性能非常缓慢,编码为消息与文件时的结果不同

python - 在 Travis CI 中测试基于 matplotlib 的绘图

python - 基于 bool np.array 有效地对 np.matrix 进行子集化,但仅直到某个阈值

python - 如何抓取需要使用 Python 登录的网站

python - 在 Django 中,我如何返回与模型相关的项目总数?

python - python 中的鸡蛋和篮子游戏

python - Pygame - 按退格键更改 Sprite 颜色

python - 为什么 pygame 在退出之前需要 time.sleep 来播放声音?