我收到一个错误,但我没有找到它 (Python)
I get an error, but I don't find it (Python)
我正在尝试用 turtle 创建一个游戏。它包括移动球拍并且不要让球掉落。我是初学者。这是程序:
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, -287)
# 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
elif (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle.ycor() + 40 and ball.ycor() > paddle.ycor() - 40):
ball.setx(340)
ballx *= -1
最后几行代码出现错误。这是:
elif (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle.ycor() + 40 and ball.ycor() > paddle.ycor() - 40):
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
我什至不知道我在最后几行代码中写的是否正确。想打球return如果打到球拍。 python 版本:3.7.3
您在这一行中漏掉了一个右括号
pen.write('Score: {}'.format(score), align='center', font=('Courier', 24, 'normal'))
我正在尝试用 turtle 创建一个游戏。它包括移动球拍并且不要让球掉落。我是初学者。这是程序:
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, -287)
# 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
elif (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle.ycor() + 40 and ball.ycor() > paddle.ycor() - 40):
ball.setx(340)
ballx *= -1
最后几行代码出现错误。这是:
elif (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle.ycor() + 40 and ball.ycor() > paddle.ycor() - 40):
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
我什至不知道我在最后几行代码中写的是否正确。想打球return如果打到球拍。 python 版本:3.7.3
您在这一行中漏掉了一个右括号
pen.write('Score: {}'.format(score), align='center', font=('Courier', 24, 'normal'))