微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以设置海龟相对于屏幕大小的位置

如何解决是否可以设置海龟相对于屏幕大小的位置

假设我的屏幕大小为 10,10

和 2,2 的 pos

如果屏幕大小更改为 5,5

pos 将是 1,1

或者有没有办法改变屏幕分辨率?

解决方法

turtle 会自动设置一个 ScrolledCanvas,因此,调整窗口大小会尝试将事物保持在大致相同的位置,但存在限制。

您可以先将海龟放在成比例的位置,然后从该比例开始绘制。

只需将您的大小和移动视为窗口的分区。使用对角线作为主要维度,然后从那里开始。

这就是可缩放矢量图形的本质 -- https://en.wikipedia.org/wiki/Vector_graphics

#! /usr/bin/python3

from math import sqrt
from random import uniform
from turtle import Turtle,Screen
turtle,screen = Turtle(),Screen()

turtle.penup()
turtle.speed( 0 )

red,green,blue = 0.95,0.95,0.95
screen.bgcolor( red,blue )

##Width,Height = 300,300
Width,Height = 600,600

##  Pythag.  A squared +B squared = C squared
Diag = sqrt( (Width *Width) +(Height *Height) )
stepsize = Diag /100

screen.screensize( Width,Height )  ##  canvas size to begin with

##  window frame.  by adding a few pixels,you avoid scrollbars
screen.setup( width = Width *1.03,height = Height *1.03 )

for i in range( 50 ):  ##  clouds
    light = uniform( 0.8,1 )
    turtle.pencolor( light,light,light )
    turtle.pensize( stepsize *uniform( 0.5,5 ) )
    turtle.setheading( uniform( 0.0,360 ) )
    turtle.goto( Width *uniform( -0.5,0.5 ),Height *uniform( 0.0,0.5 ) )
    turtle.pendown()  
    turtle.forward( stepsize *uniform( 0.1,1 ) )
    turtle.penup()

red,blue = 0.0,0.25,0.5
for i in range( 25 ):  ##  scribble water
    green += 0.01
    blue += 0.02
    water_level = Height *0.15 +(i *stepsize)
    turtle.goto( -Width *0.55,-water_level )
    turtle.pencolor( red,blue )
    turtle.pensize( stepsize +i )
    turtle.pendown()
    turtle.goto( Width *0.55,-water_level )
    turtle.penup()

red,0.95
turtle.goto( Width *0.25,Height *0.333 )
turtle.setheading( 45 )
turtle.pendown()
for i in range( 20 ):  ##  draw sun
    red -= 0.0111
    green -= 0.02
    blue -= 0.0225
    turtle.pencolor( red,blue )
    turtle.pensize( stepsize *(30 -i) )
    turtle.forward( stepsize /5 )

turtle.hideturtle()
screen.exitonclick()

##  eof  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。