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.


    Centering shape node scene

    Pythonista
    shapenode scene
    3
    10
    2705
    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.
    • resserone13
      resserone13 last edited by

      Having trouble center ing a simple rectangle on the screen

      
      from scene import *
      import sound
      import random
      import math
      A = Action
      
      #screen_size = (414.00, 896.00)
      #center_screen = (207, 448)
      
      class MyScene (Scene):
      	def setup(self):
      		self.background_color = 'black'
      		self.green_mat = ShapeNode(ui.Path.rounded_rect(207, 448, 55, 55, 2))
      		self.green_mat.color = 'green'
      		#self.green_mat.anchor_point = (0,0)
      		self.green_mat.position = (0,0)
      		#self.green_mat.
      		self.add_child(self.green_mat)
      		
      		
      	def did_change_size(self):
      		pass
      	
      	def update(self):
      		pass
      	
      	def touch_began(self, touch):
      		pass
      	
      	def touch_moved(self, touch):
      		pass 
      	
      	def touch_ended(self, touch):
      		pass
      
      if __name__ == '__main__':
      	run(MyScene(), show_fps=False)
      
      
      mikael 1 Reply Last reply Reply Quote 0
      • mikael
        mikael @resserone13 last edited by

        @resserone13, if I understand your issue correctly, this example from the docs seems relevant:

        from scene import *
        
        class MyScene (Scene):
            def setup(self):
                self.background_color = 'midnightblue'
                self.ship = SpriteNode('spc:PlayerShip1Orange')
                self.ship.position = self.size / 2
                self.add_child(self.ship)
        
        run(MyScene())
        
        resserone13 1 Reply Last reply Reply Quote 0
        • resserone13
          resserone13 @mikael last edited by

          @mikael said:

          self.ship.position = self.size / 2

          That worked. I’ve seen this but I don’t understand what’s going on or why it works. I understand it’s Size divided by 2... but I’m not sure what’s going on here. Thanks

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

            @resserone13 position = center of your shape, not lower left point

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

              @cvp I think I understand. Is it similar to this?

              
              screen_size = (414.00, 896.00)
              center_screen = (207, 448)
              
              
              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @resserone13 last edited by cvp

                @resserone13 yes, that's why @mikael writes

                self.ship.position = self.size / 2
                

                to locate your ship at the center of the screen

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

                  @cvp ok thanks. I actually was close to figuring it out the. I tried the center_screen = (207, 448) for the x and y

                  mikael 1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @resserone13 last edited by

                    @resserone13, self.size gives us a scene.Size object, which is a subclass of scene.Vector2, which supports elementwise division. Thus same as:

                    ship.position = self.size.w/2, self.size.h/2
                    
                    resserone13 1 Reply Last reply Reply Quote 0
                    • resserone13
                      resserone13 @mikael last edited by resserone13

                      @mikael ok. I completely understand now. What I wasn’t catching was that self.size gives you the size of the scene. I was wondering but how do they know what size it is???? What are they dividing it in half ??? Lol. I see now That I can do . size To get the size of scenes

                      mikael 1 Reply Last reply Reply Quote 0
                      • mikael
                        mikael @resserone13 last edited by

                        @resserone13, it is a feature of scene that in the setup method we already know the size. Very convenient when compared to the ui module.

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