python turtle循环,使用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))

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.

解决方案

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.

ZMoHK.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值