简体   繁体   English

无法使自己显示突破游戏的相关画布和物品

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

I am trying to create a breakout game using Python and Kivy. 我正在尝试使用Python和Kivy创建突破游戏。 I have tried displaying other kivy features like labels and buttons and they all work perfectly. 我尝试显示其他标记功能(例如标签和按钮),它们都可以正常工作。 However, when I try to run the below code I keep getting a blank screen. 但是,当我尝试运行以下代码时,我一直显示空白屏幕。 Any help would be appreciated. 任何帮助,将不胜感激。

BreakoutApp.py 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 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]

Also, do I need to explicitly refer to the layout .kv file in the python file or are there any specific naming restrictions. 另外,我是否需要在python文件中显式引用layout .kv文件,或者是否有任何特定的命名限制。 I found some documentation online to name the two as found below. 我在网上找到了一些文档来命名这两个文档,如下所示。

You have in kv file color 您有kv文件color

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

yet in py file there is colour (with U ): 但是在py文件中有colour (带有U ):

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

If you change it, you'll get the desired output I believe: 如果更改它,您将获得所需的输出,我相信:

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

Proof: 证明:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM