1

I'm using the nifty Kivy framework to program a game for Android. I'm trying to create a clock callback to run a specified piece of code (used to draw) 60 times a second.

For some reason, anything I draw inside of a Kivy clock event doesn't get drawn to the screen. To eliminate all variables I could, I took this sample code:

import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse


class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        with self.canvas:
            Color(1, 1, 0)
            d = 30.
            Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d))


class MyPaintApp(App):
    def build(self):
        return MyPaintWidget()


if __name__ == '__main__':
    MyPaintApp().run()

This works fine - when I click on the screen it puts a yellow circle there. However, when I modify it like this:

import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse
from kivy.clock import Clock


class MyPaintWidget(Widget):
    def update(self, t):
        with self.canvas:
            Color(1, 1, 0)
            d = 30.
            Ellipse(pos=(200 - d/2, 200 - d/2), size=(d, d))


class MyPaintApp(App):
    def build(self):
        m = MyPaintWidget()
        Clock.schedule_interval(m.update, 1.)
        return m


if __name__ == '__main__':
    MyPaintApp().run()

Nothing is ever drawn to the screen. Why? EDIT: I found out it only draws to the screen when I click it. Still not useful - I need to use this for a game's event loop! I am using Kivy 1.4.0 (the second most recent) and Ubuntu. This also doesn't work as intended on Android, either.

4

1 回答 1

1

我可以运行您的代码 - 我做了一些更改,因此您可以看到它确实每秒都被调用。它现在打印出 D 的值,该值每秒都在变化,黄色的大圆圈每秒都在增长。我希望这可以帮助你。

import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse
from kivy.clock import Clock

class MyPaintWidget(Widget):
    d = 10
    def update(self, t):
        print("D is", self.d)
        with self.canvas:
            Color(1, 1, 0)
            self.d = self.d + 1
            Ellipse(pos=(200 - self.d/2, 200 - self.d/2), size=(self.d, self.d))


class MyPaintApp(App):
    def build(self):
        m = MyPaintWidget()
        Clock.schedule_interval(m.update, 1.)
        return m


if __name__ == '__main__':
    MyPaintApp().run()
于 2012-10-09T13:40:20.687 回答