使用while循环来控制Python Turtle中的游戏 [英] Use a while loop to control game in Python turtle

查看:42
本文介绍了使用while循环来控制Python Turtle中的游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个随机游戏,其中哪个角色先赢.我基本上已经完成了全部代码,但最终需要帮助,以确定谁首先越过终点线.那我该怎么办呢?我的代码是:

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))

任何使代码更小的建议也将不胜感激.我正在尝试使用 while 循环,以便一旦越过终点线就可以停止游戏.

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.

推荐答案

我们可以测试乌龟的 .xcor()值是否大于终点的x坐标,以查看是否有人赢得了比赛.我已经在下面对您的代码进行了相应的重新整理,同时进行了一些代码压缩(但为了使操作更简单,我没有做太多的事情):

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.

这篇关于使用while循环来控制Python Turtle中的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆