【问题标题】:Why is kivy isn't drawing lines为什么 kivy 不画线
【发布时间】:2023-02-21 17:24:55
【问题描述】:

我试着让游戏使用。我是 kivy 模块世界的新手,我尝试制作 Kivy 游戏。

这些是我使用的以下文件:

主程序

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 无法绘制线条。我应该怎么办?

【问题讨论】:

  • .kv 文件是否以 &lt;/MainWidget&gt; 结尾?
  • 不,我已经尝试过但没有用
  • 抱歉,由于此错误而无法正常工作:kivy.factory.FactoryException: Unknown class <MainWidget>
  • 有人帮帮我吗?
  • 请有人回答我......

标签: python list visual-studio kivy project


【解决方案1】:

您缺少 Galaxy App 中的构建功能

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()

【讨论】:

    猜你喜欢
    • 2014-08-14
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2023-01-08
    • 2023-03-19
    • 2016-08-12
    • 1970-01-01
    相关资源
    最近更新 更多