D
D
Dmitry2022-01-19 02:16:04
Python
Dmitry, 2022-01-19 02:16:04

kivy python collision logic?

Section of python code

class birds :
    # Макет FloatLayot
    display = ObjectProperty(None)
    # Позиция птиц
    x = (-40)
    y = 800
    x_final = 700
    #Виджет Image
    bird_only = Image()

    def __init__ (self) :
        Clock.schendule_interval (self.bird_only,10)

    def bird (self) :
        self.bird_only = Image (source = 'путь к изображению',size_hint = (None,None) ,size = (60,60),pos = (self.x,self.y)
        self.display.add_widget(self.bird_only)
        # Анимация
        self.anim = Animation (x = self.x_final ,y = y ,d = 5)
        self.anim.start(self.bird_only)

I did not write imports and also excluded kv markup.
The bottom line is this: the bird flies off the screen, and then the widget is removed, thereby creating an endless cycle of birds in nature.
How can this be implemented, I have already rummaged through tons of information, there is little documentation on this topic and on the official website it does not look as detailed as we would like.
If I do the following in the birds class, I add the delete birds function :
def delete_birds (self) :
   if self.x > 650 :
       self.display.remove_widget(self.bird_only)
,
then nothing happens, the bird just flies off the screen, to check, I set its stop point in the middle, and accordingly my guesses were confirmed. The bird just remains in the center, then another one flies out and so on until the program freezes.
Most likely the variable x is simply not tracked.
Tell me how you can fix the problem and I would be glad if at least I could find documentation about this.
Final option:
class birds :
    # Макет FloatLayot
    display = ObjectProperty(None)
    # Позиция птиц
    x = (-40)
    y = 800
    x_final = 700
    #Виджет Image
    bird_only = Image()

    def __init__ (self) :
        Clock.schendule_interval (self.bird_only,10)
    
     def delete_birds (self) :
         if self.x > 650 :
             self.display.remove_widget(self.bird_only)

    def bird (self) :
        self.bird_only = Image (source = 'путь к изображению',size_hint = (None,None) ,size = (60,60),pos = (self.x,self.y)
        self.display.add_widget(self.bird_only)
        # Анимация
        self.anim = Animation (x = self.x_final ,y = y ,d = 5)
        self.anim.start(self.bird_only)
        self.delete_birds()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-19
@20PYTHON20

Well, for starters, why delete and recreate individual birds when you can just "teleport" them to the other side of the screen?
In addition, if I understand correctly, delete_birds() will work for you immediately, without waiting for the end of the animation, since the start() method does not perform animation - it only plans its gradual execution during the further operation of the window. Use the on_complete event on your Animation object.
Details, as always, in the documentation .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question