Forum Archive

Centering shape node scene

resserone13

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

@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

@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

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

resserone13

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


screen_size = (414.00, 896.00)
center_screen = (207, 448)

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

@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

@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

@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

@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.