gpt4 book ai didi

python - 这行代码使程序对重启功能无 react

转载 作者:行者123 更新时间:2023-12-04 04:01:31 24 4
gpt4 key购买 nike

我尝试用 Python 编写一个程序,让用户对其中一只 turtle 下注,赛后告诉他是否正确。我想让用户选择是否要通过单击“r”来重新开始比赛 - (我使用了 turtle.onkey 方法)并且在我输入这行代码之前它工作正常:bet = screen.textinput("选择你的赌注","输入蓝色或绿色:")。现在该程序让用户可以像我想要的那样选择他的赌注,但是当按下“r”时它不会使用react..我想澄清的是,在 textinput 代码行之后,程序不会对任何 .onkey 方法使用react。有人知道为什么会这样吗?

代码:

import turtle
import random
import ctypes

speed = random.randint(0, 2)

myTurtle = turtle.Turtle()
myTurtle2 = turtle.Turtle()
screen = turtle.Screen()
def up():
myTurtle.setheading(90)
myTurtle.forward(10)

def down():
myTurtle.setheading(270)
myTurtle.forward(10)

def left():
myTurtle.setheading(180)
myTurtle.forward(10)

def right():
myTurtle.setheading(0)
myTurtle.forward(10)

def up2():
myTurtle2.setheading(90)
myTurtle2.forward(10)

def down2():
myTurtle2.setheading(270)
myTurtle2.forward(10)

def left2():
myTurtle2.setheading(180)
myTurtle2.forward(10)

def right2():
myTurtle2.setheading(0)
myTurtle2.forward(10)

def restart():
myTurtle.setposition(400, -300)
myTurtle.setheading(90)
myTurtle2.setposition(-400, -300)
myTurtle2.setheading(90)
speed = random.randint(0, 2)
bet = screen.textinput("Choose your bet", "Enter blue or green: ")
while myTurtle.ycor() < 300 and myTurtle2.ycor() < 300:
if speed == 1:
up()
elif speed == 2:
up2()
speed = random.randint(0, 2)
if myTurtle.ycor() == 300:
if bet == "blue":
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You lost", 0x00010000)
else:
if bet == "green":
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You lost", 0x00010000)

screen.title("Turtle race")
turtle.listen()
myTurtle.shape("turtle")
myTurtle2.shape("turtle")
myTurtle.setposition(400,-300)
myTurtle.setheading(90)
myTurtle2.setposition(-400,-300)
myTurtle2.setheading(90)
myTurtle.dot(10, "blue")
myTurtle2.dot(10, "green")
myTurtle.pencolor("blue")
myTurtle2.pencolor("green")

myTurtle.speed(0)
myTurtle2.speed(0)
bet = screen.textinput("Choose your bet", "Enter blue or green: ")
while myTurtle.ycor() < 300 and myTurtle2.ycor() < 300:
if speed == 1:
up()
elif speed == 2:
up2()
speed = random.randint(0, 2)

if myTurtle.ycor() == 300:
if bet == "blue":
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Blue is the winner", "You lost", 0x00010000)
else:
if bet == "green":
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You win!", 0x00010000)
else:
ctypes.windll.user32.MessageBoxW(0, "Green is the winner", "You lost", 0x00010000)



turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')

turtle.onkey(restart, 'r')

turtle.onkey(up2, 'w')
turtle.onkey(down2, 's')
turtle.onkey(left2, 'a')
turtle.onkey(right2, 'd')
turtle.mainloop()

最佳答案

问题是 onkey 需要关注主窗 Eloquent 能从系统获取 key ,但是当您执行 textinput 时,这个焦点就会丢失,它需要 screen.listen () 再次。


最少的工作代码。

因为您在 restart() 内部和 restart() 外部重复了一些代码,所以我运行 restart() 而不是外部代码。

因为我不用Windows所以我用tkinter.messagebox来显示消息。 turtle 已经使用 tkinter 来显示带有 Canvas 和 textinput 的主窗口。

import turtle
import random
#import tkinter as tk
from tkinter import messagebox

# functions ---

def up():
myTurtle.setheading(90)
myTurtle.forward(10)

def down():
myTurtle.setheading(270)
myTurtle.forward(10)

def left():
myTurtle.setheading(180)
myTurtle.forward(10)

def right():
myTurtle.setheading(0)
myTurtle.forward(10)

def up2():
myTurtle2.setheading(90)
myTurtle2.forward(10)

def down2():
myTurtle2.setheading(270)
myTurtle2.forward(10)

def left2():
myTurtle2.setheading(180)
myTurtle2.forward(10)

def right2():
myTurtle2.setheading(0)
myTurtle2.forward(10)

def restart():
myTurtle.setposition(400, -300)
myTurtle.setheading(90)
myTurtle2.setposition(-400, -300)
myTurtle2.setheading(90)

speed = random.randint(0, 2)
bet = screen.textinput("Choose your bet", "Enter blue or green: ")

screen.listen() # <--- set focus on main window after `textinput`

while myTurtle.ycor() < 300 and myTurtle2.ycor() < 300:
if speed == 1:
up()
elif speed == 2:
up2()
speed = random.randint(0, 2)

if myTurtle.ycor() == 300:
if bet == "blue":
messagebox.showinfo("Result", "Blue is the winner\n\nYou win!")
else:
messagebox.showinfo("Result", "Blue is the winner\n\nYou lost")
else:
if bet == "green":
messagebox.showinfo("Result", "Green is the winner\n\nYou win!")
else:
messagebox.showinfo("Result", "Green is the winner\n\nYou lost")

# --- main ---

speed = random.randint(0, 2)

myTurtle = turtle.Turtle()
myTurtle2 = turtle.Turtle()

screen = turtle.Screen()
screen.title("Turtle race")

myTurtle.shape("turtle")
myTurtle2.shape("turtle")

turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')

turtle.onkey(restart, 'r')

turtle.onkey(up2, 'w')
turtle.onkey(down2, 's')
turtle.onkey(left2, 'a')
turtle.onkey(right2, 'd')

restart()

turtle.mainloop()

编辑:

减少代码 - 我将 turtle 放在列表中,稍后我可以使用 myTurtles[number] 并且我可以使用一个函数 up(number) 而不是两个函数 up()up2()

import turtle
import random
from tkinter import messagebox

# functions ---

def up(number):
myTurtles[number].setheading(90)
myTurtles[number].forward(10)

def down(number):
myTurtles[number].setheading(270)
myTurtles[number].forward(10)

def left(number):
myTurtles[number].setheading(180)
myTurtles[number].forward(10)

def right(number):
myTurtles[number].setheading(0)
myTurtles[number].forward(10)

def restart():

myTurtles[0].setposition(400, -300)
myTurtles[0].setheading(90)
myTurtles[1].setposition(-400, -300)
myTurtles[1].setheading(90)

bet = screen.textinput("Choose your bet", "Enter blue or green: ")
screen.listen()

speed = random.randint(0, 2)
while myTurtles[0].ycor() < 300 and myTurtles[0].ycor() < 300:
if speed < 2: # randint(0, 2) can gives 0, 1, 2
up(speed)
speed = random.randint(0, 2)

if myTurtles[0].ycor() == 300:
if bet == "blue":
messagebox.showinfo("Result", "Blue is the winner\n\nYou win!")
else:
messagebox.showinfo("Result", "Blue is the winner\n\nYou lost")
else:
if bet == "green":
messagebox.showinfo("Result", "Green is the winner\n\nYou win!")
else:
messagebox.showinfo("Result", "Green is the winner\n\nYou lost")

# --- main ---

screen = turtle.Screen()
screen.title("Turtle race")

myTurtles = [
turtle.Turtle(),
turtle.Turtle(),
]

myTurtles[0].shape("turtle")
myTurtles[1].shape("turtle")

turtle.onkey(lambda: up(0), 'Up')
turtle.onkey(lambda: down(0), 'Down')
turtle.onkey(lambda: left(0), 'Left')
turtle.onkey(lambda: right(0), 'Right')

turtle.onkey(lambda: up(1), 'w')
turtle.onkey(lambda: down(1), 's')
turtle.onkey(lambda: left(1), 'a')
turtle.onkey(lambda: right(1), 'd')

turtle.onkey(restart, 'r')

restart()

turtle.mainloop()

关于python - 这行代码使程序对重启功能无 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62989985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com