问题
I am trying to make a random game in which whichever character reaches the end first wins. I have basically the entire code made, but I need help with the end to determine who crosses the finish line first. So how would I do that? My code is:
from turtle import Turtle
from random import randint
t = Turtle()
t.speed(0)
t.up()
t.goto(-200,0)
t.down()
t.forward(900)
t.up()
t.goto(-200,100)
t.down()
t.forward(900)
t.up()
t.goto(-200,200)
t.down()
t.forward(1000)
t.up()
t.goto(-200,-100)
t.down()
t.forward(900)
t.up()
t.goto(-200,-200)
t.down()
t.forward(1000)
t.up()
t.goto(-200,200)
t.down()
t.right(90)
t.forward(400)
t.left(90)
t.up()
t.goto(-100, -200)
t.left(90)
t.down()
t.forward(400)
t.up()
t.goto(800,-200)
t.down()
t.forward(400)
t.up()
t.goto(700,-200)
t.down()
t.forward(400)
d = Turtle()
d.speed(5)
d.color('red')
d.shape('arrow')
d.up()
d.goto(-155,150)
d.right(360)
c = Turtle()
c.speed(5)
c.color('blue')
c.shape('turtle')
c.up()
c.goto(-155,50)
c.right(360)
b = Turtle()
b.speed(5)
b.color('yellow')
b.shape('arrow')
b.up()
b.goto(-155,-50)
b.right(360)
a = Turtle()
a.speed(5)
a.color('green')
a.shape('turtle')
a.up()
a.goto(-155,-150)
a.right(360)
for i in range(350):
a.forward(randint(1,6))
b.forward(randint(1,6))
d.forward(randint(1,6))
c.forward(randint(1,6))
Any suggestions to make the code smaller would also be appreciated. I am trying to use a while
loop so that I can stop the game as soon as the finish line is crossed.
回答1:
We can test if a turtle's .xcor()
value is greater than the x coordinate of the finish line to see if someone has won the race. I've reworked your code accordingly below along with some code compacting (but not as much as I could do, just to keep things nicely simple):
from turtle import Screen, Turtle
from random import randint
START_LINE = -300
FINISH_LINE = 300
screen = Screen()
screen.setup(1000, 600)
t = Turtle(visible=False)
t.speed('fastest')
# Race Lanes
for y in range(-200, 300, 100):
t.up()
t.goto(START_LINE - 100, y)
t.down()
t.forward((FINISH_LINE + 90) - (START_LINE - 100))
# Starting and Finishing Gates
for x in [START_LINE - 100, FINISH_LINE - 10]:
t.up()
t.goto(x, 200)
t.right(90)
t.down()
t.forward(400)
t.left(90)
t.forward(100)
t.left(90)
t.forward(400)
t.right(90)
d = Turtle('arrow')
d.color('red')
d.speed(5)
d.up()
d.goto(START_LINE - 50, 150)
d.right(360)
c = Turtle('turtle')
c.color('blue')
c.speed(5)
c.up()
c.goto(START_LINE - 50, 50)
c.right(360)
b = Turtle('arrow')
b.color('yellow')
b.speed(5)
b.up()
b.goto(START_LINE - 50, -50)
b.right(360)
a = Turtle('turtle')
a.color('green')
a.speed(5)
a.up()
a.goto(START_LINE - 50, -150)
a.right(360)
while a.xcor() < FINISH_LINE and b.xcor() < FINISH_LINE and c.xcor() < FINISH_LINE and d.xcor() < FINISH_LINE:
a.forward(randint(1, 6))
b.forward(randint(1, 6))
c.forward(randint(1, 6))
d.forward(randint(1, 6))
# The race is over
screen.mainloop()
It's good to define key locations with variables so that you don't have to work out numbers for every step in the code -- you can instead calculate and adjust as needed.
来源:https://stackoverflow.com/questions/54103580/use-a-while-loop-to-control-game-in-python-turtle