如何使用Kivy控制多个图像位置

时间:2014-03-19 07:18:42

标签: kivy

我正和Kivy一起玩游戏,你可以在屏幕上移动角色。早些时候,我让它工作在你向右移动角色的地方,背景图像会向左滚动,与右边相同。最近,我尝试做的是将背景图像与另一个图像一起添加到小部件,目的是我可以用'widget.pos'控制多个图像位置,但是一旦我这样做,不仅第二个image(hero2)不向右或向左滚动,但背景图像也不向右滚动。我想知道我的代码在这里做错了什么。

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()

1 个答案:

答案 0 :(得分:1)

这里看起来问题是您正在使用FLoatLayout。当您移动普通布局(例如FloatLayout)时,它不会自动更改子项的坐标。如果您想要这种行为,请尝试使用RelativeLayout。

http://kivy.org/docs/api-kivy.uix.relativelayout.html

和平