screensize of turtle in python - python

I am trying to work with the screen in turtle and am confused about the dimensions of the screen. So in this example:
import turtle
screen = turtle.Screen()
print(screen.screensize())
turtle.done()
Python prints the dimension of the turtle window to be (400,300). However, the screen looks much bigger on the desktop and when I move the turtle by 640 pixels to the right (from the center) or 540 pixels downwards then the edge of the screen is reached. This would indicate that the screensize is 1280 * 1080 pixels.
So my specific questions are:
What information do I get from calling screen.screensize()
When the turtle is moved, is it moved in pixels or is another metric used?
So many thanks in advance!

Let's clear up some misconceptions about turtle window size:
First, the default window you get in standalone turtle is 50% of your display width and 75% of your display height. Which means that not everyone gets the same default window. Something to consider when writing turtle software for others.
You can set the window's size using the setup() method or function. You can get the current window size using the window_width() and window_height() methods or functions.
The screensize() method or function gets/sets the size of the backing store for the window. Generally, the return value is of no use to you, as the area the turtle can travel is the size of the window, so no backing store needed. It's there for folks who, for example, want a 500x500 window onto a 2000x2000 plane that the turtle can wander. Then scrollbars appear to allow you to move that peephole of a window about the larger plane.
You can modify many of turtle's default behaviors with a turtle.cfg file.
You can also find this in the turtle documentation: https://docs.python.org/3/library/turtle.html#screenspecific

Related

Pygame screen loosing click ability when resized

I am making a game using python and pygame. I had a problem a few days ago that I needed to give my games a functionality of being resized and maintain the aspect ratio. Also everything on the screen is resized proportionately. And luckily I got a quick solution to create two different pygame surfaces. One is the screen visible to the user and the other is to manage the blitting functionality. Actually, fake screen has everything blitted and then it itself is blitted to the main screen by using
main_screen.blit((pygame.transform.scale(fake_screen, main_screen.get_rect().size), [0, 0]).
The main problem is that now since the MOUSEBUTTONDOWN events are getting triggered on the main screen and not on fake screen, But
the clicks are getting processed according to the fake screen. This means that when I click on a button after resizing, the button appears to be their but actually its at its respective position on the fake screen. This makes all the buttons loose their functionality after the VIDEORESIZE event. Can anyone help me out with this? I hope that I was able to explain.
Easy answer: use the pygame.SCALED display flag.
It resizes the main screen for you and the mouse events too, without your program needing to know anything about it. Documented on this page: https://www.pygame.org/docs/ref/mixer.html
Using this means you wouldn’t need to use a fake screen at all, or do anything at all with scaling on your end.
DIY answer:
If you still want to control the scaling yourself, you just have to scale the mouse events along with the screen. Like scale then the opposite way you scale the fake screen.
In your case it looks like that would involve dividing the mouse event x by the ratio between fakescreen width and screen width, and same with y (with heights ofc).
I got a very easy solution to this myself. I just after getting mouse x and y coordinates, changed them to proportionately corresponding points. With a simple math. I mean, if x coordinate is 15% of main screen width, then convert it to 15% of fake screen width. This way, the fake screen will get properly scaled coordinates. The mathematical equation can be as follows:-
mouse_x = mouse_x/(xd/100)
mouse_x *= 10
mouse_y = mouse_y/(yd/100)
mouse_y *= 6
Here xd and yd are width and height of the resizable main screen respectively. And 10 and 6 are 1% of 1000 and 600 which are the width and height of the fake screen.
This solved my problem and game is now working perfectly.
Thank You.

how to zoom out on Turtle canvas?

How do I zoom out on the canvas so I'll be able to see everything that is drawn from the turtle in Python. As the name suggests, I use the Turtle from Python to draw a binary tree of certain strings. I could of course force the turtle to draw more narrowly, but then the leaves would be too close to each other. That's why I'm looking for an option to zoom out on the thee when the turtle is done with its drawings.
The screenshot below should show my frustration. As you can see, it is not possible for me to see the whole binary tree.
Have not included the whole code. But let me know if you want to see it. Here is a snippet though.
import turtle
turtle.screensize(5000, 3000)
t = turtle.Turtle()
t.hideturtle()
t.speed(0); turtle.delay(0)
h = height(root)
jumpto(0, 30*h)
draw(root, 0, 30*h, 40*h) #function to draw
t.hideturtle()
turtle.mainloop()

Maximize the pygame window without making it fullscreen

Is there a way to maximize the pygame window when intializing the window with set_mode without making it completely fulsscreen. I tried to get the required window_size by printing event.size, when VIDEORESIZE is called, which is (1920, 1017). But if I use this value for my window_size when setting mode I just get a window at the same size of a maximized window. If I press maximize in the top right corner it just switches between a thicker and a thinner border.
Obviously i want the thinner border, is that possible from the start?
You should try to use the
RESIZEABLE
When using set_mode in pygame like
DISPLAYSURF = pygame.display.set_mode((1920,1017), RESIZABLE)

Why is TKinter restricted to a specific resolution?

I have been using an edited version of the graphics.py library by John Zelle which is based upon TKinter for some time now, but this question always confused me:
Why is the resolution of the screen (the pixels/unit) of the TKinter screen lower than my computer's?
Now what I mean by this is, if I draw a point on window like so:
the pixel is VERY clearly visible. However, I know for a fact that my computer can display higher resolutions (obviously). When I use a vector graphic editing software, I can't make out individual points on the window - so why can I over here. How much ever I have tried increasing the resolution of the window, the pixel size remains the same.
The size is very different to if I used vector graphics - If I had a curve, I would see a curve, not some anti-aliased pixels. So why is this? Why can't I draw smaller pixels on a TKinter screen?
The same thing happens with PNG Images - Why can't I export in a higher resolution so that I don't see any of the anti-aliasing (yes the size would increase, but why isn't the option available), why is this a limitation when I can clearly see that it is not when I am using vector graphics?
Vector graphics can be zoomed in infinitely, but that is not my point. My question is if my computer's screen is capable of displaying far higher resolutions than what I can in TKinter, why can't I display them?

Double the screen resolution in pyglet

So, I'm working on a game using Pyglet for graphics and hardware. The game I am working on requires me to do intensive collision detection using pixels. The game window is created as such:
# Set up buffer variables
bufferedHeight = 256
bufferedWidth = 144
# Create the window
window = pyglet.window.Window(bufferedWidth, bufferedHeight, resizable=True)
I want my game window to increase pixel size accordingly when the window is re-sized. Therefore, the game logic will still think the window is 256 by 145 pixels, however it will display on screen with much larger pixels. How can I go about doing this?
From what I understand, you want BIG pixels to be drawn.
I would suggest rendering your pixel buffer to an openGL texture which could be then in turn rendered to screen and scaled each time the 'resize' event happens..
But you must be aware that the square proprtion of the pixels might not be maintained on resize of the window.

Categories