How can I fix my sprite animation in pygame?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How can I remove a trailing newline in Python?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How do I list all files of a directory?pygame spritesheet sprite animation

How can Iron Man's suit withstand this?

You've spoiled/damaged the card

Metal bar on DMM PCB

Working in the USA for living expenses only; allowed on VWP?

Why don't B747s start takeoffs with full throttle?

Java 8: How to convert String to Map<String,List<String>>?

Applicants clearly not having the skills they advertise

Is it legal in the UK for politicians to lie to the public for political gain?

Does the growth of home value benefit from compound interest?

Humans meet a distant alien species. How do they standardize? - Units of Measure

How were concentration and extermination camp guards recruited?

Why is Colorado so different politically from nearby states?

PhD student with mental health issues and bad performance

Opposite of "Squeaky wheel gets the grease"

X-shaped crossword

Company is asking me to work from overseas, but wants me to take a paycut

Old black and white movie: glowing black rocks slowly turn you into stone upon touch

Word for a small burst of laughter that can't be held back

What are they doing to this poor rocket?

Accidentally renamed tar.gz file to a non tar.gz file, will my file be messed up

Explain Ant-Man's "not it" scene from Avengers: Endgame

Shrink exponential fraction argument

Responsibility for visa checking

correct term describing the action of sending a brand-new ship out into its first seafaring trip



How can I fix my sprite animation in pygame?


How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How can I remove a trailing newline in Python?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How do I list all files of a directory?pygame spritesheet sprite animation






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm coding a game in python 3 (with pygame) for a school project, but i'm struggeling with something:
I'm trying to get my sprite to have an animation when moving, but it only shows a static image:
when i press the left key, the sprite's image changes to a static image instead of an animation made of 3 png's.
Here is that part of the code:



clock = pygame.time.Clock()

walkRight = [pygame.image.load('ressources/images/R1.png'), pygame.image.load('ressources/images/R2.png'), pygame.image.load('ressources/images/R3.png')]
walkLeft = [pygame.image.load('ressources/images/L1.png'), pygame.image.load('ressources/images/L2.png'), pygame.image.load('ressources/images/L3.png')]
walkUp = [pygame.image.load('ressources/images/U1.png'), pygame.image.load('ressources/images/U2.png'), pygame.image.load('ressources/images/U3.png')]
walkDown = [pygame.image.load('ressources/images/D1.png'), pygame.image.load('ressources/images/D2.png'), pygame.image.load('ressources/images/D3.png')]


#this is the player sprite

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = (walkDown[4//3])
self.rect = self.image.get_rect()
self.rect.centerx = width / 2
self.rect.bottom = height / 2
self.speedx = 0
self.speedy = 0
self.velocity = 2


def update(self):
self.speedx = 0
self.speedy = 0
left = False
right = False
up = False
down = False
run = False
walkCount = 4
runCount = 4
clock.tick(FPS)
keys = pygame.key.get_pressed()

#I believe this is the part that has something wrong!
if walkCount + 1 >= 9:
walkCount = 0
if (down == True and run == False):
walkCount += 1
self.image = (walkDown[walkCount//3])
elif (up == True and run == False):
walkCount += 1
self.image = (walkUp[walkCount//3])
elif (right == True and run == False):
walkCount += 1
self.image = (walkRight[walkCount//3])
elif (left == True and run == False):
walkCount += 1
self.image = (walkLeft[walkCount//3])









share|improve this question




























    1















    I'm coding a game in python 3 (with pygame) for a school project, but i'm struggeling with something:
    I'm trying to get my sprite to have an animation when moving, but it only shows a static image:
    when i press the left key, the sprite's image changes to a static image instead of an animation made of 3 png's.
    Here is that part of the code:



    clock = pygame.time.Clock()

    walkRight = [pygame.image.load('ressources/images/R1.png'), pygame.image.load('ressources/images/R2.png'), pygame.image.load('ressources/images/R3.png')]
    walkLeft = [pygame.image.load('ressources/images/L1.png'), pygame.image.load('ressources/images/L2.png'), pygame.image.load('ressources/images/L3.png')]
    walkUp = [pygame.image.load('ressources/images/U1.png'), pygame.image.load('ressources/images/U2.png'), pygame.image.load('ressources/images/U3.png')]
    walkDown = [pygame.image.load('ressources/images/D1.png'), pygame.image.load('ressources/images/D2.png'), pygame.image.load('ressources/images/D3.png')]


    #this is the player sprite

    class Player(pygame.sprite.Sprite):
    def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = (walkDown[4//3])
    self.rect = self.image.get_rect()
    self.rect.centerx = width / 2
    self.rect.bottom = height / 2
    self.speedx = 0
    self.speedy = 0
    self.velocity = 2


    def update(self):
    self.speedx = 0
    self.speedy = 0
    left = False
    right = False
    up = False
    down = False
    run = False
    walkCount = 4
    runCount = 4
    clock.tick(FPS)
    keys = pygame.key.get_pressed()

    #I believe this is the part that has something wrong!
    if walkCount + 1 >= 9:
    walkCount = 0
    if (down == True and run == False):
    walkCount += 1
    self.image = (walkDown[walkCount//3])
    elif (up == True and run == False):
    walkCount += 1
    self.image = (walkUp[walkCount//3])
    elif (right == True and run == False):
    walkCount += 1
    self.image = (walkRight[walkCount//3])
    elif (left == True and run == False):
    walkCount += 1
    self.image = (walkLeft[walkCount//3])









    share|improve this question
























      1












      1








      1


      1






      I'm coding a game in python 3 (with pygame) for a school project, but i'm struggeling with something:
      I'm trying to get my sprite to have an animation when moving, but it only shows a static image:
      when i press the left key, the sprite's image changes to a static image instead of an animation made of 3 png's.
      Here is that part of the code:



      clock = pygame.time.Clock()

      walkRight = [pygame.image.load('ressources/images/R1.png'), pygame.image.load('ressources/images/R2.png'), pygame.image.load('ressources/images/R3.png')]
      walkLeft = [pygame.image.load('ressources/images/L1.png'), pygame.image.load('ressources/images/L2.png'), pygame.image.load('ressources/images/L3.png')]
      walkUp = [pygame.image.load('ressources/images/U1.png'), pygame.image.load('ressources/images/U2.png'), pygame.image.load('ressources/images/U3.png')]
      walkDown = [pygame.image.load('ressources/images/D1.png'), pygame.image.load('ressources/images/D2.png'), pygame.image.load('ressources/images/D3.png')]


      #this is the player sprite

      class Player(pygame.sprite.Sprite):
      def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.image = (walkDown[4//3])
      self.rect = self.image.get_rect()
      self.rect.centerx = width / 2
      self.rect.bottom = height / 2
      self.speedx = 0
      self.speedy = 0
      self.velocity = 2


      def update(self):
      self.speedx = 0
      self.speedy = 0
      left = False
      right = False
      up = False
      down = False
      run = False
      walkCount = 4
      runCount = 4
      clock.tick(FPS)
      keys = pygame.key.get_pressed()

      #I believe this is the part that has something wrong!
      if walkCount + 1 >= 9:
      walkCount = 0
      if (down == True and run == False):
      walkCount += 1
      self.image = (walkDown[walkCount//3])
      elif (up == True and run == False):
      walkCount += 1
      self.image = (walkUp[walkCount//3])
      elif (right == True and run == False):
      walkCount += 1
      self.image = (walkRight[walkCount//3])
      elif (left == True and run == False):
      walkCount += 1
      self.image = (walkLeft[walkCount//3])









      share|improve this question














      I'm coding a game in python 3 (with pygame) for a school project, but i'm struggeling with something:
      I'm trying to get my sprite to have an animation when moving, but it only shows a static image:
      when i press the left key, the sprite's image changes to a static image instead of an animation made of 3 png's.
      Here is that part of the code:



      clock = pygame.time.Clock()

      walkRight = [pygame.image.load('ressources/images/R1.png'), pygame.image.load('ressources/images/R2.png'), pygame.image.load('ressources/images/R3.png')]
      walkLeft = [pygame.image.load('ressources/images/L1.png'), pygame.image.load('ressources/images/L2.png'), pygame.image.load('ressources/images/L3.png')]
      walkUp = [pygame.image.load('ressources/images/U1.png'), pygame.image.load('ressources/images/U2.png'), pygame.image.load('ressources/images/U3.png')]
      walkDown = [pygame.image.load('ressources/images/D1.png'), pygame.image.load('ressources/images/D2.png'), pygame.image.load('ressources/images/D3.png')]


      #this is the player sprite

      class Player(pygame.sprite.Sprite):
      def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.image = (walkDown[4//3])
      self.rect = self.image.get_rect()
      self.rect.centerx = width / 2
      self.rect.bottom = height / 2
      self.speedx = 0
      self.speedy = 0
      self.velocity = 2


      def update(self):
      self.speedx = 0
      self.speedy = 0
      left = False
      right = False
      up = False
      down = False
      run = False
      walkCount = 4
      runCount = 4
      clock.tick(FPS)
      keys = pygame.key.get_pressed()

      #I believe this is the part that has something wrong!
      if walkCount + 1 >= 9:
      walkCount = 0
      if (down == True and run == False):
      walkCount += 1
      self.image = (walkDown[walkCount//3])
      elif (up == True and run == False):
      walkCount += 1
      self.image = (walkUp[walkCount//3])
      elif (right == True and run == False):
      walkCount += 1
      self.image = (walkRight[walkCount//3])
      elif (left == True and run == False):
      walkCount += 1
      self.image = (walkLeft[walkCount//3])






      python pygame sprite






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 13:18









      BeamerBoy725BeamerBoy725

      84




      84






















          1 Answer
          1






          active

          oldest

          votes


















          0














          walkCount is a local variable in the method update. It is initialized by 4 every time when update is called. So the value of walkCount is the same at the start of the function, every time. This causes that the image seems to be static.
          walkCount has to be an attribute of the class Player rather than a local variable:



          class Player(pygame.sprite.Sprite):
          def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image = (walkDown[4//3])
          self.rect = self.image.get_rect()
          self.rect.centerx = width / 2
          self.rect.bottom = height / 2
          self.speedx = 0
          self.speedy = 0
          self.velocity = 2

          self.walkCount = 4 # <---- add attribute

          def update(self):

          # [...]

          # walkCount = 4 <------ delete local variable

          keys = pygame.key.get_pressed()
          down = keys[pygame.K_DOWN]
          up = keys[pygame.K_UP]
          right = keys[pygame.K_RIGHT]
          left = keys[pygame.K_LEFT]
          run = False

          if self.walkCount + 1 >= 9:
          self.walkCount = 0
          if (down == True and run == False):
          self.walkCount += 1
          self.image = (walkDown[self.walkCount//3])
          elif (up == True and run == False):
          self.walkCount += 1
          self.image = (walkUp[self.walkCount//3])
          elif (right == True and run == False):
          self.walkCount += 1
          self.image = (walkRight[self.walkCount//3])
          elif (left == True and run == False):
          self.walkCount += 1
          self.image = (walkLeft[self.walkCount//3])





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324207%2fhow-can-i-fix-my-sprite-animation-in-pygame%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            walkCount is a local variable in the method update. It is initialized by 4 every time when update is called. So the value of walkCount is the same at the start of the function, every time. This causes that the image seems to be static.
            walkCount has to be an attribute of the class Player rather than a local variable:



            class Player(pygame.sprite.Sprite):
            def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = (walkDown[4//3])
            self.rect = self.image.get_rect()
            self.rect.centerx = width / 2
            self.rect.bottom = height / 2
            self.speedx = 0
            self.speedy = 0
            self.velocity = 2

            self.walkCount = 4 # <---- add attribute

            def update(self):

            # [...]

            # walkCount = 4 <------ delete local variable

            keys = pygame.key.get_pressed()
            down = keys[pygame.K_DOWN]
            up = keys[pygame.K_UP]
            right = keys[pygame.K_RIGHT]
            left = keys[pygame.K_LEFT]
            run = False

            if self.walkCount + 1 >= 9:
            self.walkCount = 0
            if (down == True and run == False):
            self.walkCount += 1
            self.image = (walkDown[self.walkCount//3])
            elif (up == True and run == False):
            self.walkCount += 1
            self.image = (walkUp[self.walkCount//3])
            elif (right == True and run == False):
            self.walkCount += 1
            self.image = (walkRight[self.walkCount//3])
            elif (left == True and run == False):
            self.walkCount += 1
            self.image = (walkLeft[self.walkCount//3])





            share|improve this answer



























              0














              walkCount is a local variable in the method update. It is initialized by 4 every time when update is called. So the value of walkCount is the same at the start of the function, every time. This causes that the image seems to be static.
              walkCount has to be an attribute of the class Player rather than a local variable:



              class Player(pygame.sprite.Sprite):
              def __init__(self):
              pygame.sprite.Sprite.__init__(self)
              self.image = (walkDown[4//3])
              self.rect = self.image.get_rect()
              self.rect.centerx = width / 2
              self.rect.bottom = height / 2
              self.speedx = 0
              self.speedy = 0
              self.velocity = 2

              self.walkCount = 4 # <---- add attribute

              def update(self):

              # [...]

              # walkCount = 4 <------ delete local variable

              keys = pygame.key.get_pressed()
              down = keys[pygame.K_DOWN]
              up = keys[pygame.K_UP]
              right = keys[pygame.K_RIGHT]
              left = keys[pygame.K_LEFT]
              run = False

              if self.walkCount + 1 >= 9:
              self.walkCount = 0
              if (down == True and run == False):
              self.walkCount += 1
              self.image = (walkDown[self.walkCount//3])
              elif (up == True and run == False):
              self.walkCount += 1
              self.image = (walkUp[self.walkCount//3])
              elif (right == True and run == False):
              self.walkCount += 1
              self.image = (walkRight[self.walkCount//3])
              elif (left == True and run == False):
              self.walkCount += 1
              self.image = (walkLeft[self.walkCount//3])





              share|improve this answer

























                0












                0








                0







                walkCount is a local variable in the method update. It is initialized by 4 every time when update is called. So the value of walkCount is the same at the start of the function, every time. This causes that the image seems to be static.
                walkCount has to be an attribute of the class Player rather than a local variable:



                class Player(pygame.sprite.Sprite):
                def __init__(self):
                pygame.sprite.Sprite.__init__(self)
                self.image = (walkDown[4//3])
                self.rect = self.image.get_rect()
                self.rect.centerx = width / 2
                self.rect.bottom = height / 2
                self.speedx = 0
                self.speedy = 0
                self.velocity = 2

                self.walkCount = 4 # <---- add attribute

                def update(self):

                # [...]

                # walkCount = 4 <------ delete local variable

                keys = pygame.key.get_pressed()
                down = keys[pygame.K_DOWN]
                up = keys[pygame.K_UP]
                right = keys[pygame.K_RIGHT]
                left = keys[pygame.K_LEFT]
                run = False

                if self.walkCount + 1 >= 9:
                self.walkCount = 0
                if (down == True and run == False):
                self.walkCount += 1
                self.image = (walkDown[self.walkCount//3])
                elif (up == True and run == False):
                self.walkCount += 1
                self.image = (walkUp[self.walkCount//3])
                elif (right == True and run == False):
                self.walkCount += 1
                self.image = (walkRight[self.walkCount//3])
                elif (left == True and run == False):
                self.walkCount += 1
                self.image = (walkLeft[self.walkCount//3])





                share|improve this answer













                walkCount is a local variable in the method update. It is initialized by 4 every time when update is called. So the value of walkCount is the same at the start of the function, every time. This causes that the image seems to be static.
                walkCount has to be an attribute of the class Player rather than a local variable:



                class Player(pygame.sprite.Sprite):
                def __init__(self):
                pygame.sprite.Sprite.__init__(self)
                self.image = (walkDown[4//3])
                self.rect = self.image.get_rect()
                self.rect.centerx = width / 2
                self.rect.bottom = height / 2
                self.speedx = 0
                self.speedy = 0
                self.velocity = 2

                self.walkCount = 4 # <---- add attribute

                def update(self):

                # [...]

                # walkCount = 4 <------ delete local variable

                keys = pygame.key.get_pressed()
                down = keys[pygame.K_DOWN]
                up = keys[pygame.K_UP]
                right = keys[pygame.K_RIGHT]
                left = keys[pygame.K_LEFT]
                run = False

                if self.walkCount + 1 >= 9:
                self.walkCount = 0
                if (down == True and run == False):
                self.walkCount += 1
                self.image = (walkDown[self.walkCount//3])
                elif (up == True and run == False):
                self.walkCount += 1
                self.image = (walkUp[self.walkCount//3])
                elif (right == True and run == False):
                self.walkCount += 1
                self.image = (walkRight[self.walkCount//3])
                elif (left == True and run == False):
                self.walkCount += 1
                self.image = (walkLeft[self.walkCount//3])






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 13:42









                Rabbid76Rabbid76

                48.9k123557




                48.9k123557





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324207%2fhow-can-i-fix-my-sprite-animation-in-pygame%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript