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

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

Related

.tracer() in python turtle [duplicate]

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.

How can I change the speed that my turtle rotates?

I'm using turtle to draw a massive project but the turning takes years to do.
I have tried the command turtle.speed("fastest") but that only changes distance speed not rotational speed.
By looking at the documentation for turtle, it looks like turtle.speed() controls both speed and rotation:
Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.
Additionally,
speed = 0 means that no animation takes place. forward/back makes turtle jump and likewise left/right make the turtle turn instantly.
Note that turtle.speed("fastest") sets speed = 0. Maybe try changing speed to 10 with just turtle.speed(10) or turtle.speed("fast").
Your current solution sounds like it should work, if you add some example code we might be able to better assist you in finding a solution!
you can use turtle.speed(speed=None)
where speed is an integer in the range 0.. 10 or a speedstring.
if the input is a number greater than 10 or smaller than 0.5, speed is set to 0.Speedstrings are mapped to speed values as follows:
"fastest":0
"fast":10
"normal":6
"slow":3
"slowest":1
speed from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.speed=0 means that no animation takes place.forward/back makes turtle jump and likewise left/right make the turtle turn instantly.
e.g:
turtle.speed()
turtle.speed(9)
turtle.speed('normal')

How to accurately track mouse movements in python, tkinter?

So i am trying to make a simple Drawpad using python and tkinter, and i have pretty much got it working, except there is massive gaps in between the individual dots.
Heres an example
I was wondering if there is a more accurate mouse tracking method? Heres my code for making the dots.
def draw(event):
global DotSize
arr = []
arr.append([event.x, event.y])
for i in arr:
yes=i[0]
no=i[1]
Canvas.create_oval(yes,no,yes,no, fill="black", width=DotSize)
//Theres a load more code in here but i dont think its relevant.
Canvas.bind("<B1 Motion>",draw)
Thanks!
It looks like you are redrawing every pixel every time. First you draw one oval. Then you draw two. then you draw three, and so on. You only need to draw the last item in the array.
As for the accuracy -- what you are doing is the most accurate way to track mouse movement in tkinter. There's nothing more efficient than binding to <B1-Motion>.

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.

Categories

Resources