Python 蟒龟速度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37191039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Turtle Speed
提问by David Bender
My friend and I are working on a Python Game using turtles. Our issue is in the section where it defines the turtles. We are trying to speed each turtle up, but when we do, the default speed runs. Why is this issue occurring?
我和我的朋友正在使用海龟开发 Python 游戏。我们的问题在于它定义海龟的部分。我们试图加快每只海龟的速度,但是当我们这样做时,默认速度会运行。为什么会出现这个问题?
import turtle
turtle.setup(1000,1000)
wn=turtle.Screen()
wn.title("Snake Game!")
#defines first turtle
t1=turtle.Turtle()
t1.pensize(2)
t1.speed(3)
t1.penup()
t1.goto(0,100)
t1.color("blue", "blue")
t1.pendown()
#defines second turtle
t2=turtle.Turtle()
t2.pensize(2)
t2.speed(3)
t2.penup()
t2.goto(0,-100)
t2.color("red", "red")
t2.pendown()
#defines outline
ol=turtle.Turtle()
ol.pensize(2)
ol.speed(7)
ol.penup()
ol.goto(450,0)
ol.color("black", "black")
ol.pendown()
ol.left(90)
ol.forward(300)
ol.left(90)
ol.forward(900)
ol.left(90)
ol.forward(600)
ol.left(90)
ol.forward(900)
ol.left(90)
ol.forward(300)
ol.hideturtle()
#defines score
score1=int(2)
score2=int(2)
def motion():
global move, score1, score2
move = True
path1 = []
path2 = []
#prints score
print("Player 1's score is", str(score1)+"!")
print("Player 2's score is", str(score2)+"!")
#defines motion
while move == True:
global pos1x, pos2x
t1.forward(1)
t2.forward(1)
pos1x = int(t1.xcor())
pos1y = int(t1.ycor())
t1xy = (pos1x, pos1y)
pos2x=int(t2.xcor())
pos2y=int(t2.ycor())
t2xy=(pos2x,pos2y)
path1.append(t1xy)
path2.append(t2xy)
#calculates score1
if t1xy in path2:
score1=int(score1-1)
print("")
print("Player 1's score is", str(score1)+"!")
print("Player 2's score is", str(score2)+"!")
t1.clear()
path1 = []
t2.clear()
path2 = []
t1.penup()
t1.goto(0,100)
t1.pendown()
t2.penup()
t2.goto(0,-100)
t2.pendown()
move = False
if score1==0:
print("Player 2 wins!")
exit()
else:
move==True
#calculates score2
if t2xy in path1:
score2=int(score2-1)
print("")
print("Player 1's score is", str(score1)+"!")
print("Player 2's score is", str(score2)+"!")
t2.clear()
path2 = []
t1.clear()
path1 = []
t2.penup()
t2.goto(0,-100)
t2.pendown()
t1.penup()
t1.goto(0,100)
t1.pendown()
move = False
if score2==0:
print("Player 1 wins!")
exit()
else:
move==True
#borders
if pos1x > 450:
t1.left(135)
if pos2x > 450:
t2.left(135)
if pos1x < -450:
t1.left(135)
if pos2x < -450:
t2.left(135)
if pos1y > 300:
t1.left(135)
if pos2y > 300:
t2.left(135)
if pos1y < -300:
t1.left(135)
if pos2y < -300:
t2.left(135)
#defines controls
def left():
t1.speed(500)
t1.left(45)
t1.speed(3)
def right():
t1.speed(500)
t1.right(45)
t1.speed(3)
def backwards():
t1.left(180)
def stop():
global move
move = False
t1.forward(0)
t2.forward(0)
def left2():
t2.speed(500)
t2.left(45)
t2.speed(3)
def right2():
t2.speed(500)
t2.right(45)
t2.speed(3)
def backwards2():
t2.left(180)
def motion2():
move = True
path1 = []
path2 = []
#onkeys
wn.onkey(left2, "Left")
wn.onkey(right2, "Right")
wn.onkey(backwards2, "Down")
wn.onkey(left, "a")
wn.onkey(right, "d")
wn.onkey(backwards, "s")
wn.onkey(motion, "t")
wn.onkey(stop, "y")
wn.onkey(motion2, "p")
wn.listen()
wn.mainloop()
回答by cdlane
What do you want to happen here?
你想在这里发生什么?
def left():
t1.speed(500)
t1.left(45)
t1.speed(3)
Setting the speed above 10 (fast) sets it to 0 (fastest). And you're setting it to 3 (slow) as soon as the operation is done.
将速度设置为 10(快)以上将其设置为 0(最快)。一旦操作完成,您就将其设置为 3(慢)。
As far as I can tell, you temporarily speed up the turtles on operations that don't get much out of it, e.g. left()
but leave the turtles in slowspeed for operations that should benefit from it, e.g. motion()
据我所知,您暂时加快了海龟的运行速度,但不会从中受益,例如,left()
但让海龟在应该从中受益的操作中保持低速,例如motion()
I'd suggest you pull out all your speed()
calls and rethink them, preferably using the string arguments "slowest", "slow", "normal", "fast" & "fastest" to help document what you're doing and avoid going out of range.
我建议您撤出所有speed()
电话并重新考虑它们,最好使用字符串参数“slowest”、“slow”、“normal”、“fast”和“fastest”来帮助记录您正在做的事情并避免外出的范围。
回答by A Display Name
If it's as simple as changing a turtles speed use:
如果它像更改海龟速度一样简单,请使用:
turtle.speed(number)
Turtle is the turtles name eg. TestTurtle
海龟是海龟的名字,例如。 TestTurtle
The number can be from 1
to 10
and has to be a whole number. You cannot have for example 4.7
as the number.
该数字可以是从1
到10
并且必须是整数。例如4.7
,您不能将其作为数字。
Setting it above 10
will cause the speed to not work.
设置在上面10
会导致速度不起作用。
回答by Yehuda Katz
No point in setting the turtle speed any higher than 10. If you don't use animation, set the speed to 0: speed(0). Otherwise use speed(10).
将海龟速度设置为高于 10 没有任何意义。如果您不使用动画,请将速度设置为 0:speed(0)。否则使用速度(10)。