我尝试在 Python 上的海龟扩展上做一个乒乓球游戏,一切都很顺利。
我想在玩家达到一定分数时重新开始游戏。游戏正常重新开始,并且窗口自行清除,除了中心的一个黑色方块,在接下来的游戏中保持不变。我怎样才能摆脱那个广场?
这是我重新启动游戏的代码:
import turtle
def run_game():
wn = turtle.Screen()
wn.title("Ping Pong Pour le Nuls")
wn.bgcolor("green")
wn.setup(width=800, height=600)
wn.tracer(0)
Player_1 = wn.textinput("Player 1", "Name of the player: ")
Player_2 = wn.textinput("Player 2", "Name of the player: ")
# Score
score_a = 0
score_b = 0
# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)
# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.4
ball.dy = -0.4
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write(str(Player_1) + " : 0 " + str(Player_2) + " : 0 ", align="center", font=("Courier", 14, "normal"))
# Funtion
def paddle_a_up():
y = paddle_a.ycor()
y += 20
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor()
y += -20
paddle_a.sety(y)
def paddle_b_up():
y = paddle_b.ycor()
y += 20
paddle_b.sety(y)
def paddle_b_down():
y = paddle_b.ycor()
y += -20
paddle_b.sety(y)
def pen_2():
pen_2 = turtle.Turtle()
pen_2.speed(0)
pen_2.color("white")
pen_2.penup()
pen_2.hideturtle()
pen_2.goto(0, 0)
pen_2.write("Player 2 Wins", align="center", font=("Courier", 24, "normal"))
# Keyboard binding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "x")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")
# Main Game Loop
while True:
wn.update()
# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
score_a += 1
pen.clear()
pen.write(str(Player_1) + " : {} ".format(score_a) + str(Player_2) + " : {} ".format(score_b),
align="center", font=("Courier", 14, "normal"))
if ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
score_b += 1
pen.clear()
pen.write(str(Player_1) + " : {} ".format(score_a) + str(Player_2) + " : {} ".format(score_b),
align="center", font=("Courier", 14, "normal"))
# Paddle and ball collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):
ball.setx(340)
ball.dx *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):
ball.setx(-340)
ball.dx *= -1
# Paddles can't go beyond screen
if paddle_a.ycor() >= 260:
paddle_a_down()
if paddle_a.ycor() <= -260:
paddle_a_up()
if paddle_b.ycor() >= 260:
paddle_b_down()
if paddle_b.ycor() <= -260:
paddle_b_up()
if score_a >= 3:
restart = wn.textinput("Game result", "Well done " + Player_1 + ", you won ! \nDo you want to restart ? (y/n)")
if restart == "y":
wn.reset()
run_game()
else:
break
if score_b >= 3:
restart = wn.textinput("Game result", "Well done " + Player_2 + ", you won ! \nDo you want to restart ? (y/n)")
if restart == "y":
wn.reset()
run_game()
else:
break
run_game()
非常感谢您的帮助,祝您有美好的一天。 游戏输出