Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9292971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:00:58+00:00 2026-06-18T21:00:58+00:00

I’m having trouble flipping a sprite in pygame (I can get it to go

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:00:59+00:00Added an answer on June 18, 2026 at 9:00 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I want to show the soap response to UIWebview.. my soap response is, <p><img
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.