为什么我的剪刀石头布游戏中有一段时间 True 循环不起作用?

Why does a while True loop not work in my rock paper scissors game?

我正在尝试制作一款石头剪刀布游戏,其中包含 3 个按钮,可通过导入乌龟在屏幕上点击。一切顺利并且有效,按钮有效,单击某个按钮时屏幕上显示正确的文本。 (我还没有添加计算机选择部分,所以我的代码只是用户输入)但是现在我想添加当你第二次点击一个按钮时,最后一个按钮的文本消失。因为如果你一直点击,文本会停留在那里并且会重叠。所以我想添加一个“你想再玩一次吗?”通过将按钮置于 while True 循环中来输入内容,这样每次单击按钮时它都会询问用户是否要再次播放,但这不起作用。我没有收到任何错误,屏幕打开并为我绘制了所有内容,但我无法单击任何按钮,文本没有显示然后崩溃。如果我删除 while 循环和输入问题,它会再次运行。它应该工作吧?或者这是不可能的?我在其他地方用另一个游戏读过它,所以我不知道为什么它不起作用。这是我的 while True 循环代码。 while True 循环结束了,如果我的代码比应该的长,我很抱歉,我还不知道所有的技巧:

import turtle

fontButtons = ("Courier", 22, "bold")
fontText = ("Arial", 26, "bold")

screen = turtle.Screen()
screen.bgcolor("light blue")
screen.setup(800,800)

#center = turtle.Turtle()
#center.goto(0,0)
#center.shape("circle")
#center.size(1)

#game
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()
pen.fillcolor("light yellow")
pen.goto(-250,0)
pen.begin_fill()
pen.pendown()

for i in range(2):
    pen.forward(225)
    pen.left(90)
    pen.forward(200)
    pen.left(90)

pen.end_fill()
pen.penup()
pen.goto(25,0)
pen.begin_fill()
pen.pendown()

for i in range(2):
    pen.forward(225)
    pen.left(90)
    pen.forward(200)
    pen.left(90)

pen.end_fill()
pen.penup()
pen.goto(-135,165)
pen.write("You", font=fontText, align="center")

pen.goto(135,165)
pen.write("Computer", font=fontText, align="center")
             

#rock

rock = turtle.Turtle()
rock.hideturtle()
rock.penup()
rock.speed(0)
rock.fillcolor("light yellow")
rock.goto(-200,-100)
rock.pendown()
rock.begin_fill()

for i in range(2):
    rock.forward(100)
    rock.left(90)
    rock.forward(40)
    rock.left(90)
    
rock.end_fill()

rock.penup()
rock.goto(-150,-95)
rock.write("ROCK",font=fontButtons, align="center")

#paper

paper = turtle.Turtle()
paper.hideturtle()
paper.penup()
paper.speed(0)
paper.fillcolor("light yellow")
paper.goto(-50,-100)
paper.pendown()
paper.begin_fill()

for i in range(2):
    paper.forward(100)
    paper.left(90)
    paper.forward(40)
    paper.left(90)
    
paper.end_fill()

paper.penup()
paper.goto(0,-95)
paper.write("PAPER",font=fontButtons, align="center")

#scissor

scissor = turtle.Turtle()
scissor.hideturtle()
scissor.penup()
scissor.speed(0)
scissor.fillcolor("light yellow")
scissor.goto(100,-100)
scissor.pendown()
scissor.begin_fill()

for i in range(2):
    scissor.forward(100)
    scissor.left(90)
    scissor.forward(40)
    scissor.left(90)
    
scissor.end_fill()

scissor.penup()
scissor.goto(150,-95)
scissor.write("SCISSOR",font=fontButtons, align="center")

while True:
#rock click
    def buttonClickR(x,y):
        if x > -200 and x < -100 and y > -100 and y < -60:
            pen.goto(-135,100)
            pen.write("Rock", font=fontText, align="center")
                            

    turtle.onscreenclick(buttonClickR, 1, True)
    turtle.listen()
    
#paper click
    def buttonClickP(x,y):
        if x > -50 and x < 50 and y > -100 and y < -60:
            pen.goto(-135,100)
            pen.write("Paper", font=fontText, align="center")
            

    turtle.onscreenclick(buttonClickP, 1, True)
    turtle.listen()
    

#scissor click
    def buttonClickS(x,y):
        if x > 100 and x < 200 and y > -100 and y < -60:
            pen.goto(-135,100)
            pen.write("Scissor", font=fontText, align="center")
            
    
    turtle.onscreenclick(buttonClickS, 1, True)
    turtle.listen()

    again = input("Do you want to play again? ")

    if again == 'yes':
        screen.clear()
    
    else:
        print('Bye!')       


turtle.done()

turtle 的“循环”不需要 while 循环 - turtle.done() 不会“结束”turtle 代码 - 它表示你已“完成”设置并 starts the everlasting turtle mainloop.

你的代码是因为你一遍又一遍地解决同样的问题,而不是把它放到一个方法中去用参数调用。

你画了 2 个大方框和 3 个小方框,小方框里面的文字也居中,大方框上面的文字不同。

在 turtle 中请求文本输入可以通过 turtle.textinput(...) 更优雅地完成,然后跳到控制台上的 input()。

要退出,请在适当的地方使用 turtle.exitonclick() - 或调用 turtle.bye()

您的代码重复了很多您应该放入函数中的东西:

import turtle

# this uses the ealy defined "pen" turtle - no need to use different ones
# and a global for logic - you can refine that if you want, for demo it works

fontButtons = ("Courier", 22, "bold")
fontText = ("Arial", 26, "bold")

screen = turtle.Screen()
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()
pen.fillcolor("light yellow")

# reapeating things put into methods to be used multiple times

def box_at(x, y, width, height):
    """Makes a box in the current fillcolor at x,y to x+width,y+height. 
    Pen is up afterwards."""
    pen.penup()
    pen.goto(x,y)
    pen.begin_fill()
    pen.pendown()

    for i in range(2):
        pen.forward(width)
        pen.left(90)
        pen.forward(height)
        pen.left(90)

    pen.end_fill()
    pen.penup()


def button(x,y,width,height, text):
    """Makes box + text centered inside, space width appropriate to text."""
    # no need to repeat painting a box - we solved that earlier
    box_at(x,y,width,height)

    pen.goto(x+width/2,y)
    pen.write(text, font=fontButtons, align="center")

def board():
    # YOU
    box_at(-250,10,225,200)

    # COMPUTER
    box_at(25,10,225,200)

    # custom text placement
    pen.goto(-135,165)
    pen.write("You", font=fontText, align="center")
    pen.goto(135,165)
    pen.write("Computer", font=fontText, align="center")

def buttons():
    # all the buttons
    button(-200, -100, 120, 40, "ROCK")
    button( -50, -100, 120, 40, "PAPER")
    button( 100, -100, 120, 40, "SCISSOR")

def reset():
    # clear screen and make it beautiful
    screen.bgcolor("light blue")
    screen.setup(800,800)
    board()
    buttons()

游戏使用这些函数是这样的:

reset()
moves = 0

# all the logic
def buttonClick(x,y):
    global moves  # not fancy - buts its just a demo

    # handle all button clicks inside this one function
    # adapted the x/y values to fit my bigger buttons
    if x > -200 and x < -80 and y > -100 and y < -60:
        board()  # this clears the former things
        pen.goto(-135,100)
        pen.write("Rock", font=fontText, align="center")
        moves += 1
    elif x > -50 and x < 70 and y > -100 and y < -60:
        board() # this clears the former things
        pen.goto(-135,100)
        pen.write("Paper", font=fontText, align="center")
        moves += 1
    elif x > 100 and x < 220 and y > -100 and y < -60:
        board() # this clears the former things
        pen.goto(-135,100)
        pen.write("Scissor", font=fontText, align="center")
        moves += 1
    
    # TODO make the computer do something, evaluate moves, print tally

    # program logic, every 3rd click ask for continue
    if moves == 3:

        # TODO: print tally and announce winner

        again = turtle.textinput("Continue?" , "Do you want to play again? ")

        if again == 'yes':
            moves = 0
            reset() 
        else:
            print('Bye!')
            screen.clear()         
            turtle.exitonclick() # or turtle.bye() directly

# register the game logic click handler
turtle.onscreenclick(buttonClick, 1, True)

# start the mainloop until turtly.bye() or exitonclick() is done
turtle.mainloop()