python 类初始化中不能判断_类在python中没有正确初始化

我试着学习如何使用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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值