Created
October 10, 2012 22:37
-
-
Save tshirtman/3868962 to your computer and use it in GitHub Desktop.
This gist show how to build a scrolling and repeating background with kivy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.app import App | |
from kivy.uix.widget import Widget | |
from kivy.clock import Clock | |
from kivy.graphics import Rectangle | |
from kivy.core.image import Image as CoreImage | |
from kivy.core.window import Window | |
class MyWidget(Widget): | |
def __init__(self, **kw): | |
super(MyWidget, self).__init__(**kw) | |
with self.canvas: | |
texture = CoreImage('Sky_back_layer.png').texture | |
texture.wrap = 'repeat' | |
self.rect_1 = Rectangle(texture=texture, size=self.size, pos=self.pos) | |
texture = CoreImage('Vegetation_(middle_layer).png').texture | |
texture.wrap = 'repeat' | |
self.rect_2 = Rectangle(texture=texture, size=self.size, pos=self.pos) | |
texture = CoreImage('Ground_(front_layer).png').texture | |
texture.wrap = 'repeat' | |
self.rect_3 = Rectangle(texture=texture, size=self.size, pos=self.pos) | |
Clock.schedule_interval(self.txupdate, 0) | |
def txupdate(self, *l): | |
t = Clock.get_boottime() | |
#print t | |
self.rect_1.tex_coords = -(t * 0.001), 0, -(t * 0.001 + 10), 0, -(t * 0.001 + 10), -10, -(t * 0.001), -10 | |
self.rect_2.tex_coords = -(t * 0.01), 0, -(t * 0.01 + 1), 0, -(t * 0.01 + 1), -1, -(t * 0.01), -1 | |
self.rect_3.tex_coords = -(t * 0.1), 0, -(t * 0.1 + 1), 0, -(t * 0.1 + 1), -1, -(t * 0.1), -1 | |
class MyApp(App): | |
def build(self): | |
return MyWidget(size=Window.size) | |
if __name__ == '__main__': | |
MyApp().run() |
Hey, I'm trying to understand how the repeat function works. If I remove the Clock Interval update I notice that the initial frame is not tiled. Regards Erik
@chaaken: hey, the repeats simply works because of the w and h factors in the equation i posted below, since it's between e.g 0.001 it means at 0.1% of the rect, it'll have already used the whole width/height of the texture, so what happens after that depends on the wrap
mode, since i set repeat, it'll start over, there are other modes that for example just continue with the same pixel value as the last one, but on its own, the mode is not enough, you also need the tex_coords value that make it wrap.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Rusbens1 sorry i didn't see your question before, i don't have notifications about that.
you see that the current tex_coords is set this way
it's uv mapping, there are 4 sets of coordinates, one for each uv coord of the rectangle, if we remove the factors, it's currently of this form
which makes sense if you look at it like this
if you want to scroll vertically, instead of horizontally, you just have to use
t
inv
coords instead ofu
coords.something like
might do the trick