kvc1992
kvc1992

Reputation: 11

pygame.transform.flip is not flipping neither giving me an error?

So we have gotten an asignment to make a small video game, so far i have the player, a background, i can jump and walk. My problem is that no mater what direction i walk, i get the same animation, i am trying too flip the animation with pygame.transform.flip but cant get it too work, i dont get an error or nothing. But my player charecther still monwalks backwards..

i have tried too use the flip command before i am blitting onto the screen, and have now tried too move the whole animation into a function, both things give me the same result.

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()

Expected my player charecther too turn around so that i could walk both ways (I can walk both ways, but i am moonwalking one direction)

Upvotes: 1

Views: 259

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

The function pg.transform.flip is never called.
pygame.event is a class and not an event type. Apart from that the 1st parameter to pg.transform.flip has to be a pygame.Surface object rather than a pygame.Rect object.

Flip the surface dependent on the state of the variable flip (in global scope):

def Animation():
    surf = walkList[AnimationCounter]
    if flip:
        surf = pg.transform.flip(surf,False,True)
    direction = win.blit(surf, (x, y))

Chang the state of flip dependent on the key events:

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()

Upvotes: 1

Related Questions