Tuesday, August 24, 2021

kv 40 moving balls


#widget4.py

from kivy.app import App
from kivy.graphics import Ellipse, Color
from kivy.metrics import dp
from kivy.properties import Clock
from kivy.uix.widget import Widget
from random import random

class Widget4(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.balls_properties = []
        self.num = 10
        for i in range(self.num):
            new_ball = {
                "size": dp(10+90*random()),
                "vx": 1+9*random(),
                "vy": 1+9*random()
            }

            self.balls_properties.append(new_ball)

        with self.canvas:
            self.balls = []
            for i in range (self.num):
                Color(random(), random(), random())
                size = self.balls_properties[i]["size"]
                ball = Ellipse(pos=(0, 0), size=(size, size))
                self.balls.append(ball)

        Clock.schedule_interval(self.update, 0.05)

    def on_size(self, *args):
        for i in range(self.num):
            size = self.balls_properties[i]["size"]
            self.balls[i].pos = ((self.width-size)*random(), (self.height-size)*random())

    def update(self, dt):
        for i in range(self.num):
            x, y = self.balls[i].pos
            size = self.balls_properties[i]["size"]
            vx = self.balls_properties[i]["vx"]
            vy = self.balls_properties[i]["vy"]

            x += vx
            y += vy

            if y + size > self.height:
                y = self.height - size
                self.balls_properties[i]["vy"] = -vy
            if x + size > self.width:
                x = self.width - size
                self.balls_properties[i]["vx"] = -vx
            if y < 0:
                y = 0
                self.balls_properties[i]["vy"] = -vy
            if x < 0:
                x = 0
                self.balls_properties[i]["vx"] = -vx

            self.balls[i].pos = (x,y)


class Widget4App(App):
    def build(self):
        return Widget4()


Widget4App().run()

reference:

No comments:

Post a Comment