Kivy - 删除touch_down上的对象

时间:2016-10-19 12:43:31

标签: python kivy

我最近开始和Kivy一起玩,想做一个简单的射击游戏。

这是我的代码:

import kivy
kivy.require('1.9.0')

from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '600')

from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Rectangle
from kivy.lang import Builder
from kivy.config import Config
import random, time

a = Builder.load_string('''
<BattleField>
    BoxLayout:
        orientation: 'vertical'

<Target>
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'target.jpg'    

''')

class BattleField(Widget):

    #to destroy target
    def on_touch_down(self,touch):
        #if self.collide_point(*touch.pos):
        if self.collide_widget(self.target):
            self.remove_widget(self.target)

    def appear_target(self, *args):

        random_pos = tuple([random.randint(0, 600) for i in range(2)])
        self.target = Target()
        self.target.pos = (random_pos)
        self.add_widget(self.target)


class Target(Widget):

    pass

class ClockApp(App):

    def build(self):
        g = BattleField()
        Clock.schedule_interval(g.appear_target, 1)
        return g

if __name__ == '__main__':
    ClockApp().run()

所以目前它每1秒钟添加一个新目标。但是,我在这里遇到的问题很少:

  1. 只能删除最近添加的小部件
  2. 无论我点击哪里,都会删除最近添加的小部件。
  3. 有人可以帮我解决这个问题吗?谢谢!

2 个答案:

答案 0 :(得分:0)

Have you tried "self.parent.remove_widget(self.target)" instead of "self.remove_widget(self.target)"? To remove a widget, your code needs to act from a higher point in the widgets tree (I'd like to test yours but I don't have a computer right now).

答案 1 :(得分:0)

你去了(你原来的帖子没有太多变化)。您应该查看官方文档中的this page

from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '600')
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Rectangle
from kivy.lang import Builder
from kivy.config import Config
import random, time

a = Builder.load_string('''
<BattleField>
    BoxLayout:
        orientation: 'vertical'

<Target>
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'target.jpg'    

''')

class BattleField(Widget):

    def __init__(self, **kwargs):
        super(BattleField, self).__init__(**kwargs)
        Clock.schedule_interval(self.appear_target, 1)


    def appear_target(self, *args):
        random_pos = tuple([random.randint(0, 600) for i in range(2)])
        target = Target()
        target.pos = random_pos
        self.add_widget(target)


class Target(Widget):

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.parent.remove_widget(self)


class ClockApp(App):

    def build(self):
        return BattleField()

if __name__ == '__main__':
    ClockApp().run()