class Brick: def __init__(self, canvas, ltx, lty, rbx, rby, color): self.lx = ltx self.ly = lty self.rx = rbx self.ry = rby self.cvs = canvas self.color = color self.id = canvas.create_rectangle((ltx, lty, rbx, rby), fill=color) def exist(self, brick): if ball.isHit(brick) == 'Upper' or ball.isHit(brick) == 'Lower': self.cvs.delete(brick.id) bricks.remove(brick) ball.change_velocity_y() elif ball.isHit(brick) == 'Left' or ball.isHit(brick) == 'Right': self.cvs.delete(brick.id) bricks.remove(brick) ball.change_velocity_x() ball = Ball(canvas, 3, 5, 'red') paddle = Paddle(canvas, 10) bricks = [] n = 0 x = 50 y = 50 j = 10 while n < j: bricks.append(Brick(canvas, x, y, x+30, y+15, 'blue')) x = x + 20 y = y + 10 n = n + 1 while True: ball.fly() paddle.run() if ball.isHit(paddle) == 'Upper': ball.change_velocity_y() n = 0 while n < j: bricks[n].exist(bricks[n]) n = n + 1有什么错误
时间: 2024-01-18 07:04:22 浏览: 168
gb28181.tar.gz_GB28181_brick7en_gb28181 sip_reviewtu2_sip gb
5星 · 资源好评率100%
这段代码存在一个错误,即在Brick类的exist方法中,应该使用self来引用当前对象的属性和方法,而不是使用参数brick。正确的代码应该是:
```python
class Brick:
def __init__(self, canvas, ltx, lty, rbx, rby, color):
self.lx = ltx
self.ly = lty
self.rx = rbx
self.ry = rby
self.cvs = canvas
self.color = color
self.id = canvas.create_rectangle((ltx, lty, rbx, rby), fill=color)
def exist(self):
if ball.isHit(self) == 'Upper' or ball.isHit(self) == 'Lower':
self.cvs.delete(self.id)
bricks.remove(self)
ball.change_velocity_y()
elif ball.isHit(self) == 'Left' or ball.isHit(self) == 'Right':
self.cvs.delete(self.id)
bricks.remove(self)
ball.change_velocity_x()
ball = Ball(canvas, 3, 5, 'red')
paddle = Paddle(canvas, 10)
bricks = []
n = 0
x = 50
y = 50
j = 10
while n < j:
bricks.append(Brick(canvas, x, y, x+30, y+15, 'blue'))
x = x + 20
y = y + 10
n = n + 1
while True:
ball.fly()
paddle.run()
if ball.isHit(paddle) == 'Upper':
ball.change_velocity_y()
n = 0
while n < j:
bricks[n].exist()
n = n + 1
```
这样就可以正确地删除砖块并修改小球的速度了。
阅读全文