在 python 海龟中使用多个 While True 循环
Using more than one While True loop in python turtle
我目前遇到一个问题,每次我尝试在我的代码中使用多个 While True 循环时,网站都会崩溃。我正在使用 python turtle 进行编码,这是开始使用 python 进行编码的一种很好的初学者方式。所以基本上我试图让一个乌龟对象无限旋转一个圆圈,直到玩家按下 space 栏。当海龟对象为蓝色时,海龟会被触发旋转。一旦玩家按下按钮,乌龟对象将变为绿色,表示它停止转动并向乌龟对象在按下 space 栏之前所面对的方向向前移动 300 像素。这个小游戏的目标是击中被称为靶子的红球。每次代码运行时,此目标都会在 canvas 上的随机位置生成。一旦乌龟对象与目标发生碰撞,屏幕顶部的分数将从 0 变为 1。
import turtle
import random
t = turtle.Turtle()
target = turtle.Turtle()
scoreBoard = turtle.Turtle()
scoreBoard.ht()
target.ht()
screen = turtle.Screen()
x = random.randint(-300,300)
y = random.randint(-300,300)
scoreNum = 0
turnSpeed = 2
t.shape('turtle')
t.color('blue')
target.speed(0)
target.penup()
target.goto(30,0)
target.shape('circle')
target.color('red')
target.st()
scoreBoard.color('orange')
scoreBoard.speed(0)
scoreBoard.penup()
scoreBoard.goto(-75,310)
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
def turnStop():
t.color('green')
t.forward(300)
def checkCollision():
if abs(t.xcor() - target.xcor()) < 10 and abs(t.ycor() - target.ycor()) < 10:
return True
else:
return False
screen.onkey(turnStop,'space')
screen.listen()
while t.color()[0] == 'blue':
t.right(turnSpeed)
while t.color()[0] == 'green':
if checkCollision():
scoreNum = scoreNum + 1
scoreBoard.clear()
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
如您所见,我有两个 while True 循环。一个用于当玩家在游戏开始时是蓝色的。这允许 turtle 对象永远旋转,直到玩家击中 space 条。另一个 while true 循环用于当玩家通过按下 space 条变为绿色时,检查是否与红球目标发生碰撞。但每次我在 运行 代码后按 space 栏时,网站就会崩溃。有什么想法吗?提前致谢!
P.S我是juni学习的学生。这是一个在线 class,您可以在其中与老师实时学习编码。它带有自己的文本编辑器,所以我一直用它来编码。
P.S P.S 有没有更好的方法将您的代码粘贴到此论坛?我不得不将我的代码粘贴到堆栈溢出并将每行代码缩进 4 次,这有点乏味。
当你有两个 for 循环时,一个将永远不会到达另一个,直到另一个完成。
为什么不在主循环中使用另一个 if 条件?
while t.color()[0] == 'blue':
if t.color()[0] == 'green':
t.right(turnSpeed)
PS:先粘贴,再标记为代码
好吧,我和我的老师解决了我在代码中遇到的问题。我们修复了它,现在它 运行 符合预期。
import turtle
import random
t = turtle.Turtle()
target = turtle.Turtle()
scoreBoard = turtle.Turtle()
scoreBoard.ht()
target.ht()
screen = turtle.Screen()
x = random.randint(-300,300)
y = random.randint(-300,300)
scoreNum = 0
turnSpeed = 2
t.shape('turtle')
t.color('blue')
target.speed(0)
target.penup()
target.goto(30,0)
target.shape('circle')
target.color('red')
target.st()
scoreBoard.color('orange')
scoreBoard.speed(0)
scoreBoard.penup()
scoreBoard.goto(-75,310)
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
def turnStop():
t.color('green')
def checkCollision():
if abs(t.xcor() - target.xcor()) < 10 and abs(t.ycor() - target.ycor()) < 10:
return True
else:
return False
screen.onkey(turnStop,'space')
screen.listen()
while True:
while t.color()[0] == 'blue':
t.right(turnSpeed)
if t.color()[0] == 'green':
for i in range(30):
t.forward(10)
if checkCollision():
x = random.randint(-300,300)
y = random.randint(-300,300)
scoreNum = scoreNum + 1
scoreBoard.clear()
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
target.goto(x,y)
t.penup()
t.goto(0,0)
t.color('blue')
t.pendown()
break
else:
break
基本上我必须在我的 while color is blue 循环中嵌套 while True 循环以及我的条件来检查代码是否为绿色。感谢任何回复的人,感谢他们试图帮助我解决我的问题!非常感谢!
我目前遇到一个问题,每次我尝试在我的代码中使用多个 While True 循环时,网站都会崩溃。我正在使用 python turtle 进行编码,这是开始使用 python 进行编码的一种很好的初学者方式。所以基本上我试图让一个乌龟对象无限旋转一个圆圈,直到玩家按下 space 栏。当海龟对象为蓝色时,海龟会被触发旋转。一旦玩家按下按钮,乌龟对象将变为绿色,表示它停止转动并向乌龟对象在按下 space 栏之前所面对的方向向前移动 300 像素。这个小游戏的目标是击中被称为靶子的红球。每次代码运行时,此目标都会在 canvas 上的随机位置生成。一旦乌龟对象与目标发生碰撞,屏幕顶部的分数将从 0 变为 1。
import turtle
import random
t = turtle.Turtle()
target = turtle.Turtle()
scoreBoard = turtle.Turtle()
scoreBoard.ht()
target.ht()
screen = turtle.Screen()
x = random.randint(-300,300)
y = random.randint(-300,300)
scoreNum = 0
turnSpeed = 2
t.shape('turtle')
t.color('blue')
target.speed(0)
target.penup()
target.goto(30,0)
target.shape('circle')
target.color('red')
target.st()
scoreBoard.color('orange')
scoreBoard.speed(0)
scoreBoard.penup()
scoreBoard.goto(-75,310)
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
def turnStop():
t.color('green')
t.forward(300)
def checkCollision():
if abs(t.xcor() - target.xcor()) < 10 and abs(t.ycor() - target.ycor()) < 10:
return True
else:
return False
screen.onkey(turnStop,'space')
screen.listen()
while t.color()[0] == 'blue':
t.right(turnSpeed)
while t.color()[0] == 'green':
if checkCollision():
scoreNum = scoreNum + 1
scoreBoard.clear()
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
如您所见,我有两个 while True 循环。一个用于当玩家在游戏开始时是蓝色的。这允许 turtle 对象永远旋转,直到玩家击中 space 条。另一个 while true 循环用于当玩家通过按下 space 条变为绿色时,检查是否与红球目标发生碰撞。但每次我在 运行 代码后按 space 栏时,网站就会崩溃。有什么想法吗?提前致谢!
P.S我是juni学习的学生。这是一个在线 class,您可以在其中与老师实时学习编码。它带有自己的文本编辑器,所以我一直用它来编码。
P.S P.S 有没有更好的方法将您的代码粘贴到此论坛?我不得不将我的代码粘贴到堆栈溢出并将每行代码缩进 4 次,这有点乏味。
当你有两个 for 循环时,一个将永远不会到达另一个,直到另一个完成。 为什么不在主循环中使用另一个 if 条件?
while t.color()[0] == 'blue':
if t.color()[0] == 'green':
t.right(turnSpeed)
PS:先粘贴,再标记为代码
好吧,我和我的老师解决了我在代码中遇到的问题。我们修复了它,现在它 运行 符合预期。
import turtle
import random
t = turtle.Turtle()
target = turtle.Turtle()
scoreBoard = turtle.Turtle()
scoreBoard.ht()
target.ht()
screen = turtle.Screen()
x = random.randint(-300,300)
y = random.randint(-300,300)
scoreNum = 0
turnSpeed = 2
t.shape('turtle')
t.color('blue')
target.speed(0)
target.penup()
target.goto(30,0)
target.shape('circle')
target.color('red')
target.st()
scoreBoard.color('orange')
scoreBoard.speed(0)
scoreBoard.penup()
scoreBoard.goto(-75,310)
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
def turnStop():
t.color('green')
def checkCollision():
if abs(t.xcor() - target.xcor()) < 10 and abs(t.ycor() - target.ycor()) < 10:
return True
else:
return False
screen.onkey(turnStop,'space')
screen.listen()
while True:
while t.color()[0] == 'blue':
t.right(turnSpeed)
if t.color()[0] == 'green':
for i in range(30):
t.forward(10)
if checkCollision():
x = random.randint(-300,300)
y = random.randint(-300,300)
scoreNum = scoreNum + 1
scoreBoard.clear()
scoreBoard.write('Score:' + str(scoreNum),font=('Arial',15, 'normal'))
target.goto(x,y)
t.penup()
t.goto(0,0)
t.color('blue')
t.pendown()
break
else:
break
基本上我必须在我的 while color is blue 循环中嵌套 while True 循环以及我的条件来检查代码是否为绿色。感谢任何回复的人,感谢他们试图帮助我解决我的问题!非常感谢!