2017-03-01 91 views
0

我目前正在學習如何在python中使用kivy。我下面這個教程創建一個簡單的乒乓球比賽在Kivy運行簡單的pong教程時出現Typer錯誤

https://kivy.org/docs/tutorials/pong.html

的當我到達那裏我試圖動畫球的一部分。我完全按照教程編寫了代碼。我用F5運行從IDLE程序,我得到的交互shell一個

return game() 
Type Error: 'PongGame' object is not callable 

消息和遊戲本身凍結的窗口。有關如何解決這個問題的任何想法?在此先感謝

這裏是我寫的代碼(完全按照教程): -

爲main.py

代碼
from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.properties import NumericProperty, ReferenceListProperty,\ 
ObjectProperty 
from kivy.vector import Vector 
from kivy.clock import Clock 
from random import randint 


class PongBall(Widget): 
    velocity_x = NumericProperty(0) 
    velocity_y = NumericProperty(0) 
    velocity = ReferenceListProperty(velocity_x, velocity_y) 

    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 off top and bottom 
     if (self.ball.y < 0) or (self.ball.top > self.height): 
      self.ball.velocity_y *= -1 

     # bounce off left and right 
     if (self.ball.x < 0) or (self.ball.right > self.width): 
      self.ball.velocity_x *= -1 


class PongApp(App): 
    def build(self): 
     game = PongGame() 
     game.serve_ball() 
     Clock.schedule_interval(game.update, 1.0/60.0) 
     return game 


if __name__ == '__main__': 
    PongApp().run() 

代碼pong.kv

#:kivy 1.0.9 

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

<PongGame>: 
    ball: pong_ball 

    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 
+0

很奇怪。在錯誤信息中返回遊戲(),但是在你的代碼中,「返回遊戲」(後者是正確的,第一個嘗試調用該對象!) –

回答