Forum Archive

How to add border size, border colors in SpriteNodes?

ilikepython

Hello, this is my first post, and I am new to the scene library, I don’t know how to add border colors nor know how to change the border size and if it’s possible, can someone please tell me how?

JonB

I don't think sprite node had a good built in method for this. You could, in setup, draw your sprite into a ImageContext, and then a stroke a rect on top. Then pass the ctx.get_image() as the image to your sprite node.

cvp

@JonB I would prefer fill the ImageContext with a color, then draw the image in it... 😂

import ui
from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'

        ui_image = ui.Image.named('test:Mandrill')
        wi,hi = ui_image.size
        w = 100
        h = w * hi/wi
        bs = 10
        with ui.ImageContext(w,h) as ctx:
            pth= ui.Path.rect(0,0,w,h)
            ui.set_color('red')
            pth.fill()
            ui_image.draw(bs,bs,w-2*bs,h-2*bs)
            ui_image = ctx.get_image()
        texture = Texture(ui_image)             
        self.ship = SpriteNode(texture)


        self.ship.position = self.size / 2
        self.add_child(self.ship)

run(MyScene())

cvp

@ilikepython previous post also for you