【问题标题】:Python Turtle : Clear window and restart a gamePython Turtle:清除窗口并重新启动游戏
【发布时间】:2020-04-19 19:15:25
【问题描述】:

我尝试在 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()

非常感谢您的帮助,祝您有美好的一天。 Game Output

【问题讨论】:

  • 你能发布你的整个代码吗?特别是初始化海龟的部分。我有一种预感,“方形”就是乌龟。
  • 您好,首先感谢您的帮助!我将整个代码添加到主帖

标签: python shapes restart python-turtle


【解决方案1】:

这里。我没有使用wn.reset(),而是使用wn.clearscreen()。它成功了!

代码:

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.color("black")
    ball.shape("circle")
    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.clearscreen()
                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.clearscreen()
                run_game()
            else:
                break


run_game()

希望这会有所帮助!

【讨论】:

  • @GuillaumePetri 你介意点赞并接受吗?非常感谢!
  • 我投了赞成票,但声望低于 15 似乎并不抱歉。我接受了,但再次感谢!
  • 没问题@GuillaumePetri 谢谢!
猜你喜欢
  • 2020-08-16
  • 2015-02-19
  • 2023-03-30
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
相关资源
最近更新 更多