Monday, August 23, 2021

kv 39 moving ball

#widget3.py
from kivy.app import App
from kivy.graphics import Ellipse
from kivy.metrics import dp
from kivy.properties import Clock
from kivy.uix.widget import Widget


class Widget3(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ball_size = dp(50)
        self.vx = 3
        self.vy = 3
        with self.canvas:
            self.ball = Ellipse(pos=self.center, size=(self.ball_size, self.ball_size))

        Clock.schedule_interval(self.update, 0.01)

    def on_size(self, *args):
        self.ball.pos = (self.center_x-self.ball_size/2, self.center_y-self.ball_size/2)

    def update(self, dt):
        x, y = self.ball.pos

        x += self.vx
        y += self.vy

        if y + self.ball_size > self.height:
            y = self.height  - self.ball_size
            self.vy = -self.vy
        if x + self.ball_size > self.width:
            x = self.width - self.ball_size
            self.vx = -self.vx
        if y < 0:
            y = 0
            self.vy = -self.vy
        if x < 0:
            x = 0
            self.vx = -self.vx

        self.ball.pos = (x,y)


class Widget3App(App):
    def build(self):
        return Widget3()


Widget3App().run()

reference:
 

Sunday, August 22, 2021

Nomad Living in a Truck

kv 38 canvas python



#widget2.py
from kivy.app import App
from kivy.graphics import Color, Rectangle, Line, Ellipse
from kivy.uix.widget import Widget
#from kivy.core.window import Window

class Widget2(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        with self.canvas:
            w = self.width
            h = self.height
            Color(0, 1, 0)
            self.rect1 = Rectangle(pos=(w/4, h/4), size=(w/2, h/2))
            Color(1, 0, 0)
            self.ellipse1 = Ellipse(pos=(w/8, h/8), size=(w/4, h/4))
            Color(1, 0, 0)
            self.ellipse2 = Ellipse(pos=(w*5/8, h/8), size=(w/4, h/4))
            Color(0, 0, 1)
            if w>h:
                self.line1 = Line(circle=(w/2, h/2, h/8), width=2)
            else:
                self.line1 = Line(circle=(w / 2, h / 2, w / 8), width=2)

        self.bind(pos=self.window_resize, size=self.window_resize)


    def window_resize(self, *args):
        w = self.width
        h = self.height
        self.rect1.pos = (w/4, h/4)
        self.rect1.size = (w/2, h/2)
        self.ellipse1.pos = (w/8, h/8)
        self.ellipse1.size = (w / 4, h / 4)
        self.ellipse2.pos = (w *5/ 8, h / 8)
        self.ellipse2.size = (w / 4, h / 4)
        if w>h:
            self.line1.circle = (w/2, h/2, h/8)
        else:
            self.line1.circle = (w / 2, h / 2, w / 8)
        self.line1.width = 2

class Widget2App(App):
    def build(self):
        return Widget2()


Widget2App().run()

reference:

window resize event

Saturday, August 21, 2021

Boston Dynamics

python 37 line 1



#widget1.kv
Widget1:
    canvas:
        Line:
            points: (0, self.height/2, self.width, self.height/2)
        Line:
            points: (self.width/2, 0, self.width/2, self.height)

        Color:
            rgba: 0, 0, 1, 1
        Line:
            rectangle: (self.width/4, self.height/4, self.width/2, self.height/2) #x, y, w, h
            width: 2

        Color:
            rgba: 1, 1, 0, 1
        Line:
            ellipse: (self.width/4, self.height/4, self.width/2, self.height/2)
            width: 2

        Color:
            rgba: 1, 0, 0, 1
        Line:
            circle: (self.width/2, self.height/2, self.height/4) if self.width > self.height else (self.width/2, self.height/2, self.width/4) #centerX, centerY, radius
            width: 2

------------------------
widget1.py
from kivy.app import App
from kivy.uix.widget import Widget


class Widget1(Widget):
    pass

class Widget1App(App):
    pass


Widget1App().run()

reference:

Thursday, August 19, 2021

共同富裕

kv36 draw ellipse rectangle



#widget1.kv
Widget1:
    canvas:
        Ellipse:
            pos: self.center_x - self.width/4, self.center_y - self.height/4
            size: self.width/2, self.height/2

        #Rectangle:
            #pos: self.center_x - self.width/4, self.center_y - self.height/4
            #size: self.width/2, self.height/2

---------------------
#widget1.py
from kivy.app import App
from kivy.uix.widget import Widget


class Widget1(Widget):
    pass

class Widget1App(App):
    pass


Widget1App().run()

referemce: