无法让 kivy 显示相关 canvas 和突破游戏的项目

Cannot get kivy to display the relevant canvas and items for a breakout game

我正在尝试使用 Python 和 Kivy 创建一个突破游戏。我试过显示其他 kivy 功能​​,如标签和按钮,它们都运行良好。但是,当我尝试 运行 下面的代码时,我一直得到一个空白屏幕。任何帮助,将不胜感激。

BreakoutApp.py

from kivy.app import App # App is base for any kivy app
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.modalview import ModalView

from kivy.properties import (ListProperty, NumericProperty, ObjectProperty, StringProperty)

from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle

from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
import random

__version__ = '0.1' # used in Android compilation

#Game is a big widget that will contain the entire game
# A subclass of Float layout as it will proportion its children in proportion to its own pos and size
class Game(FloatLayout): # Will contain everything
    blocks = ListProperty([])
    player = ObjectProperty() # The game's player instance
    ball = ObjectProperty() # The game's ball instance

    def setup_blocks(self):
        for y_jump in range(5):
            for x_jump in range(10):
                print "in setup blocks"
                block = Block(pos_hint={
                    'x': 0.05 + 0.09*x_jump,
                    'y': 0.05 + 0.09*y_jump})
                self.blocks.append(block)
                self.add_widget(block)

# App will initialise everything that kivy needs
class BreakoutApp(App):
    def build(self):
            print "in build self blocks"
            g = Game()
            g.setup_blocks()
            return g
            #f = FloatLayout()
            #s = Scatter()
            #l = Label(text="Hello!",
            #          font_size=150)
            #f.add_widget(s)
            #s.add_widget(l)
            #return f



    #Require a class for each game object
    #kivy properties are special attributes declared at class level
class Player(Widget): # A moving paddle
    position = NumericProperty(0.5) 
    direction = StringProperty("none")

    def __init__(self, **kwargs):
        super(Player, self).__init__(**kwargs)
        with self.canvas:
            Color(1, 0, 0, 1)
            Rectangle(pos=self.pos, size=self.size)


class Ball(Widget): # A bouncing ball
    # pos_hints are for proportional positioning, see below
    pos_hint_x = NumericProperty(0.5)
    pos_hint_y = NumericProperty(0.3)
    proper_size = NumericProperty(0.)
    velocity = ListProperty([0.1,0.5])

class Block(Widget): # Each coloured block to destroy
    colour = ListProperty([1, 0, 0])

    def __init__(self, **kwargs):
        super(Block, self).__init__(**kwargs)
        self.colour = random.choice([
            (0.78,0.28,0), (0.28,0.63,0.28), (0.25,0.28,0.78)])



if __name__ == "__main__":
    print "main method called"
    BreakoutApp().run()

Breakout.kv

#:kivy 1.9.1

<Player>:
    canvas:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

<Ball>:
    canvas:
        Color:
            rgb: 1, 0.55, 0
        Rectangle:
            pos: self.pos
            size: self.size

<Block>:
    canvas:
        Color:
            rgb: self.color #property defined above
        Rectangle:
            pos: self.pos
            size: self.size
        Color:
            rgb: 0.1, 0.1, 0.1
        Line:
            rectangle:
                [self.x, self.y, self.width, self.height]

此外,我是否需要在 python 文件中明确引用布局 .kv 文件,或者是否有任何特定的命名限制。我在网上找到了一些文档来命名这两个,如下所示。

您在 kv 文件中有 color

<Block>:
    canvas:
        Color:
            rgb: self.color #property defined above

然而在 py 文件中有 colourU):

class Block(Widget): # Each coloured block to destroy
    colour = ListProperty([1, 0, 0])

如果你改变它,我相信你会得到想要的输出:

<Block>:
    canvas:
        Color:
            rgb: self.colour #property defined above

证明: