Search code examples
pythonlistvisual-studiokivyproject

Why is kivy isn't drawing lines


I tried to make Game using. I am new to the world of kivy module, and I tried to make Kivy game.

These are following files I used:

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

galaxy.kv

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

When I tried to run my code, the kivy was not able to draw the lines. What should I do?


Solution

  • You where missing the build function in 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()