如何让所有海龟使用 python 海龟 onclick 响应点击?
How do I get all the turtles to respond to a click using python turtle onclick?
我正在尝试制作一款游戏,您必须在其中收集乌龟,这样当您单击乌龟时,它就会移动到一个盒子中。这样做的问题是,当您单击其中一只海龟时,您没有单击的海龟会移动。我认为这是因为我创建了一个变量来控制多只海龟,但我不知道如何解决这个问题。这是我的代码:(顺便说一句,我在使用堆栈溢出的 'code' 按钮添加代码时遇到了问题)
import turtle
import random
s = turtle. Screen()
s.bgcolor("lightgreen")
print("Welcome! this pond is infested with turtles! Can you catch all the turtles?")
for x in range(0,5):
t = turtle.Turtle()
c = 'yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white'
t.color(random.choice(c))
t.shape("turtle")
t.penup()
t.hideturtle()
r = random.randint(-75,75)
r2 = random.randint(-75,75)
r3 = random.randint(1,10)
t.goto(r,r2)
t.showturtle()
def fxn (x, y):
t.goto(95,-122)
print("yes")
t.onclick(fxn)
我建议使用面向对象编程来执行此操作,因为它可能比程序化操作容易得多。程序上应该可以做到,但是很繁琐。
一个面向对象的例子:
import turtle
import random
# Create MyTurtle class
class MyTurtle:
def __init__(self, colour):
self.t = turtle.Turtle()
self.t.color(colour)
self.t.shape("turtle")
self.t.penup()
self.t.hideturtle()
r = random.randint(-75,75)
r2 = random.randint(-75,75)
r3 = random.randint(1,10)
self.t.goto(r,r2)
self.t.onclick(self.on_turtle_clicked)
self.t.showturtle()
def on_turtle_clicked(self, x, y):
self.t.goto(95, -122)
print("yes")
s = turtle.Screen()
s.bgcolor("lightgreen")
colours = ['yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white']
print("Welcome! this pond is infested with turtles! Can you catch all the turtles?")
turtles = []
for x in range(0, 5):
turtles.append(MyTurtle(random.choice(colours)))
如果需要,Python 中有面向对象编程的教程。
https://realpython.com/python3-object-oriented-programming/
希望对您有所帮助!
我们可以使用 functools 中的 partial
修补您的代码。这允许我们将“which turtle”传递给您的 onclick()
函数:
from random import choice, randint
from functools import partial
from turtle import Screen, Turtle
COLORS = ['yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white']
def fxn(t, x, y):
t.goto(95, -122)
print("yes")
screen = Screen()
screen.bgcolor('lightgreen')
print("Welcome! This pond is infested with turtles! Can you catch all the turtles?")
for x in range(5):
t = Turtle()
t.hideturtle()
t.shape('turtle')
t.color(choice(COLORS))
t.penup()
x = randint(-75, 75)
y = randint(-75, 75)
t.goto(x, y)
t.onclick(partial(fxn, t))
t.showturtle()
screen.mainloop()
为适应 partial()
所做的更改很少,上面的大部分更改都是针对样式的。我同意@Nathcat 的观点,即使用 面向对象的 方法会更好。但是,由于我们要追逐的是海龟,所以我会将 MyTurtle
设为海龟而不是 包含一只乌龟:
from random import choice, randint
from turtle import Screen, Turtle
COLORS = ['yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white']
class MyTurtle(Turtle):
def __init__(self, color):
super().__init__()
self.hideturtle()
self.color(color)
self.shape('turtle')
self.penup()
x = randint(-75, 75)
y = randint(-75, 75)
self.goto(x, y)
self.onclick(self.on_turtle_clicked)
self.showturtle()
def on_turtle_clicked(self, x, y):
self.goto(95, -122)
print("yes")
screen = Screen()
screen.bgcolor('lightgreen')
print("Welcome! This pond is infested with turtles! Can you catch all the turtles?")
turtles = [MyTurtle(choice(COLORS)) for _ in range(5)]
screen.mainloop()
我正在尝试制作一款游戏,您必须在其中收集乌龟,这样当您单击乌龟时,它就会移动到一个盒子中。这样做的问题是,当您单击其中一只海龟时,您没有单击的海龟会移动。我认为这是因为我创建了一个变量来控制多只海龟,但我不知道如何解决这个问题。这是我的代码:(顺便说一句,我在使用堆栈溢出的 'code' 按钮添加代码时遇到了问题)
import turtle
import random
s = turtle. Screen()
s.bgcolor("lightgreen")
print("Welcome! this pond is infested with turtles! Can you catch all the turtles?")
for x in range(0,5):
t = turtle.Turtle()
c = 'yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white'
t.color(random.choice(c))
t.shape("turtle")
t.penup()
t.hideturtle()
r = random.randint(-75,75)
r2 = random.randint(-75,75)
r3 = random.randint(1,10)
t.goto(r,r2)
t.showturtle()
def fxn (x, y):
t.goto(95,-122)
print("yes")
t.onclick(fxn)
我建议使用面向对象编程来执行此操作,因为它可能比程序化操作容易得多。程序上应该可以做到,但是很繁琐。
一个面向对象的例子:
import turtle
import random
# Create MyTurtle class
class MyTurtle:
def __init__(self, colour):
self.t = turtle.Turtle()
self.t.color(colour)
self.t.shape("turtle")
self.t.penup()
self.t.hideturtle()
r = random.randint(-75,75)
r2 = random.randint(-75,75)
r3 = random.randint(1,10)
self.t.goto(r,r2)
self.t.onclick(self.on_turtle_clicked)
self.t.showturtle()
def on_turtle_clicked(self, x, y):
self.t.goto(95, -122)
print("yes")
s = turtle.Screen()
s.bgcolor("lightgreen")
colours = ['yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white']
print("Welcome! this pond is infested with turtles! Can you catch all the turtles?")
turtles = []
for x in range(0, 5):
turtles.append(MyTurtle(random.choice(colours)))
如果需要,Python 中有面向对象编程的教程。 https://realpython.com/python3-object-oriented-programming/
希望对您有所帮助!
我们可以使用 functools 中的 partial
修补您的代码。这允许我们将“which turtle”传递给您的 onclick()
函数:
from random import choice, randint
from functools import partial
from turtle import Screen, Turtle
COLORS = ['yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white']
def fxn(t, x, y):
t.goto(95, -122)
print("yes")
screen = Screen()
screen.bgcolor('lightgreen')
print("Welcome! This pond is infested with turtles! Can you catch all the turtles?")
for x in range(5):
t = Turtle()
t.hideturtle()
t.shape('turtle')
t.color(choice(COLORS))
t.penup()
x = randint(-75, 75)
y = randint(-75, 75)
t.goto(x, y)
t.onclick(partial(fxn, t))
t.showturtle()
screen.mainloop()
为适应 partial()
所做的更改很少,上面的大部分更改都是针对样式的。我同意@Nathcat 的观点,即使用 面向对象的 方法会更好。但是,由于我们要追逐的是海龟,所以我会将 MyTurtle
设为海龟而不是 包含一只乌龟:
from random import choice, randint
from turtle import Screen, Turtle
COLORS = ['yellow', 'gold', 'orange', 'red', 'magenta', 'navy', 'blue', 'purple', 'cyan', 'brown', 'black', 'gray', 'white']
class MyTurtle(Turtle):
def __init__(self, color):
super().__init__()
self.hideturtle()
self.color(color)
self.shape('turtle')
self.penup()
x = randint(-75, 75)
y = randint(-75, 75)
self.goto(x, y)
self.onclick(self.on_turtle_clicked)
self.showturtle()
def on_turtle_clicked(self, x, y):
self.goto(95, -122)
print("yes")
screen = Screen()
screen.bgcolor('lightgreen')
print("Welcome! This pond is infested with turtles! Can you catch all the turtles?")
turtles = [MyTurtle(choice(COLORS)) for _ in range(5)]
screen.mainloop()