我是制作电子游戏的新手,我被一件我不懂的事情困住了。我正在创建的游戏包括移动一个桨并击球,如下所示:
我用的是海龟,我试着写下桨和球之间的碰撞,但我不明白它是如何工作的。以下是脚本:
import turtle
width,height = 800, 600
score = 0
wn = turtle.Screen()
wn.title('Breakout')
wn.bgcolor('black')
wn.setup(width,height)
wn.tracer()
# Paddle
paddle = turtle.Turtle()
paddle.speed(0)
paddle.shape('square')
paddle.shapesize(stretch_wid=5, stretch_len=1)
paddle.color('white')
paddle.penup()
paddle.left(90)
paddle.goto(0, -290)
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape('square')
ball.color('white')
ball.penup()
ball.goto(0,0)
ballx = 3
bally = -3
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color('white')
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write('Score: 0', align='center', font=('Courier', 24, 'normal'))
# Paddle movement
def paddle_right():
x = paddle.xcor()
x -= 20
paddle.setx(x)
def paddle_left():
x = paddle.xcor()
x += 20
paddle.setx(x)
wn.listen()
wn.onkeypress(paddle_right, 'a')
wn.onkeypress(paddle_left, 'd')
while True:
wn.update()
ball.setx(ball.xcor() + ballx)
ball.sety(ball.ycor() + bally)
# Borders
if ball.xcor() > 390:
ball.setx(390)
ballx *= -1
if ball.xcor() < -390:
ball.setx(-390)
ballx *= -1
if ball.ycor() > 290:
ball.sety(290)
bally *= -1
if ball.ycor() < -290:
ball.goto(0, 0)
bally *= -1
score -= 1
pen.clear()
pen.write('Score: {}'.format(score), align='center', font=('Courier', 24, 'normal'))
# Paddle and ball collision
有人能帮我写最后几行代码,并向我解释一下它是如何工作的吗?多谢各位
相关问题 更多 >
编程相关推荐