0

I am trying to draw a rectangle the same size as the screen on the turtle screen, but it goes across the border (is visible only when I enlarge window) but I need it all on screen.

That's how the screen looks

The fragment of code:

WIDTH, HEIGHT = 280, 207

turtle = t.Turtle()
turtle.hideturtle()
turtle.speed(0)
turtle.penup()

turtle_screen = turtle.getscreen()
# coords should be in upper left corner
turtle_screen.setup(width = WIDTH + 20, height = HEIGHT + 20, startx = None, starty = None)
turtle_screen.setworldcoordinates(llx = -1, lly = WIDTH, urx = HEIGHT, ury = -1)

turtle.penup()
turtle.setpos(0, 0)
turtle.color('black', '#dbb4a0')
turtle.pendown()
turtle.begin_fill()
edges = [ WIDTH, HEIGHT, WIDTH, HEIGHT]
for edge in edges:
    turtle.forward(edge)
    turtle.left(90)

turtle.end_fill()
turtle_screen.exitonclick()

I am wondering if the inside screen has different coordinates than the size? Any ideas?

2 Answers2

0

Here is your code corrected :

import turtle as t

WIDTH, HEIGHT = 280, 207

turtle = t.Turtle()
turtle.hideturtle()
turtle.speed(0)
turtle.penup()

turtle_screen = turtle.getscreen()
# coords should be in upper left corner
turtle_screen.setup(width = WIDTH + 20, height = HEIGHT + 20, startx = None, starty = None)
turtle_screen.setworldcoordinates(llx = 0, lly = HEIGHT, urx = WIDTH, ury = 0)

turtle.penup()
turtle.setpos(0, 0)
turtle.color('black', '#dbb4a0')
turtle.pendown()
turtle.begin_fill()
edges = [ WIDTH, HEIGHT, WIDTH, HEIGHT]
for edge in edges:
    turtle.forward(edge)
    turtle.left(90)

turtle.end_fill()
turtle_screen.exitonclick()

You just inverted lly and urx in the setworldcoordinates call. X axis corresponds to your WIDTH, and y axis to your HEIGHT

With this code, I've got this result.

totok
  • 1,436
  • 9
  • 28
0

Here's my similar solution (also corrects HEIGHT and WIDTH inversion in setworldcoordinates()) but based on my earlier answer about working with small windows in turtle:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 280, 207

CHROME = 14
OFFSET = CHROME / -2 + 2

screen = Screen()
# coords should be in upper left corner
screen.setup(WIDTH + CHROME, HEIGHT + CHROME)
screen.setworldcoordinates(llx=0, lly=HEIGHT, urx=WIDTH, ury=0)

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.color('black', '#dbb4a0')

turtle.penup()
turtle.setpos(OFFSET, OFFSET)
turtle.pendown()

turtle.begin_fill()

for _ in range(2):
    turtle.forward(WIDTH)
    turtle.left(90)
    turtle.forward(HEIGHT)
    turtle.left(90)

turtle.end_fill()

screen.exitonclick()

enter image description here

cdlane
  • 40,441
  • 5
  • 32
  • 81