无法获得kivy显示相关画布和突破游戏的项目

时间:2017-05-20 00:47:35

标签: python kivy kivy-language

我正在尝试使用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文件,或者是否有任何特定的命名限制。我在网上找到了一些文件来命名这两个文件,如下所示。

1 个答案:

答案 0 :(得分:1)

你有kv文件color

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

但在py文件中有colour U ):

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

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

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

证明:

enter image description here