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()
Related
I'm making a platformer game with python turtle, the game not built for high graphics any way, the rendering created with pen, but I still want a way to make the pen less pixelated,
I tried a lot of things like using tkinter root.tk.call('tk', 'scaling', 2.0) and ctypes.windll.shcore.SetProcessDpiAwareness(1)
but it just don't work on the turtle pen graphics.
example:
from turtle import *
wn = Screen()
wn.colormode(255)
pen = Turtle()
pen.speed(0)
pen.ht()
pen.pu()
pen.pensize(15)
pen.pencolor(0, 255, 255)
pen.setpos(-150, -50)
pen.pd()
pen.setpos(150, 50)
wn.mainloop()
So that code draws a diagonal line, but as you can see it is too pixelated, and I guess it has resolution limit (not sure):
screen shot from the code up
screen shot of the life bar from my own game when using 1920x1080 resolution and it still seems like 720p not anti aliased
so if you know any way to do it, really thanks, if you can't find a way to do it through turtle or tkinter, or any other trusted module, I don't even care if I have to edit turtle for the solution.
Is there a way to export a python turtle drawing to a video?
The video should contain the real-time drawing process.
In particular I'm not interested in a screen-recording workaround, but a solution that would work in a headless system like a cloud environment.
Here's an example of a turtle drawing
import turtle
tr = turtle.Turtle()
rad = 80
tr.circle(rad)
turtle.done()
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.
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
I'm using turtle to make a little game and realized I could use image files with turtle.registershape(filename). I know you can resize the default shapes with turtle.shapesize or turtle.resizemode("auto") and changing pensize, but is there any way to resize a gif file using these methods?
import turtle
turtle.addshape("example.gif")
t = turtle.Turtle()
t.shape("example.gif")
t.resizemode("auto")
t.pensize(24)
t.stamp()
turtle.exitonclick()
I want something like this to work, but the turtle is displayed normally, not resized.
I reviewed the applicable turtle code and I believe the answer is, "No, not within turtle itself."
Bringing in tkinter, which underlies turtle, gives us some limited (integral) turtle image expansion and reduction capability:
from tkinter import PhotoImage
from turtle import Turtle, Screen, Shape
screen = Screen()
# substitute 'subsample' for 'zoom' if you want to go smaller:
larger = PhotoImage(file="example.gif").zoom(2, 2)
screen.addshape("larger", Shape("image", larger))
tortoise = Turtle("larger")
tortoise.stamp()
tortoise.hideturtle()
screen.exitonclick()
If you want more flexibility, the standard approach seems to be to either resize the graphic outside of turtle/tkinter or use the PIL module to resize the graphic dynamically and hand it to turtle/tkinter.
The answer is no. I went through the source code of cpython library and tracked to the draw image method call. See _drawturtle method of cpython library, where drawImage method is called with no information of variables like pensize or shapesize. I agree with #cdlane that you will have to use other resize using other libraries, however one tip, if you know beforehand the set of image sizes you will need, you can generate them and load during the game start instead of resizing at the runtime.
No, there is no way to resize gif's shape via turtle explicitly. However, you can resize the shape manually using this website: GIF RESIZER
Further, use the optimized gif (length & width wise) in your code. I also use this approach and is very helpful.
Thanks!