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):
        # 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')

if __name__=='__main__':

And the .kv file:

#:kivy 1.11.1

            size : self.size
            pos : self.pos
            source: "assets\plx-1.png"
            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.?


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?

  • 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\$ – Mitchell van Zuylen Apr 28 '20 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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