【问题标题】:Finding size of turtle screen in replit在 repli 中查找海龟屏幕的大小
【发布时间】:2020-07-11 02:26:43
【问题描述】:

我正在尝试在 repli 中找到海龟屏幕的大小,以便我可以知道海龟在哪里越界。

这是我的代码:

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 更简单的方法,请在 cmets 中告诉我!

【问题讨论】:

标签: python python-3.x turtle-graphics


【解决方案1】:

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

from turtle import Screen

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

Repl 控制台输出:

Starting X
.....400 450
> 

【讨论】:

  • 谢谢!我不知道!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多