Visual Studio 为什么Kivy不画线

r3i60tvu  于 2023-02-24  发布在  其他
关注(0)|答案(1)|浏览(113)

我试着用kivy模块做游戏。我是kivy模块世界的新手,我试着做kivy游戏。
这些是我使用的以下文件:

www.example.commain.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import *
from kivy.lang import Builder
from kivy.properties import NumericProperty

Builder.load_file('galaxy.kv')
class MainWidget(Widget):
    perspective_point_x = NumericProperty(0)
    perspective_point_y = NumericProperty(0)

    V_NB_LINES = 7
    V_NB_SPACING = .1
    vertical_lines = []

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        self.init_vertical_lines()

    def on_parent(self, widget, parent):
        pass

    def on_size(self, *args):
        self.update_vertical_lines()

    def on_perspective_point_x(self, widget, value):
        pass

    def on_perspective_point_y(self, widget, value):
        pass

    def init_vertical_lines(self):
        with self.canvas:
            Color(1, 1, 1)
            for i in range(0, self.V_NB_LINES):
                self.vertical_lines.append(Line())

    def update_vertical_lines(self):
        central_line_x = int(self.width/2)
        spacing = int(self.V_NB_SPACING * self.width)
        offset = -int(self.V_NB_LINES/2)
        for i in range(0, self.V_NB_LINES):
            line_x = central_line_x + offset*spacing
            self.vertical_lines[i].points = [line_x, 0, line_x, self.height]
            offset += 1

class GalaxyApp(App):
    pass

GalaxyApp().run()

星系. kv

<MainWidget>
    perspective_point_x: self.width  / 2
    perspective_point_y: self.height * 0.75

当我试着运行我的代码时,kivy无法画出线条。我该怎么办?

iugsix8n

iugsix8n1#

您在GalaxyApp中缺少构建功能

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import *
from kivy.lang import Builder
from kivy.properties import NumericProperty

Builder.load_file('galaxy.kv')

class MainWidget(Widget):
    perspective_point_x = NumericProperty(0)
    perspective_point_y = NumericProperty(0)

    V_NB_LINES = 7
    V_NB_SPACING = .1
    vertical_lines = []

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        self.init_vertical_lines()

    def on_parent(self, widget, parent):
        pass

    def on_size(self, *args):
        self.update_vertical_lines()

    def on_perspective_point_x(self, widget, value):
        pass

    def on_perspective_point_y(self, widget, value):
        pass

    def init_vertical_lines(self):
        with self.canvas:
            Color(1, 1, 1)
            for i in range(0, self.V_NB_LINES):
                self.vertical_lines.append(Line())

    def update_vertical_lines(self):
        central_line_x = int(self.width/2)
        spacing = int(self.V_NB_SPACING * self.width)
        offset = -int(self.V_NB_LINES/2)
        for i in range(0, self.V_NB_LINES):
            line_x = central_line_x + offset*spacing
            with self.canvas:
                self.vertical_lines[i].points = [line_x, 0, line_x, self.height]
            offset += 1

class GalaxyApp(App):
    def build(self):
        return MainWidget()

GalaxyApp().run()

相关问题