我和我的朋友正在学校编写练习,我们遇到了如何让海龟保持正轨的困难。为了说明这个广场的第一面,两只乌龟正在两侧比赛。但是,当它们转90度时,其中1或2个会脱离侧面,这对于运动要求是不正确的。
这是我们的代码:
import turtle
from random import randint
def read_int(prompt,first,last):
x = int(input(prompt))
while x < first or x > last:
print("Not in range. Try Again!!!")
x= int(input(prompt))
return x
square_count = read_int("Enter your laps between 1 and 10: ",1,10)
print(square_count)
#def t():
window = turtle.Screen()
window.bgcolor('lightblue')
def draw_square(turtle, center, size):
xPt, yPt = center
xPt -= size / 2
yPt += size / 2
side = 4
size = 300
angle = 90
turtle.speed(0)
turtle.up()
turtle.goto(xPt, yPt)
turtle.down()
for i in range(side):
turtle.forward(size)
turtle.right(angle)
t = turtle.Turtle()
draw_square(t,(0,0),300)
t.shape('turtle')
t.color("red")
t.pensize(5)
t.up()
t.goto(-150, 150)
#def r():
r = turtle.Turtle()
draw_square(r,(0,0),300)
r.shape('turtle')
r.color("yellow")
r.up()
r.pensize(5)
r.goto(-150, 150)
sides = 4
size = 300
count_int = int(square_count)* sides
if count_int > 1:
for sides in range(count_int):
i = 0
e = 0
while i in range(0, size) or e in range(0, size):
t_step = randint(1, 5)
t.forward(t_step)
i = i + t_step
r_step = randint(1, 5)
r.forward(r_step)
e = e + r_step
t.right(90)
r.right(90)
window.exitonclick()
答案 0 :(得分:0)
你的错误在于程序的结束。
while i in range(0, size) or e in range(0, size):
t_step = randint(1, 5)
t.forward(t_step)
i = i + t_step
r_step = randint(1, 5)
r.forward(r_step)
e = e + r_step
在假设i = 298的情况下,t_step选择一个随机整数为3.乌龟最终会移动大于300的范围,从而移动到绘制的框之外。 循环不会停止,因为它只会在运行所有代码时意识到i超出范围。 (即,在环断裂时,乌龟已经移出了盒子。)这就是为什么乌龟出门了。您可以尝试使用“if”条件来阻止这种情况。
答案 1 :(得分:0)
你有两种问题。
首先,你应该注意你的乌龟采取多少步骤。因为如果一个正方形的大小为300,那么乌龟可能会采取一定数量的步骤,其总和不等于300.我建议您将随机步骤的生成更改为:
steps = random.randrange(0, 4, 2)
通过这种方式,乌龟只需要0步或2步,你确定总和会带你到300.
第二点可能更重要。如果你像你一样构建种族循环,只有当另一只乌龟到达一条末尾时,乌龟才会转动,你应该重新组织你的代码,使两只乌龟独立于另一只。
答案 2 :(得分:0)
我会采取略微不同的方法 - 而不是硬编码两只乌龟,设计任意数量的海龟,然后只比赛两只乌龟。这是基于任意数量的海龟的重写,包括一些样式代码的返工,以及@Claudio的一些代码建议:
from random import randint
from turtle import Turtle, Screen
from collections import defaultdict
SIZE = 300
SIDES = 4
ANGLE = 90
MAXIMUM_STRIDE = 5
COLORS = ('red', 'gold', 'green', 'orange', 'blue')
MAX_RACERS = len(COLORS)
def read_int(prompt, first, last):
x = int(input(prompt))
while not first <= x <= last:
print('Not in range! Try Again.')
x = int(input(prompt))
return x
def draw_square(turtle, center, size):
xPt, yPt = center
xPt -= size / 2
yPt += size / 2
turtle.up()
turtle.goto(xPt, yPt)
turtle.down()
for _ in range(SIDES):
turtle.forward(SIZE)
turtle.right(ANGLE)
lap_count = read_int('Enter number of laps (between 1 and 10): ', 1, 10)
no_racers = read_int('Enter number of racers (between 2 and {}): '.format(MAX_RACERS), 2, MAX_RACERS)
jockey_colors = COLORS[0:no_racers]
window = Screen()
window.bgcolor('lightblue')
racers = defaultdict(dict)
draw_square(Turtle(visible=False), (0, 0), SIZE)
for color in jockey_colors:
jockey = Turtle('turtle', visible=False)
jockey.speed('fastest')
jockey.color(color)
jockey.up()
jockey.goto(-SIZE/2, SIZE/2)
jockey.showturtle()
racers[color]['jockey'] = jockey
racers[color]['sides'] = 0
racers[color]['position'] = 0
finished = False
while not finished:
for racer in racers.values():
jockey = racer['jockey']
step = randint(1, MAXIMUM_STRIDE + 1)
if racer['position'] + step > SIZE:
racer['sides'] += 1
if racer['sides'] == lap_count * SIDES:
finished = True
break
baby_step = SIZE - racer['position']
jockey.forward(baby_step)
jockey.right(ANGLE)
racer['position'] = 0
step -= baby_step
jockey.forward(step)
racer['position'] += step
window.exitonclick()
答案 3 :(得分:-1)
你在这里:
import turtle
from random import randint
def read_int(prompt,first,last):
x = int(input(prompt))
while x < first or x > last:
print("Not in range. Try Again!!!")
x= int(input(prompt))
return x
no_of_laps = read_int("Enter your laps between 1 and 10: ",1 , 10)
print(no_of_laps)
window = turtle.Screen()
window.bgcolor('lightblue')
def draw_square(turtle, center, size):
xPt, yPt = center
xPt -= size / 2
yPt += size / 2
side = 4
size = 300
angle = 90
turtle.speed(0)
turtle.up()
turtle.goto(xPt, yPt)
turtle.down()
for i in range(side):
turtle.forward(size)
turtle.right(angle)
t = turtle.Turtle()
draw_square(t,(0,0),300)
t.shape('turtle')
t.color("red")
t.pensize(5)
t.up()
t.goto(-150, 150)
#def r():
r = turtle.Turtle()
draw_square(r,(0,0),300)
r.shape('turtle')
r.color("yellow")
r.up()
r.pensize(5)
r.goto(-150, 150)
sides = 4
size = 300
no_of_turns = int(no_of_laps) * sides
countTurns_t = 0
countTurns_r = 0
t_i = 0
r_i = 0
while countTurns_t < no_of_turns and countTurns_r < no_of_turns:
t_step = randint(1, 5)
if t_i + t_step > size:
t_step = size - t_i
t.forward(t_step)
t_i = 0
t.right(90)
countTurns_t += 1
else:
t.forward(t_step)
t_i += t_step
r_step = randint(1, 5)
if r_i + r_step > size:
r_step = size - r_i
r.forward(r_step)
r_i = 0
r.right(90)
countTurns_r += 1
else:
r.forward(r_step)
r_i += r_step
window.exitonclick()
P.S。希望您能看到上面提供的代码如何以及为什么按预期工作。仔细阅读其他答案,他们指出了编写代码时遇到的重要问题。