kivy - Can I check collide_widget using a list? -
i trying make game can move character around screen , had if character ran picture of tree, character stop moving. after getting work, tried change code instead of using tree widget, wanted iterate through list of widgets, if character runs of these, character stops moving. what's strange works when have 1 widget in list. can put list[0] or list[1] in code , when character stop when encountering widgets. again, if have more 1 widget in list , try iterate through list, not work, character not stop when encountering of widgets.
i'm wondering did wrong or how fix this. want if character runs of images in list, character stop moving.
from kivy.app import app kivy.uix.widget import widget kivy.uix.image import image kivy.core.window import window kivy.uix.screenmanager import screenmanager, screen kivy.uix.label import label kivy.uix.behaviors import buttonbehavior kivy.core.audio import soundloader kivy.uix.relativelayout import relativelayout kivy.uix.floatlayout import floatlayout kivy.uix.gridlayout import gridlayout kivy.uix.screenmanager import fallouttransition kivy.clock import clock kivy.graphics import color gamelayout = floatlayout(size=(300, 300)) bglayout = floatlayout() characterselectionlayout = gridlayout(cols=2) class game(screen): 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 npcs(image): def __init__(self, **kwargs): super(npcs, self).__init__(**kwargs) self.allow_stretch=true 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.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): bglayout.x += 4 else: in listofwidgets: if self.collide_widget(i): self.x -=0 else: self.x -=6 elif keycode[1] == 'right': self.source ='selectionscreen/right.zip' self.anim_delay=.20 if self.x > (window.width * .70): bglayout.x -= 4 else: in listofwidgets: if self.collide_widget(i): self.x += 0 else: self.x += 6 else: return false return true class gameapp(app): def build(self): global sm sm = screenmanager() game = game(name='game') sm.add_widget(game) global listofobject listofobject = [] hero = moveableimage(source='selectionscreen/right1.png', size_hint=(none,none), allow_stretch = false, size=(40, 65)) self.tree = npcs(source='selectionscreen/tree.zip', allow_stretch=false, size_hint=(none,none), pos_hint={'x':.20, 'y':.30}, size=(50, 50), pos=(300, 300)) self.testdude = npcs(source='selectionscreen/testdude.png', allow_stretch=false, size_hint=(none,none), pos_hint={'x':.60, 'y':.70}, size=(100, 124), pos=(800, 900)) listofwidgets.append(self.tree) listofwidgets.append(self.testdude) self.background=bg(source='selectionscreen/background12.png', pos_hint={'x':0, 'y':0}) bglayout.add_widget(self.background) bglayout.add_widget(self.tree) bglayout.add_widget(self.testdude) gamelayout.add_widget(bglayout) gamelayout.add_widget(hero) game.add_widget(gamelayout) return sm if __name__ == '__main__': gameapp().run()
to answer overall question...yes, there's no reason shouldn't able to.
tracking down problem need more code though...at moment snippet doesn't make sense, because list
isn't defined anywhere. maybe post short example runs, or @ least full code you're using?
as side note, list
not variable name because overrides list
class instantiator can annoying or actively buggy later.
Comments
Post a Comment