.tracer() in python turtle [duplicate] - python

I am currently learning about the turtle module in Python. So I read the function of tracer() in this link, but I haven't understood what does n-th regular screen update actually means. If for example, I set screen.tracer(0) or screen.tracer(2, 100) what do they actually do?

The turtle was originally a small programmable physical robot that
carried a pen and could trace its path as it moved.
From https://compform.net/turtles
Generally, the computer can draw graphics instantaneously. Displaying every drawing update on the screen, and slowing down these updates, so we can see them, is tracing in turtle graphics.
The tracer() function turns automatic screen updates on or off -- on by default -- and also sets the update() delay. In Python 2, the first argument to tracer() is boolean, True to have automatic screen updates on, False to turn them off. You can still use tracer() this way in Python 3, and it's the way I most commonly use it.
In Python 3, the first argument, n, is a number. If n is 0 (zero), automatic screen updates are off. If n is 1 (one), the default, automatic screen updates will happen. This matches the Python 2 model. When automatic updates are off, you need to explicitly call update() when you want the screen to reflect the current state of the drawing.
But in Python 3, if n is greater than 1 (one), then only every nth automatic screen update will occur. If n is 2, only every other screen update will actually happen. However, there's a glitch:
As I discuss in my tracer() rules of thumb, some Python turtle graphics operations force an update() regardless of tracer() settings. Due to this, and other turtle underpinnings, calculating the proper nth to set n, is error prone. So my recommendation is to stick with the Python 2 model, and ignore this feature.
Finally, the second, delay, argument to tracer(), is the time delay added after an update() to allow users to see the change, before something else gets updated. The default value for this is 10 milliseconds, which is fairly short. This is similar to turtle.speed(), but affects everything, not just an individual turtle.

Related

How do you rotate an image in Pygame without constantly redrawing the background?

I am making a randomized spinner in pygame, but the only method I've found is to constantly redraw the whole screen. Is there a way that doesn't use this, it becomes extremely laggy.
You can not. why do you want that? It is common to redraw the entire scene in each frame. Of course you can try to redraw just a rectangular section of the background. However, this is usually not worth the effort.
The pygame way to do this is to pass a list of rectangular areas to pygame.display.update(). If rectangles are passed to pygame.display.update() then not the entire display will be redrawn, just the specified areas::
Update portions of the screen for software displays
[...]
You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles.
e.g.:
while run:
# [...]
screen.blit(background, (0, 0))
screen.blit(image1, bounding_rect1)
screen.blit(image2, bounding_rect2)
pygame.display.update([bounding_rect1, bounding_rect2])
That is how computer animation works, you're redrawing the screen on every frame. Every game practically works the same way. I am not sure what graphics pipeline pygame uses but in OpenGL you usually initialize all your geometry outside of the render loop and then use pointers to batch-process everything in the render-loop so you're not duplicating a lot of boilerplate code or making duplicate objects every frame.
A related question has been answered here:
https://gamedev.stackexchange.com/questions/125546/what-is-the-best-way-to-handle-the-actual-render-loop

How to draw the whole picture at once in Python using turtle?

I am making an analogue clock in Python using turtle. It has to be renewed every t seconds. For that, I am redrawing it every t seconds. I need it to be redrawn immediately (now it places all the elements one by one taking more than my t seconds). How can I do that? Actually, it does not have to be redrawn every t seconds, it's just the hands that have to move. Is there any other, easier way?
I have tried making speed 0 but that does not help. Maybe, there are some other ways to make the hands move?
turtle.reset()
turtle.speed(0)
while True:
turtle.reset()
clock_face.draw()
hour_hand.showCurrentTime()
minute_hand.showCurrentTime()
second_hand.showCurrentTime()
turtle.up()
time.sleep(t)
You can use turtle.tracer(0, 0) which will turn off animation and should significantly speed up your animation. If you decide to turn off animation, you will need to use turtle.update() at the end of your code.
However if you want it to animate every so often, the first parameter is some n value that will animate the n-th animation, and the second is a delay.
Some people have gotten things working very fast here:
How to speed up python's 'turtle' function and stop it freezing at the end

python variable not being reset

I'm currently making a game with pygame. I have one issue with my game at the moment.
The scrolling of the screen is fine, but once the image has been scrolled completely after its second time (it works fine first time) the screen blit goes all weird, all the sprites in the game leaves massive trails behind where it was previously (check screnshots). This is simply because for some stragnge reason the variable "x" is not being reset back to 0 once it has exceeded the screenwidth after the second time, it resets after it equal 1384 the first time but not after the second time..
any help is greatly appreciated.
http://pastebin.com/ub6gi8Zn (pastebin code gone)
Blit working fine before background has repeated itself twice SCREENSHOT
gyazo.com/aa5626d4927b0b9299ce2ec42c9ba501 -- after the background repeating itself twice-- sorry couldn't add more than 2 links
The problem is that you aren't clearing the screen buffer.
That is, the sprites and background just get redrawn over themselves because you haven't erased the pixels of the last time you drew those sprites on the screen!
I have never used pygame before but this is a general graphics programming problem, I tried looking up and it seems the function you are looking for is screen.fill(some_color). You fill the entire screen with a given color so the output of the last drawn frame disappear.
By the way this also happens because your "background" doesn't fill the entire area of the screen.

Beginner problem with pygame

I'm just beginning a very simple pygame code that draws a green line on a white background. However, I have to use pygame.display.flip() in order for it to show up, but it seems that I would have to use this every time I made a change and this seems too impractical for me to be doing it right. Am I missing something?
There's a good reason for this. Double buffering is a technique used to prevent "flickering". Basically, you want to draw a frame to memory instead of to the monitor and then push the frame all at once when its drawn. Otherwise, you can see different elements of the same frame go up at different times.
What you want to do is call pygame.display.flip() only once per frame draw, not after every change. Usually, this is done by having a "draw" function that is called at the end of a frame once the physics manipulations and game rules are done, and flip()ing at the end of draw.
Yes, you will have to call it each time you want to update screen. By default pygame uses double buffering Wiki link.

Creating a Selection tool using PyGtk

Can anyone help or point me in the right direction for figuring out how to create a drag and draw rectangular box to be used as a selection tool in PyGtk? I am presently using an event box with a drawable window and the user can click once in the upper left and once in the lower right corner of the portion of image they would like to choose which will then draw a rectangle over the selection, but a drag and draw rectangle will allow the user to better adjust and get better accuracy.
I have looked quite a few places for information or a tutorial on this but I haven't found much. I am relatively new to Gtk+ so perhaps this is so simple that no one has to ask.
Actually, this doesn't seem all that lamebrained at all. It is actually quite specific, and a little challenging.
I'll give you the steps to start you off, but as you're beginning (and you didn't post any specific code), it would be better for you to create the code yourself based on documentation and my hints.
By the way, look up the official PyGTK documentation - that should be your definitive source for all the objects and functions of PyGTK. It is very well written and exhaustive, and I rarely have to look more than five minutes to find what I need.
What I suggest you do is use three signals, connected to your drawing area.
button-press-event
button-release-event
motion-notify-event
Create three callbacks (tutorial here), one for each event. Connect your drawing area to your events and callbacks (again, see tutorial. You may need to go through a few pages on it.).
You are going to also need to create two boolean variables on the global level (above the main class, at the same level you import modules.) The first controls whether the selection tool is chosen (call it "Select_On"), and the second for if it is active (call it "Select_Active")
On the button you use to start the select tool, set "Select_On" to "True". This should probably be a toggle button, so make sure you set it up so "Select_On" gets set to off if you toggle the button off.
On button-press-event, create the object for selecting. What you're going with now actually should work well. Also, set "Select_Active" to "True".
On motion-notify-event, change the size of your object based on cursor position. Refer to that documentation for that particular kind of object to learn how to change its size, and refer here for how to get the cursor position.
Be prepared to write an algorithm to determine how to change the size of the selection object based on the cursor position. If you need help with that, feel free to ask for it in a separate question.
On button-release-event, set "Select_Active" to "False", and call all your code for actually confirming the selection.
As an aside, the benefit to using the "motion-notify-event" is that, as soon as the cursor leaves the widget you're selection in, the selection box stops changes sizes. The cursor must re-enter the widget to continue changing the selection box size.
I hope all that works for you, and wishing you the very best on your project!

Categories

Resources