Feb-13-2022, 04:17 AM
(This post was last modified: Feb-13-2022, 04:17 AM by deanhystad.)
"What can I do better?" is a lazy question. Please ask specific questions.
The first thing you should do is put all the code in one module. I and others would be more inclined to help if we could copy your posted code directly into our editor and run it. This is not a long enough program to need multiple modules.
I think bounce is not quite right. You can put all the bounces in one function (I would add a wall bounce), but I think it clearer if you have wall_bounce(), paddle_bounce() and brick_bounce().
I think paddle bounce "self.dy = -self.dy" makes for a dull game. If the ball hits in the center of the panel then dy = -dy, but if the ball hits off center you should adjust the relative lateral and vertical velocities. This would give the player some ability to aim the ball instead of just blocking the ball.
The bricks bounce looks wrong. You are going to hit a brick on the top, on the bottom, on the left side, or on the right side. You need a test for each. I would start by trying these (pseudo code):
I don't understand this: "How to get every brick gets vanishing after 3 hits?' Should a brick vanish after it is hit 3 times? Should all the bricks vanish after you hit a particular brick three times? Should all the bricks vanish after you hit any brick three times? Should all the bricks vanish after 3 brick hits? I suspect the first (hit a brick 3 times and it vanishes), but I also expect your code to do that.
This code:
The first thing you should do is put all the code in one module. I and others would be more inclined to help if we could copy your posted code directly into our editor and run it. This is not a long enough program to need multiple modules.
I think bounce is not quite right. You can put all the bounces in one function (I would add a wall bounce), but I think it clearer if you have wall_bounce(), paddle_bounce() and brick_bounce().
I think paddle bounce "self.dy = -self.dy" makes for a dull game. If the ball hits in the center of the panel then dy = -dy, but if the ball hits off center you should adjust the relative lateral and vertical velocities. This would give the player some ability to aim the ball instead of just blocking the ball.
The bricks bounce looks wrong. You are going to hit a brick on the top, on the bottom, on the left side, or on the right side. You need a test for each. I would start by trying these (pseudo code):
if brick.left <= ball.center_x <= brick.right: # Hit top or bottom ball.dy = -ball.dy else: # Hit side ball.dx = -ball.dxI also question if it makes sense to do all the bricks. I would run your code to find out if that was easy for me to do.
I don't understand this: "How to get every brick gets vanishing after 3 hits?' Should a brick vanish after it is hit 3 times? Should all the bricks vanish after you hit a particular brick three times? Should all the bricks vanish after you hit any brick three times? Should all the bricks vanish after 3 brick hits? I suspect the first (hit a brick 3 times and it vanishes), but I also expect your code to do that.
This code:
if bricks: for brick in bricks: score = brick.takehit(score)In conjunction with this code:
def takehit(self, score): # the ball has collided with *this* brick self.hits -= 1 if self.hits == 0: self.kill() score += 1 return scoreShould result in a brick disappearing after it "takehit()" 3 times