在海龟比赛中如何让海龟停在特定坐标?
How do you make a turtle stop at certain coordinates in a turtle race?
我正在创建一场海龟比赛,我想让海龟在到达终点线时停下来。
圆圈部分是终点线,位于x坐标220。
到目前为止,这是我的代码:
import turtle as trtl
#add the background
wn = trtl.Screen()
wn.setup(width = 1.0 , height = 1.0)
wn.bgpic("image.png")
# Make the turtle racers
BlueTurtle = trtl.Turtle()
BlueTurtle.shape("turtle")
BlueTurtle.color("blue")
BlueTurtle.speed(4)
BlueTurtle.penup()
BlueTurtle.goto(-220,-0)
BlueTurtle.pendown()
RedTurtle = trtl.Turtle()
RedTurtle.color("red")
RedTurtle.speed(4)
RedTurtle.shape("turtle")
RedTurtle.penup()
RedTurtle.goto(-220,-75)
RedTurtle.pendown()
# Game configuration variables
def move_blue():
BlueTurtle.forward(10)
def move_red():
RedTurtle.forward(10)
# Create numbers and the title
pen = trtl.Turtle()
pen.pensize(10)
pen.speed(10)
pen.speed(20)
pen.penup()
pen.goto(-100, 70)
pen.pendown()
pen.write("Turtle Race", font=("Times New Roman", 40, "normal"))
numbers = ["1","2"]
coordinates = -200,50
for i in range(2):
pen.penup()
pen.goto(coordinates)
pen.pendown()
pen.write(numbers[i])
coordinates = (coordinates[0], coordinates[1] + -100)
# Make the lines that divide the racers and the finish line
pen.penup()
pen.goto(-180, -40)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(-180, 40)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(-180, -110)
pen.pendown()
pen.forward(400)
pen.pensize(0)
pen.penup()
pen.goto(230, 60)
pen.pendown()
pen.right(90)
pen.forward(175)
pen.hideturtle()
# Personalizes it so that the player can move their turtle with any key they like
key1 = input("Type a letter to use to move your turtle - Blue Turtle")
key2 = input("Type a letter to use to move your turtle - Red Turtle")
# Listen for events
wn = trtl.Screen()
wn.onkeypress(move_blue, key1)
wn.onkeypress(move_red, key2)
wn.listen()
# The function for the game to stop if one of the turtles touches the finish line
keepPlaying = True
while keepPlaying:
if BlueTurtle.pos() == (220, 0):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Blue Turtle hit the finish line - the blue turtle wins!" , font =
("Arial", 20, "normal"))
keepPlaying = False
if RedTurtle.pos() == (220, 0):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Red Turtle hit the finsh line = the red turtle wins!" , font =("Arial",
20, "normal"))
keepPlaying = False
wn.mainloop()
最后一个区域写着“如果其中一只乌龟触及终点线,游戏将停止的功能”。是我到目前为止所尝试过的。
它在终端中没有显示任何错误,但是当它被执行时,它似乎总是说“v.s。代码没有响应”当它没有那个部分时它工作正常。
红色从 (-220,-75)
开始,所以 if RedTurtle.pos() == (220, 0):
永远不会为真。但是,除此之外,您正在混合使用事件驱动编程和游戏循环编程。
在
wn.onkeypress(move_blue, key1)
wn.onkeypress(move_red, key2)
wn.listen()
您设置了 2 个事件并告诉应用程序开始侦听这些事件。当一个发生时,将调用相应的函数。
但是这里:
keepPlaying = True
while keepPlaying:
...
你有一个不断检查海龟位置的繁忙循环。但这会阻止到达最后一行 wn.mainloop()
。
您可以将该循环更改为检查海龟是否到达终点线的函数。使用全局变量来标记游戏何时结束:
game_over = False
def move_blue():
if not game_over:
BlueTurtle.forward(10)
check_for_win()
def move_red():
if not game_over:
RedTurtle.forward(10)
check_for_win()
然后,将循环更改为函数
def check_for_win():
global game_over
if BlueTurtle.pos() == (220, 0):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Blue Turtle hit the finish line - the blue turtle wins!" , font =
("Arial", 20, "normal"))
game_over = True
if RedTurtle.pos() == (220, -75):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Red Turtle hit the finsh line = the red turtle wins!" , font =("Arial",
20, "normal"))
game_over = True
还有其他需要改进的地方,但我认为这是你问题的核心。
我正在创建一场海龟比赛,我想让海龟在到达终点线时停下来。
圆圈部分是终点线,位于x坐标220。 到目前为止,这是我的代码:
import turtle as trtl
#add the background
wn = trtl.Screen()
wn.setup(width = 1.0 , height = 1.0)
wn.bgpic("image.png")
# Make the turtle racers
BlueTurtle = trtl.Turtle()
BlueTurtle.shape("turtle")
BlueTurtle.color("blue")
BlueTurtle.speed(4)
BlueTurtle.penup()
BlueTurtle.goto(-220,-0)
BlueTurtle.pendown()
RedTurtle = trtl.Turtle()
RedTurtle.color("red")
RedTurtle.speed(4)
RedTurtle.shape("turtle")
RedTurtle.penup()
RedTurtle.goto(-220,-75)
RedTurtle.pendown()
# Game configuration variables
def move_blue():
BlueTurtle.forward(10)
def move_red():
RedTurtle.forward(10)
# Create numbers and the title
pen = trtl.Turtle()
pen.pensize(10)
pen.speed(10)
pen.speed(20)
pen.penup()
pen.goto(-100, 70)
pen.pendown()
pen.write("Turtle Race", font=("Times New Roman", 40, "normal"))
numbers = ["1","2"]
coordinates = -200,50
for i in range(2):
pen.penup()
pen.goto(coordinates)
pen.pendown()
pen.write(numbers[i])
coordinates = (coordinates[0], coordinates[1] + -100)
# Make the lines that divide the racers and the finish line
pen.penup()
pen.goto(-180, -40)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(-180, 40)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(-180, -110)
pen.pendown()
pen.forward(400)
pen.pensize(0)
pen.penup()
pen.goto(230, 60)
pen.pendown()
pen.right(90)
pen.forward(175)
pen.hideturtle()
# Personalizes it so that the player can move their turtle with any key they like
key1 = input("Type a letter to use to move your turtle - Blue Turtle")
key2 = input("Type a letter to use to move your turtle - Red Turtle")
# Listen for events
wn = trtl.Screen()
wn.onkeypress(move_blue, key1)
wn.onkeypress(move_red, key2)
wn.listen()
# The function for the game to stop if one of the turtles touches the finish line
keepPlaying = True
while keepPlaying:
if BlueTurtle.pos() == (220, 0):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Blue Turtle hit the finish line - the blue turtle wins!" , font =
("Arial", 20, "normal"))
keepPlaying = False
if RedTurtle.pos() == (220, 0):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Red Turtle hit the finsh line = the red turtle wins!" , font =("Arial",
20, "normal"))
keepPlaying = False
wn.mainloop()
最后一个区域写着“如果其中一只乌龟触及终点线,游戏将停止的功能”。是我到目前为止所尝试过的。 它在终端中没有显示任何错误,但是当它被执行时,它似乎总是说“v.s。代码没有响应”当它没有那个部分时它工作正常。
红色从 (-220,-75)
开始,所以 if RedTurtle.pos() == (220, 0):
永远不会为真。但是,除此之外,您正在混合使用事件驱动编程和游戏循环编程。
在
wn.onkeypress(move_blue, key1)
wn.onkeypress(move_red, key2)
wn.listen()
您设置了 2 个事件并告诉应用程序开始侦听这些事件。当一个发生时,将调用相应的函数。
但是这里:
keepPlaying = True
while keepPlaying:
...
你有一个不断检查海龟位置的繁忙循环。但这会阻止到达最后一行 wn.mainloop()
。
您可以将该循环更改为检查海龟是否到达终点线的函数。使用全局变量来标记游戏何时结束:
game_over = False
def move_blue():
if not game_over:
BlueTurtle.forward(10)
check_for_win()
def move_red():
if not game_over:
RedTurtle.forward(10)
check_for_win()
然后,将循环更改为函数
def check_for_win():
global game_over
if BlueTurtle.pos() == (220, 0):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Blue Turtle hit the finish line - the blue turtle wins!" , font =
("Arial", 20, "normal"))
game_over = True
if RedTurtle.pos() == (220, -75):
pen.penup()
pen.goto(-220, -220)
pen.pendown()
pen.write("The Red Turtle hit the finsh line = the red turtle wins!" , font =("Arial",
20, "normal"))
game_over = True
还有其他需要改进的地方,但我认为这是你问题的核心。