from random import randint from time import sleep size(500, 500) score = 0 lives = 3 ball = ellipse(250, 250, 20, 20) paddle = rect(200, 480, 100, 10) bricks = [] for i in range(5): for j in range(10): brick = rect(j*50, i*20, 50, 20) bricks.append(brick) while True: ball.x+= 5 paddle.x = mouseX - 50 if ball.y < 0: ball.y = 0 elif ball.y > height: lives -= 1 ball.x = 250 ball.y = 250 sleep(1) elif ball.x < 0 or ball.x > width: ball.xspeed *= -1 elif ball.y + ball.height > paddle.y and ball.x > paddle.x and ball.x < paddle.x + paddle.width: ball.yspeed *= -1 else: for brick in bricks: if ball.intersects(brick): bricks.remove(brick) ball.yspeed *= -1 score += 10 background(255) fill(0) text("Score: " + str(score), 10, 20) text("Lives: " + str(lives), 10, 40) for brick in bricks: fill(randint(0, 255), randint(0, 255), randint(0, 255)) rect(brick.x, brick.y, brick.width, brick.height) fill(255, 0, 0) rect(paddle.x, paddle.y, paddle.width, paddle.height) fill(0, 0, 255) ellipse(ball.x, ball.y, ball.width, ball.height) if len(bricks) == 0 or lives == 0: background(255) fill(0) textSize(32) text("Game Over", width/2-80, height/2-16) textSize(16) text("Final Score: " + str(score), width/2-60, height/2+16) break 我使用processing当中的python部分写的打砖块游戏 那里存在错误
时间: 2023-11-13 07:04:13 浏览: 98
在你的代码中,有两个错误:
1. 在代码的开头,你需要把 `from random import randint from time import sleep` 改为两行,分别写成 `from random import randint` 和 `from time import sleep`。
2. 在第15行,你需要添加一个 `ball.yspeed` 的变量来控制球的垂直速度,否则会导致球不会弹回来。
以下是修改后的代码:
```
from random import randint
from time import sleep
size(500, 500)
score = 0
lives = 3
ball = ellipse(250, 250, 20, 20)
paddle = rect(200, 480, 100, 10)
bricks = []
for i in range(5):
for j in range(10):
brick = rect(j*50, i*20, 50, 20)
bricks.append(brick)
while True:
ball.x += 5
ball.y += ball.yspeed
paddle.x = mouseX - 50
if ball.y < 0:
ball.y = 0
ball.yspeed *= -1
elif ball.y > height:
lives -= 1
ball.x = 250
ball.y = 250
ball.yspeed = 5
sleep(1)
elif ball.x < 0 or ball.x > width:
ball.xspeed *= -1
elif ball.y + ball.height > paddle.y and ball.x > paddle.x and ball.x < paddle.x + paddle.width:
ball.yspeed *= -1
else:
for brick in bricks:
if ball.intersects(brick):
bricks.remove(brick)
ball.yspeed *= -1
score += 10
background(255)
fill(0)
text("Score: " + str(score), 10, 20)
text("Lives: " + str(lives), 10, 40)
for brick in bricks:
fill(randint(0, 255), randint(0, 255), randint(0, 255))
rect(brick.x, brick.y, brick.width, brick.height)
fill(255, 0, 0)
rect(paddle.x, paddle.y, paddle.width, paddle.height)
fill(0, 0, 255)
ellipse(ball.x, ball.y, ball.width, ball.height)
if len(bricks) == 0 or lives == 0:
background(255)
fill(0)
textSize(32)
text("Game Over", width/2-80, height/2-16)
textSize(16)
text("Final Score: " + str(score), width/2-60, height/2+16)
break
```
阅读全文
相关推荐


















