我试着学习如何使用Kivy(以前除了学校作业外从未编写过任何程序),但我遇到了一些麻烦。在
我的代码如下所示。在
问题出在我的Ball类中,当应用程序启动时,BubblePop.SetupLevel()被调用,Ball应该被Ball类的实例填充。但不知怎么的,它不起作用。因此,当调用BobblePop.update()时,ball.draw()行出现错误AttributeError: 'NoneType' object has no attribute 'draw'
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 kivy.lang import Builder
from kivy.graphics import Color, Ellipse
from random import random, randint
from kivy.core import window
#balls are what bounce around the screen. They turn into bubbles upon
#colliding with a bubble.
def Ball(width,height):
def __init__(self,width,height):
self.x = randint(0,width)
self.y = randint(0,height)
self.colorRGB = [0,0,0]
self.velX = 0
self.velY = 0
self.ball_size = 20
def draw(self):
Ellipse(pos=(self.x,self.y), size = (self.ball_size,self.ball_size))
class Bubble(Widget):
pass
class BubblePop(Widget):
balls = []
bubbles = []
def SetupLevel(self,numballs):
for x in xrange(numballs):
ball = Ball(self.height,self.width)
self.balls.append(ball)
def on_touch_down(self,touch):
with self.canvas:
r = random()
g = random()
b = random()
Color(r,g,b)
d = 80.
self.bubbles.append([touch.x - d / 2,touch.y - d / 2,[r,g,b]])
Ellipse(pos=(self.bubbles[-1][0], self.bubbles[-1][1]), size=(d, d))
def update(self,dt):
with self.canvas:
self.canvas.clear()
for ball in self.balls:
ball.draw()
class BubbleApp(App):
def build(self):
game = BubblePop()
game.SetupLevel(10)
Clock.schedule_interval(game.update, 1.0/60.0)
return game
if __name__ == '__main__':
BubbleApp().run()