当制作一个永远画图的程序时,我该如何在屏幕上保持绘图?

2024-03-28 18:02:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个相当简单的代码,我在我的空闲时间,但由于它的随机指针总是有机会离开屏幕。我以前看过我的孩子在他们的绘图/精灵中使用Scratch,如果它碰到边缘,它会反弹回来,所以它永远不会离开屏幕。我正在寻找这样的代码,但Python。你知道吗

from turtle import*
from random import*

while True:
    forward(randint(1, 360))
    right(randint(1, 360))

Tags: 代码fromimport绘图屏幕时间孩子空闲
1条回答
网友
1楼 · 发布于 2024-03-28 18:02:38
from turtle import *  
from random import*  

while True:  
    forward(randint(1, 360))
    right(randint(1, 360)) 
    if xcor() < 0:
        setx(20)
    if ycor() < 0:
        sety(20)
    if xcor() > window_width():
        setx(window_width()-20)
    if ycor() > window_height():
        sety(window_height()-20) 

应该管用,20个由你选择。你知道吗

编辑:其中一篇重复文章中的解决方案可能更好

相关问题 更多 >