Reputation: 6785
I am making a game with Kivy where you can move a character around the screen. Earlier, I had it working where when you move the character right, the background image would scroll to the left, and same with the right. Recently, what I tried to do was add the background image to a widget along with another image, with the intention that i could control multiple image locations with 'widget.pos', but as soon as I did this, not only does the second image (hero2) not scroll right or left but neither does the background image. I am wondering what I did wrong in my code here.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.audio import SoundLoader
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import FallOutTransition
gamelayout = FloatLayout(size=(300, 300))
class Game(Screen):
pass
class IconButton(ButtonBehavior, Image):
pass
class Bg(Image):
def __init__(self, **kwargs):
super(Bg, self).__init__(**kwargs)
self.allow_stretch = True
self.size_hint = (None, None)
self.size = (1440, 1440)
class MoveableImage(Image):
def __init__(self, **kwargs):
super(MoveableImage, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
self._keyboard.bind(on_key_up=self.on_keyboard_up)
self.size_hint = (.11, .11)
self.y = (Window.height/2.1)
self.app = App.get_running_app()
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.source = 'selectionscreen/left.zip'
self.anim_delay=.20
if self.x < (Window.width * .25):
self.app.test.x += 4
else:
self.x -= 6
elif keycode[1] == 'right':
self.source ='selectionscreen/right.zip'
self.anim_delay=.20
if self.x > (Window.width * .70):
self.app.test.x -= 4
else:
self.x += 6
elif keycode[1] == 'down':
self.source ='selectionscreen/right.zip'
self.anim_delay=.20
if self.y < (Window.height * .25):
self.app.test.y += 4
else:
self.y -= 6
elif keycode[1] == 'up':
self.source = 'selectionscreen/back.zip'
self.anim_delay=.1
if self.y > (Window.height * .70):
self.app.test.y -= 4
else:
self.y += 6
else:
return False
return True
def on_keyboard_up(self, keyboard, keycode):
if keycode[1] == 'left':
self.source = 'selectionscreen/left1.png'
elif keycode[1] == 'right':
self.source ='selectionscreen/right1.png'
elif keycode[1] == 'down':
self.source ='selectionscreen/right1.png'
elif keycode[1] == 'up':
self.source ='selectionscreen/back2.png'
else:
return False
return True
class gameApp(App):
def build(self):
global sm
sm = ScreenManager()
game = Game(name='game')
sm.add_widget(game)
hero = MoveableImage(source='selectionscreen/jeezyright1.png', pos=(75, 40))
self.hero2 = Image(source='selectionscreen/hero2.zip', pos=(100, 200))
self.background=Bg(source='selectionscreen/background9.png')
self.test=Bg()
self.test.add_widget(self.background)
self.test.add_widget(self.hero2)
gamelayout.add_widget(self.test)
gamelayout.add_widget(hero)
game.add_widget(gamelayout)
return sm
if __name__ == '__main__':
gameApp().run()
Upvotes: 0
Views: 545
Reputation: 452
Looks like the problem here is that you are using a FLoatLayout. When you move a normal layout (such as a FloatLayout), it does not automatically change the children's co-ordinates. If you want that behavior, try using a RelativeLayout.
http://kivy.org/docs/api-kivy.uix.relativelayout.html
Peace out
Upvotes: 1