python - 在 repli 中查找海龟屏幕的大小
问题描述
我试图在 replit 中找到海龟屏幕的大小,以便我可以知道海龟在哪里超出了界限。
这是我的代码:
import turtle as t
from random import randint as r
import sys
window = t.Screen()
pet = t.Turtle()
def rand_color():
red = r(0, 255)
green = r(0, 255)
blue = r(0, 255)
pet.color(red, green, blue)
def random_turtle(num_moves):
global count
while count < num_moves:
rand_color()
pet.forward(r(10, 100))
check()
pet.right(r(0, 360))
check()
count += 1
def check():
global count
x = t.xcor()
y = t.ycor()
if x > 350 or x < -350:
t.right(180)
if count > 100:
sys.stop()
else:
random_turtle(100 - count)
elif y > 330 or y < -320:
t.right(180)
if count > 100:
sys.stop()
else:
random_turtle(100 - count)
else:
random_turtle(100 - count)
如果有比找到海龟屏幕的大小并将 x 除以 2 更简单的方法,请在评论中告诉我!
解决方案
即使在 Repl.it 中,您也可以询问海龟本身:
from turtle import Screen
screen = Screen()
print(screen.window_width(), screen.window_height())
复制控制台输出:
Starting X
.....400 450
>
推荐阅读
- java - 带有长度比较器错误的Java TreeSet?
- powershell - O365 移动设备导出
- .net - 在 chrome 上下载我的新程序危险吗?
- javascript - 如何根据事件重新运行脚本和更新
- javascript - javascript 函数仅适用于第一个包含的函数
- bash - bash中的echo和return有什么区别?
- c++ - 不知道为什么 extern 不起作用
- inno-setup - 如果用户不将“Enter”替换为其他任何内容,则 Inno Setup 禁用下一个按钮
- google-apps-script - 在 Google 表格上,我想创建一个按钮,单击时充当链接
- ios - TableView 根本不会显示数据