2014-03-19 51 views
0

我正在與Kivy進行遊戲,您可以在屏幕上移動角色。早些時候,我將它應用於當你將角色移動到右側時,背景圖像會滾動到左側,與右側相同。最近,我試圖做的是將背景圖像與另一個圖像一起添加到小部件中,意圖是我可以用'widget.pos'控制多個圖像位置,但只要我這樣做,不僅第二個圖像(英雄2)不向右或向左滾動,但背景圖像也不會。我想知道我在這裏的代碼中做了什麼錯誤。如何使用Kivy控制多個圖像位置

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

回答

相關問題