2017-05-20 5 views
0

PythonとKivyを使用してブレイクアウトゲームを作成しようとしています。私はラベルやボタンのような他の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

あなたはcolourU付き)がありPYファイルにはまだKVファイルにcolor

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

を持っている:

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

+0

は、私が行くこの後を与え、それが動作するかどうかを教えてくれます、ありがとうございます! –

+0

ええと、私はこれを試した後で、何の効果もないようですね? –

+0

@EoinClancyは最新の記事を参照してください。それは私の誤植です。 :) – KeyWeeUsr

関連する問題