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

时间:2021-05-19 18:46:29

标签: python turtle-graphics python-turtle

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

和 2, 2 的 pos

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

pos 将是 1, 1

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

1 个答案:

答案 0 :(得分:0)

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, green, 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, green, 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, green, blue )
    turtle.pensize( stepsize +i )
    turtle.pendown()
    turtle.goto( Width *0.55, -water_level )
    turtle.penup()

red, green, blue = 0.95, 0.95, 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, green, blue )
    turtle.pensize( stepsize *(30 -i) )
    turtle.forward( stepsize /5 )

turtle.hideturtle()
screen.exitonclick()

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