omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Sprite animation from sprite sheet at 8 FPS?

    Pythonista
    sprite spritesheet game scene module animation
    4
    16
    7742
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Vile
      Vile @JonB last edited by

      @JonB, I’ve also never used Action.wait before, so I’d appreciate an example of this, thanks.

      Remember, I’m still a noob here lol

      1 Reply Last reply Reply Quote 0
      • pulbrich
        pulbrich last edited by

        @Vile :

        import time #this module is needed to get the system time
        

        In your set-up code:

        self.last_update = time.process_time()
        

        In your update method:

        current_time = time.process_time()
        if current_time - self.last_update > 0.125:
        	self.last_update = current_time
        	modify_the_sprite
        
        Vile 1 Reply Last reply Reply Quote 1
        • Vile
          Vile @pulbrich last edited by

          @pulbrich

          I tried your method, but it seems that anything I put into the update function doesn’t get used...

          Here’s the updated code:

          from scene import *
          import time
          
          sprite_sheet = [
          Texture('IMG_0227.png').subtexture(Rect(0,0,0.25,1)),
          Texture('IMG_0227.png').subtexture(Rect(0.25,0,0.25,1)),
          Texture('IMG_0227.png').subtexture(Rect(0.5,0,0.25,1)),
          Texture('IMG_0227.png').subtexture(Rect(0.75,0,.25,1)),
          ]
          
          class MyScene (Scene):
          	def setup(self):
          		self.screen="game"
          		self.background_color = 'black'
          		self.last_update = time.process_time()
          		self.sprite = SpriteNode(sprite_sheet[0],
          		scale = 1,
          		position = self.size / 2,
          		parent = self)
          		self.add_child(self.sprite)
          		n=0
          		
          		def update(self):
          			current_time = time.process_time()
          			if current_time - self.last_update > 0.125:
          				self.last_update = current_time
          				#modify_the_sprite
          				self.sprite.texture=sprite_sheet[n]
          				n=n+1
          run(MyScene())
          
          1 Reply Last reply Reply Quote 0
          • Vile
            Vile last edited by

            Wait it actually works, I just had the wrong indentation on the update function XD

            Here’s the code:

            from scene import *
            import time
            
            sprite_sheet = [
            Texture('IMG_0227.png').subtexture(Rect(0,0,0.25,1)),
            Texture('IMG_0227.png').subtexture(Rect(0.25,0,0.25,1)),
            Texture('IMG_0227.png').subtexture(Rect(0.5,0,0.25,1)),
            Texture('IMG_0227.png').subtexture(Rect(0.75,0,.25,1)),
            ]
            
            class MyScene (Scene):
            	def setup(self):
            		self.screen="game"
            		self.background_color = 'black'
            		self.last_update = time.process_time()
            		self.sprite = SpriteNode(sprite_sheet[0],
            		scale = 1,
            		position = self.size / 2,
            		parent = self)
            		self.add_child(self.sprite)
            		self.n=0
            		
            	def update(self):
            		#self.sprite.texture=sprite_sheet[1]
            		current_time = time.process_time()
            		if current_time - self.last_update > 0.05:
            			self.last_update = current_time
            			#modify_the_sprite
            			self.sprite.texture=sprite_sheet[self.n]
            			self.n=self.n+1
            			if self.n > 3:
            				self.n=0
            run(MyScene())
            

            Thanks a lot for your help!

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              dont use time.process_time. instead use scene.t -- scene includes t which is the time since the start of the scene, and dt, which is the time since last update.

              Vile 1 Reply Last reply Reply Quote 1
              • Vile
                Vile @JonB last edited by

                @JonB, I tried using scene.t and scene.dt, but I can’t get it working...

                1 Reply Last reply Reply Quote 0
                • pulbrich
                  pulbrich last edited by pulbrich

                  Instead of using the time function that I suggested, put Scene.t (mind the capital S)

                  CORRECTION: it is self.t (instance attribute), thanks @JonB

                  self.dt is not directly suitable for your case; it gives you the time lapsed since the last call to update().

                  1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB last edited by

                    I think it should be self.t.
                    it is an instance attribute, not a class attrib, if memory serves -- the time is incremented in the running scene only.

                    1 Reply Last reply Reply Quote 1
                    • cvp
                      cvp last edited by

                      Following @JonB advice, this works

                      .
                      .
                      .
                              self.last_t = self.t
                              
                          def update(self):
                              #self.sprite.texture=sprite_sheet[1]
                              if (self.t-self.last_t) > 0.05:
                                  #modify_the_sprite
                                  self.last_t = self.t
                      .
                      .
                      .
                      
                      1 Reply Last reply Reply Quote 1
                      • Vile
                        Vile last edited by

                        @JonB and @cvp, thanks once again! It seems to work in the way I expected now.

                        It seems that scene.t uses a different time frame from time.process_time, because with the same number value, the animations appear to go at different speeds.

                        cvp 1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @Vile last edited by

                          @Vile thank only @jonb 😀

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Powered by NodeBB Forums | Contributors