python - 如何在不使用 turtle.mainloop() 的情况下停止海龟冻结?
问题描述
当我要求用户输入海龟时,海龟页面会冻结。
我尝试过使用turtle.mainloop()
,但这只是完全停止了程序。
有没有办法可以防止海龟页面冻结和程序停止?
这是代码:
import turtle
import time
import random
def quickshape(sides,size,thickness):
int(thickness)
turtle.width(thickness)
int(sides)
int(size)
angle = 360 / sides
int(angle)
for i in range(sides):
turtle.forward(size)
turtle.right(angle)
def correct_position():
global x
global y
turtle.penup()
x = random.randint(-400,400)
y = random.randint(-250,250)
turtle.setpos(x, y)
def move_alot():
global x
global y
turtle.penup()
x += random.randint(-100,100)
y += random.randint(-100,100)
goes = 1
print("My Doodle .co can draw you lots of shapes, depending on what you ask us!")
print("To make a new set of patterns, close the pattern page... but only when it is drawing.")
print()
print("Before we start, do you want day or night mode?")
while True:
mode = input("Mode: ")
if mode == 'day' or mode[0] == 'd':
print("Day mode it is.")
mode = 'day'
break
elif mode == 'night' or mode[0] == 'n':
print("Night mode it is.")
mode = 'night'
break
else:
print("Hmm... which mode?")
titlename = input("Title: ")
print("Configuring - takes a couple of seconds...")
while True:
turtle.ht()
turtle.tracer(0)
turtle.screensize()
turtle.setup(width = 1.0, height = 1.0)
turtle.title(titlename + " [waiting for input - do not close]")
print("[ pattern",goes,"]")
while True:
try:
sides = int(input("Sides: "))
except ValueError:
print("[Input Error]")
else:
if sides > 35:
print("Warning: >35 sides")
break
try:
turtle.penup()
x = random.randint(-200,200)
y = random.randint(-100,100)
turtle.setpos(x, y)
while True:
try:
numshapes = int(input("Amount: "))
except ValueError:
print("[Input Error]")
else:
break
while True:
try:
size = int(input("Length of each side: "))
except ValueError:
print("[Input Error]")
else:
break
while True:
try:
thickness = int(input("Thickness: "))
except ValueError:
print("[Input Error]")
else:
break
if mode == 'day':
if goes == 1:
print("Setting up the day...")
turtle.bgcolor("white")
else:
if goes == 1:
print("Setting up the night...")
turtle.bgcolor("black")
print("We are now drawing...")
turtle.width(thickness)
part = 1
int(part)
for part in range(numshapes):
turtle.penup()
viewpart = part + 1
int(viewpart)
percentage = round(int(viewpart)/int(numshapes) * 100,2)
turtle.title(titlename+" [drawing pattern. "+str(percentage)+"% complete.]")
R = random.random()
G = random.random()
B = random.random()
turtle.color(R, G, B)
x += random.randint(-10,5)
y += random.randint(-5,10)
if int(turtle.xcor()) > 650 or int(turtle.xcor()) < -650 or int(turtle.ycor()) > 500 or int(turtle.ycor()) < -500:
correct_position()
else:
if random.randint(1,45) == 1:
move_alot()
turtle.forward(x)
turtle.left(y)
turtle.pendown()
quickshape(sides,size,thickness)
turtle.update()
print("Finished drawing pattern",goes)
turtle.title(titlename + " [finished drawing pattern " + str(goes) + "]")
except turtle.Terminator:
print("The page was closed when we were drawing.")
print("Reseting number of patterns...")
turtle.title(titlename + " [reseting...]")
goes = 0
print()
else:
print()
goes +=1
解决方案
这是代码 - 但它对这个问题不太重要。
代码对于问题总是很重要!
有足够的代码,而且问题相当模糊,我将尝试将您的代码翻译成 Python 的方法 ;-),让您看看您的问题是否仍然存在:
from turtle import Screen, Turtle
from random import random, randint
def quickshape(sides, size, thickness):
turtle.width(thickness)
angle = 360 / sides
for _ in range(sides):
turtle.forward(size)
turtle.right(angle)
def correct_position():
global x, y
x = randint(-400, 400)
y = randint(-250, 250)
def move_alot():
global x, y
x += randint(-100, 100)
y += randint(-100, 100)
print("My Doodle .co can draw you lots of shapes, depending on what you ask us!")
print()
print("Before we start, do you want day or night mode?")
goes = 1
while True:
mode = input("Mode: ")
if mode[0].lower() == 'd':
print("Day mode it is.")
mode = 'day'
break
if mode[0].lower() == 'n':
print("Night mode it is.")
mode = 'night'
break
print("Hmm... which mode?")
titlename = input("Title: ")
print("Configuring - takes a couple of seconds...")
screen = Screen()
screen.tracer(False)
screen.setup(width=1.0, height=1.0)
if mode == 'day':
screen.bgcolor("white")
else:
screen.bgcolor("black")
turtle = Turtle()
turtle.hideturtle()
while True:
screen.title(titlename + " [Waiting for input - do not close]")
pattern = "[Pattern {}]".format(goes)
sides = screen.numinput(pattern, "Number of sides:", default=6, minval=3, maxval=35)
if sides is None:
break
sides = int(sides) # numinput() returns float
numshapes = screen.numinput(pattern, "Number of shapes:", default=3, minval=1, maxval=50)
if numshapes is None:
break
numshapes = int(numshapes)
size = screen.numinput(pattern, "Length of each side:", default=25, minval=5, maxval=500)
if size is None:
break
thickness = screen.numinput(pattern, "Thickness of pen:", default=1, minval=1, maxval=10)
if thickness is None:
break
x = randint(-200, 200)
y = randint(-100, 100)
for part in range(1, numshapes + 1):
turtle.penup()
percentage = round(part/numshapes * 100, 2)
screen.title(titlename + " [Drawing pattern. " + str(percentage) + "% complete.]")
turtle.color(random(), random(), random())
if randint(1, 45) == 1:
move_alot()
else:
x += randint(-10, 5)
y += randint(-5, 10)
if not (-650 < x < 650 and -500 < y < 500):
correct_position()
turtle.setposition(x, y)
turtle.pendown()
quickshape(sides, size, thickness)
screen.title(titlename + " [Finished drawing pattern " + str(goes) + "]")
screen.update()
goes += 1
screen.mainloop()
我遗漏了这个功能:
要制作一组新图案,请关闭图案页面……但仅在绘图时。
因为一旦关闭窗口,就无法再次启动 Python turtle。
推荐阅读
- angular-material - 如何在 Mat 日期选择器中隐藏日历标题 (calendarHeaderComponent)
- java - Java解决骑士之旅需要很长时间
- powershell - 如何检查 OU 中的所有组是否已启用成员
- javascript - 为什么我将它以 json 格式发送到 app.post url 时没有得到值?
- python - 为什么 pygame 滞后以及如何解决?
- javascript - 如何使用 jsearch.value 在 MarkLogic 中进行非通配符搜索?
- typescript - 打字稿-console.log 不显示对象属性
- sftp - 如何使用 bash 从 SFTP 下载最新文件(连接使用密钥文件和密码)
- rdf - 如何检查默认凭据以首次登录 anzograph?
- pandas - 如何在熊猫中创建多列数据透视表?