1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, NumericProperty, ListProperty
from kivy.clock import Clock
from kivy.vector import Vector
from kivy.core.audio import SoundLoader
import random
from kivy.uix.widget import Widget
Builder.load_string("""
<MonImage>:
canvas.before:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
pos: self.pos
size: self.size
source: 'crane.png'
<Killer>:
canvas.before:
Color:
rgba: (1, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
source: 'crane.png'
<BougeIm>:
Button:
pos: self.pos
text: "Ajoute"
on_press: root.ajoute()
Button:
pos: (root.width - self.width , 0)
text: "killer"
on_press: root.set_killeur()
""")
class MonImage(Widget):
pass
class Killer(Widget):
""" widget tueur """
pass
class Tir(Widget):
pass
class BougeIm(Widget):
vecteur = [Vector(4,0), Vector(2,0)]
indice = 0
def ajoute(self):
""" ajoute un nouveau widget MonImage"""
self.vecteur.append(Vector(random.randint(1,8),random.randint(1,8)))
wid = MonImage(pos=(200, 100))
self.add_widget(wid)
self.ids[f'new{self.indice}'] = wid
self.indice += 1
def initialise(self):
for position, tag in [((1, 1),'yop'), ((1, 300
), 'truc')]:
wid = MonImage(pos=position)
self.add_widget(wid)
self.ids[tag] = wid
self.callback = Clock.schedule_interval(self.update, 1/60)
def set_killeur(self):
self.vecteur.append(Vector(random.randint(1,6),random.randint(1,6)))
wid = Killer(pos=(100, 100))
self.add_widget(wid)
self.ids['killeur'] = wid
def update(self, dt):
for index, (widg_id, widg) in enumerate(self.ids.items()):
widg.pos = self.vecteur[index]+widg.pos
if widg.x + widg.size[0] > self.width or widg.x < 0:
self.vecteur[index].x *= -1
if widg.y + widg.size[1]> self.height or widg.y <0:
self.vecteur[index].y *= -1
if "killeur" in self.ids.keys() and widg != self.ids["killeur"] and len(self.ids)>1:
if self.ids['killeur'].collide_widget(widg):
self.remove_widget(self.ids[widg_id])
self.ids.pop(widg_id)
self.vecteur.pop(index)
break
class TestApp(App):
def build(self):
WidgetRoot = BougeIm()
WidgetRoot.initialise()
return WidgetRoot
if __name__ == '__main__':
TestApp().run() |