如何在Kivy中每两秒将椭圆的颜色更改为随机整数?使用随机整数。

huangapple go评论63阅读模式
英文:

How do I change the color of ellipse to a randint every two seconds in Kivy, using random integers

问题

class CanvasExample5(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ball_size = dp(50)
        with self.canvas:
            self.color = Color(0, 1, 0)
            self.ball = Ellipse(pos=self.center, size=(self.ball_size, self.ball_size))
        Clock.schedule_interval(self.update, 1/60)
        Clock.schedule_interval(self.colour, 1)
        self.vx = dp(5)
        self.vy = dp(3)

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

    def colour(self, *args):
        random_1 = random.randint(0, 255)
        random_2 = random.randint(0, 255)
        random_3 = random.randint(0, 255)
        self.color = Color(random_1, random_2, random_3)
英文:
class CanvasExample5(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ball_size = dp(50)
        with self.canvas:
            self.color = Color(0, 1, 0)
            self.ball = Ellipse(pos=self.center, size=(self.ball_size, self.ball_size))
        Clock.schedule_interval(self.update, 1/60)
        Clock.schedule_interval(self.colour, 1)
        self.vx = dp(5)
        self.vy = dp(3)

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

    def colour(self, *args):
        random_1 = random.randint(0, 255)
        random_2 = random.randint(0, 255)
        random_3 = random.randint(0, 255)
        self.color = Color(random_1, random_2, random_3)

答案1

得分: 1

Ellipse 图形命令使用执行时生效的 Color,因此之后更改 Color 不会影响 Ellipse。一个修复方法是清除画布,然后使用新的 Color 重新绘制 Ellipse

def colour(self, *args):
    random_1 = random.random()
    random_2 = random.random()
    random_3 = random.random()
    self.canvas.clear()
    with self.canvas:
        Color(random_1, random_2, random_3)
        self.ball = Ellipse(pos=self.center, size=(self.ball_size, self.ball_size))

此外,请注意,Color 命令接受的值范围在 0 到 1 之间,而不是 0 到 255。

英文:

The Ellipse graphics command uses the Color that is in effect at the time it is executed, so changing the Color afterwards will have no effect on the Ellipse. One fix is to clear the canvas and redraw the Ellipse with the new Color:

def colour(self, *args):
    random_1 = random.random()
    random_2 = random.random()
    random_3 = random.random()
    self.canvas.clear()
    with self.canvas:
        Color(random_1, random_2, random_3)
        self.ball = Ellipse(pos=self.center, size=(self.ball_size, self.ball_size))

Also, note that the Color command takes values between 0 and 1, not 0 to 255.

huangapple
  • 本文由 发表于 2023年6月22日 11:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定