乒乓球拍没有正确击球



我正在尝试用Python 3制作一款乒乓游戏,可以说我已经走得很远了。我现在遇到了一个错误(错误不是错误(,当球碰到球拍边缘时不会反弹。相反,它会等待,直到它撞到桨的中心;可能是因为我使用了ycor函数。这是我迄今为止的代码:

import turtle


wn = turtle.Screen()
wn.bgcolor('white')
wn.title("Ping Pong")
wn.tracer(0)
wn.setup(width=800, height=600)

pad1 = turtle.Turtle()
pad1.shape('square')
pad1.penup()
pad1.shapesize(5, 1)
pad1.speed(0)
pad1.goto(-340, 0)
pad1.speed(0)

pad2 = turtle.Turtle()
pad2.shape('square')
pad2.penup()
pad2.shapesize(5, 1)
pad2.speed(0)
pad2.goto(340, 0)
pad2.speed(0)
ball = turtle.Turtle()
ball.shape("circle")
ball.penup()
ball.color('black')
ball.speed(0)
ball.dy = -0.2
ball.dx = -0.2

def paddle_a_up():
y = pad1.ycor()
y += 20
pad1.sety(y)

def paddle_a_down():
y = pad1.ycor()
y -= 20
pad1.sety(y)

def paddle_b_up():
y = pad2.ycor()
y += 20
pad2.sety(y)

def paddle_b_down():
y = pad2.ycor()
y -= 20
pad2.sety(y)

wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")
while True:
wn.update()
ball.sety(ball.ycor() + ball.dy)
ball.setx(ball.xcor() + ball.dx)

if ball.ycor() < -270:
ball.dy *= -1

if ball.ycor() > 280:
ball.dy *= -1
if ball.xcor() < -380:
ball.goto(0, 0)
ball.dx *= -1

if ball.xcor() > 380:
ball.goto(0, 0)
ball.dx *= -1

if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < pad2.ycor() + 40 and 
ball.ycor() > pad2.ycor - 40):
ball.setx(340)
ball.dx *= -1

这是

if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < pad2.ycor() + 40 and 
ball.ycor() > pad2.ycor - 40):

给我带来这么多麻烦的声明。我是一个初级程序员,但所有类型的答案都很受欢迎。提前感谢!

语句给我带来了所有这些麻烦。

if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < pad2.ycor() + 40 and 
ball.ycor() > pad2.ycor - 40):

没有问题,只是您的拼写错误问题,您正在调用一个方法作为属性,这就是为什么您在上面的行中将pad2.ycor替换为pad2.ycor(),这样您的行就变成了

if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < pad2.ycor() + 40 and 
ball.ycor() > pad2.ycor() - 40):

同样,对于第一个pad,在优化并添加额外条件后,您错过了评估其与球碰撞的条件对,您的代码现在将遵循

import turtle


wn = turtle.Screen()
wn.bgcolor('white')
wn.title("Ping Pong")
wn.tracer(0)
wn.setup(width=800, height=600)

pad1 = turtle.Turtle()
pad1.shape('square')
pad1.penup()
pad1.shapesize(5, 1)
pad1.speed(0)
pad1.goto(-340, 0)
pad1.speed(0)

pad2 = turtle.Turtle()
pad2.shape('square')
pad2.penup()
pad2.shapesize(5, 1)
pad2.speed(0)
pad2.goto(340, 0)
pad2.speed(0)
ball = turtle.Turtle()
ball.shape("circle")
ball.penup()
ball.color('black')
ball.speed(0)
ball.dy = -0.2
ball.dx = -0.2

def paddle_a_up():
y = pad1.ycor()
y += 20
pad1.sety(y)

def paddle_a_down():
y = pad1.ycor()
y -= 20
pad1.sety(y)

def paddle_b_up():
y = pad2.ycor()
y += 20
pad2.sety(y)

def paddle_b_down():
y = pad2.ycor()
y -= 20
pad2.sety(y)

wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")
while True:
wn.update()
ball.sety(ball.ycor() + ball.dy)
ball.setx(ball.xcor() + ball.dx)
if ball.ycor() < -270 or ball.ycor() > 280:
ball.dy *= -1
if ball.xcor() < -380 or ball.xcor() > 380:
ball.goto(0, 0)
ball.dx *= -1

if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < pad1.ycor() + 40 and ball.ycor() > pad1.ycor() - 40):
ball.setx(-340)
ball.dx *= -1
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < pad2.ycor() + 40 and ball.ycor() > pad2.ycor() - 40):
ball.setx(340)
ball.dx *= -1

相关内容

  • 没有找到相关文章

最新更新