2
\$\begingroup\$

I'm drawing a background into kivy, and then wrap and place a transparent image on top of it. Each second I move the location of the transparent a little bit.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
from kivy.clock import Clock

class ScrollApp(App):

    def build(self):
        self.bkg = Background()
        self.clock = Clock.schedule_interval(self.bkg.scroll_texture, 1)
        return self.bkg


class Background(Widget):

    plx_2 = ObjectProperty(None)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # set as texture
        self.plx_2 = Image(source=r"assets\plx-2.2.png").texture
        self.plx_2.wrap = 'repeat'
        self.plx_2.uvsize = (1,-1)

    def scroll_texture(self, time_passed):
        # update uvpos
        x,y = self.plx_2.uvpos
        x = x - 0.05 % 1 # this magic number will change later
        self.plx_2.uvpos = (x, y)
        # redraw the image    
        texture = self.property('plx_2')
        texture.dispatch(self)


if __name__=='__main__':
    ScrollApp().run()

And the .kv file:

#:kivy 1.11.1

<Background>:    
    canvas.before:
        Rectangle:
            size : self.size
            pos : self.pos
            source: "assets\plx-1.png"
        Rectangle:
            size : self.size   
            texture: self.plx_2

The code works, but I feel very odd about the way I'm redrawing the texture with dispatching itself as an event. Is there a better way? Second, now if I were to add multiple transparent images on top, I would have to add a lot of code. Is there a a better way than just adding another Rectangle in the .kv file, adding another ObjectProperty in the Background class etc, etc.?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The only really odd thing I see is this:

x - 0.05 % 1

Modulation takes precedence over subtraction, and 0.05 % 1 == 0.05, so the modulation has no effect. Is this what you intended?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I intended it to constrain x between 0 and 1, by only giving me the remainder. It should use brackets to first execute the x-0.05. Thanks for the catch! \$\endgroup\$ Commented Apr 28, 2020 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.