MRG123
MRG123

Reputation: 113

How to flip an img in Pygame, having trouble in my code?

I'm having trouble flipping a sprite in pygame (I can get it to go right but I want the img to flip on the left key),
I researched how to flip an image and found the pygame.transform.flip, but as a beginner to pygame, I'm not sure how to use it, and the tutorials aren't making sense to me.
Can anyone help me with the code below (I'm not sure if I even put the self.img1 for the flip in the right place)?

import pygame, sys, glob 
from pygame import *

class Player:
    def __init__(self):      
        self.x = 200     
        self.y = 300   
        self.ani_speed_init = 6
        self.ani_speed = self.ani_speed_init 
        self.ani = glob.glob("walk\Pinks_w*.png")
        self.ani.sort() 
        self.ani_pos = 0
        self.ani_max = len(self.ani)-1
        self.img = pygame.image.load(self.ani[0])
        self.img1 = pygame.transform.flip(self.ani[0], True, False)

        self.update(0)

    def update(self, pos):
        if pos != 0: 
            self.ani_speed-=1
            self.x+=pos
            if self.ani_speed == 0:
                self.img = pygame.image.load(self.ani[self.ani_pos])
                self.ani_speed = self.ani_speed_init
                if self.ani_pos == self.ani_max:
                    self.ani_pos = 0
                else:
                    self.ani_pos+=1
        screen.blit(self.img,(self.x,self.y))

h = 400
w = 800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
player1 = Player()
pos = 0

while True:
    screen.fill((0,0,0))
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            pos = 1
        elif event.type == KEYUP and event.key == K_RIGHT:
            pos = 0
        elif event.type == KEYDOWN and event.key == K_LEFT:
            pos = -1
        elif event.type == KEYUP and event.key == K_LEFT:
            pos = 0

    player1.update(pos)

    pygame.display.update()

Upvotes: 1

Views: 6010

Answers (2)

ninMonkey
ninMonkey

Reputation: 7501

To flip, you would do:

# input
if event.type == KEYDOWN:
    if event.key == K_RIGHT:
        flip_x = True
    elif event.key == K_LEFT:
        flip_x = False
    elif event.key == K_UP:
        flip_y = True
    elif event.key == K_DOWN:
        flip_y = False

# then to flip
new_image = pygame.transform.flip(original_image, flip_x, flip_y)

Upvotes: 2

pradyunsg
pradyunsg

Reputation: 19406

Your Player class is not very well readable. As, your names are not easy to understand.
In my version of your code, all I have changed is the names and I have added a check for the value of pos and applied the flip if needed. So, you may need to change the check (if condition), to get the desired results.

And Yes, you don't have a pygame.Sprite, so, the word sprite is misleading, in this case.

My version of your Player class:

class Player:
    def __init__(self):      
        self.x = 200  
        self.y = 300   
        self.speed_init = 6
        self.images = [pygame.image.load(img) for img in glob.glob("walk\Pinks_w*.png")]
        self.index = 0
        self.max_index = len(self.images)-1
        self.speed = 0
        self.img = self.images[self.index]

    def update(self, pos):
        if pos != 0:
            self.speed -= 1
            self.x += pos
            if not self.speed:
                self.speed = self.speed_init
                if self.index < self.max_index:
                    self.index += 1
                else:
                    self.index = 0
            self.img = self.images[self.index]
        # change True to False if needed, or change the operator.
        if pos > 0:
            self.img = pygame.transform.flip(self.img,True,False)
        screen.blit(self.img,(self.x,self.y))

Update
There was a small problem with the update function. The problem was that since speed was always constant and not 0, the if not self.speed: did not work. So, change the update function to this:

def update(self, pos):
    if pos != 0:
        self.speed -= 1
        self.x += pos
        # no more self.speed checks
        if self.index < self.max_index:
            self.index += 1
        else:
            self.index = 0
        self.img = self.images[self.index]
    # change True to False if needed, or change the operator.
    if pos < 0:
        self.img = pygame.transform.flip(self.img,True,False)
    screen.blit(self.img,(self.x,self.y))

Update 2
It seems that there is some kind of typo in your code,
Here's (my version of) the Code, The whole thing.

import pygame, sys, glob 
from pygame import *

class Player:
    def __init__(self):      
        self.x = 200  
        self.y = 300   
        self.speed_init = 6
        self.images = [pygame.image.load(img) for img in glob.glob("walk\Pinks_w*.png")]
        self.index = 0
        self.max_index = len(self.images)-1
        self.speed = 0
        self.img = self.images[self.index]
        print self.max_index

    def update(self, pos):
        if pos != 0:
            self.speed -= 1
            self.x += pos
            print self.index, self.max_index
            if self.index < self.max_index:
                self.index += 1
            else:
                self.index = 0
            self.img = self.images[self.index]
        # change True to False if needed, or change the operator.
        if pos < 0:
            self.img = pygame.transform.flip(self.img,True,False)
        screen.blit(self.img,(self.x,self.y))

h = 400
w = 800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
player1 = Player()
pos = 0

while True:
    screen.fill((0,0,0))
    clock.tick(20)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            pos = 2
        elif event.type == KEYUP and event.key == K_RIGHT:
            pos = 0
        elif event.type == KEYDOWN and event.key == K_LEFT:
            pos = -2
        elif event.type == KEYUP and event.key == K_LEFT:
            pos = 0

    player1.update(pos)

    pygame.display.update()

Upvotes: 1

Related Questions