void Wall::set_brick(std::string b) { string brick = b.substr(0, 2); upLeftBrick = brick; upRightBrick = brick; bottomLeftBrick = brick; bottomRightBrick = brick; horizonBrick = brick; verticalBrick = brick; }
时间: 2023-10-23 12:14:22 浏览: 101
这段代码是 C++ 语言编写的,实现了一个 Wall 类的方法 set_brick。该方法用于设置墙砖的样式,即将墙的左上、右上、左下、右下、水平和垂直部分的砖块样式全部设为一个给定的字符串 b 的前两个字符。
函数内部的代码逻辑包括:
1. 将参数 b 的前两个字符作为墙砖的样式;
2. 将墙的左上、右上、左下、右下、水平和垂直部分的砖块样式全部设为该样式。
相关问题
void Wall::set_brick(string ulb,string urb,string blb,\ string brb,string hb,string vb) { upLeftBrick = ulb.substr(0,2); upRightBrick = urb.substr(0,2); bottomLeftBrick = blb.substr(0,2); bottomRightBrick = brb.substr(0,2); horizonBrick = hb; verticalBrick = vb; }
这段代码是 C++ 语言编写的,实现了一个 Wall 类的方法 set_brick。该方法用于设置墙砖的样式,即将墙的左上、右上、左下、右下、水平和垂直部分的砖块样式全部设为给定的字符串 ulb、urb、blb、brb、hb 和 vb 的前两个字符。
函数内部的代码逻辑包括:
1. 将参数 ulb、urb、blb 和 brb 的前两个字符作为墙的左上、右上、左下和右下部分的砖块样式;
2. 将参数 hb 和 vb 作为墙的水平和垂直部分的砖块样式;
3. 将墙的左上、右上、左下、右下、水平和垂直部分的砖块样式全部设为上述样式。
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有什么错误
这段代码存在一个错误,即在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
```
这样就可以正确地删除砖块并修改小球的速度了。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)