首页 > 解决方案 > 在 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 更简单的方法,请在评论中告诉我!

标签: pythonpython-3.xturtle-graphics

解决方案


即使在 Repl.it 中,您也可以询问海龟本身:

from turtle import Screen

screen = Screen()
print(screen.window_width(), screen.window_height())

复制控制台输出:

Starting X
.....400 450
> 

推荐阅读