Reputation: 1
Following the kivy pong tutorial
and I've come across the same problem as OP in this post kivy official pong tutorial: 'NoneType' object has no attribute 'center'
I've attempted this solution to no avail.
When I run this is what I get:
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
Upvotes: 0
Views: 1001
Reputation: 12199
Go into your kv file, find <PongGame>
and pass a value to your ball
in python file like this
<PongGame>:
ball: pong_ball
This will make a link between ball
and PongBall
which has id: pong_ball
and then you'll be able to access properties such as velocity
, center
and so on.
If there's no such a link, an AttributeError
will jump out and it means that there's no such a thing in somewhere, in your case there's no center
(and many others) in basically None
value.
Upvotes: 1