kivy pong教程:'' NoneType"没有属性' center'

时间:2016-04-30 02:49:50

标签: python kivy

跟随kivy pong tutorial

我在这篇文章kivy official pong tutorial: 'NoneType' object has no attribute 'center'

中遇到了与OP相同的问题

我试过这个解决方案无济于事。

当我跑步时,这就是我得到的:

    Traceback (most recent call last):
   File "main.py", line 58, in <module>
     PongApp().run()
   File "/usr/local/lib/python2.7/site-packages/kivy/app.py", line 802, in run
     root = self.build()
   File "main.py", line 52, in build
     game.serve_ball()
   File "main.py", line 34, in serve_ball
     self.ball.center = self.center
 AttributeError: 'NoneType' object has no attribute 'center'

main.py:

from kivy.app import App
from kivy.uix.widget import Widget
#import properties and vector
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
# import clock and randomint
from kivy.clock import Clock
from random import randint



#define the ball
class PongBall(Widget):

    #velocity of ball on x and y axis
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)

    #referencelist property so we can use ball.velocity as
    #shorthand, e.g w.pos, x.pos, w.x and w.y
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    #"move" function will move the ball one step.
    #this will be called in equal intervals to animate the ball
    def move(self):
        self.pos = Vector(*self.velocity) + self.pos

class PongGame(Widget):
    ball = ObjectProperty(None)

    def serve_ball(self):
        self.ball.center = self.center
        self.ball.velocity = Vector(4,0).rotate(randint(0,360))

    def update(self, dt):
        self.ball.move()

        #bounce ball off top and bottom
        if (self.ball.y < 0) or (self.ball.top > self.height):
            self.ball.velocity_y *= -1
        #bounce ball of left and right
        if (self.ball.x < 0) or (self.ball.right > self.widht):
            self.ball.velocity_x += -1

#define base Class for kivy app
#function initializes and returns the Root Widget
class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 6.0)
        return game

#here the class PongApp is initialized and its run() method called. This initializes and starts our Kivy application.
if __name__ == '__main__':
    PongApp().run()

pong.kv:

<PongBall>
    size: 50, 50
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size

#draw the pong game canvas and lines
<PongGame>:
    canvas:
        Rectangle:
            pos: self.center_x - 5, 0
            size: 10, self.height

    Label:
        font_size: 70
        center_x: root.width / 4
        top: root.top - 50
        text: "0"

    Label:
        font_size: 70
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: "0"

    PongBall:
        id: pong_ball
        center: self.parent.center

1 个答案:

答案 0 :(得分:1)

进入你的kv文件,找到<PongGame>并将值传递给python文件中的ball

<PongGame>:
    ball: pong_ball

这会在ballPongBall之间建立id: pong_ball之间的链接,然后您就可以访问velocity,{{1}等属性等等。

如果没有这样的链接,center会跳出来,这意味着在某个地方没有这样的东西,在你的情况下,没有AttributeError 1}}(和许多其他人)基本上是center值。