Creating a Simple Timer in Python and Using it with Programs in turtle


Creating Timer in Python

Creating a simple timer in Python for games or animations is not too difficult. In this article you will learn how to create and use a timer. You will:

  1. Understand the difference between a Console and an Editor when writing Python code

  2. Learn about the time module and the time.time() function

  3. Find out how to start and stop a timer and make things happen when a certain amount of time has passed

Getting Started

You will use a module called time. Recall that a module is what we import in a program. You can think of this as fetching a book from the library that contains useful commands. In this case the book is called time and it contains commands to do with, you guessed it, time.


Coding in a Console

You will start using a Python Console. If you are using PyCharm or similar editors, find the Python Console option. In PyCharm it's at the bottom left of the window (if you cannot see the text Python Console in your version, click on the small square in the bottom left corner):


If you're using a web-based platform, such as a Trinket-based one, then you should be able to find a Console there too. On Trinket-based systems, including the codetoday learning platform, you can find it here:

You know you're in a Console environment because each line starts with:

>>>

In a Console, each time you hit the Return (or Enter) key, that line of code is run (or executed). This is different to the usual setting (which we call writing a script) in which all the lines will run one after the other only when you press the Run button.


Using the time Module in Python

You can start by bringing the book called time from the Python library and running the function that is also called time from inside this book:


>>> import time
>>> time.time()
1599396434.8482718

The number you get will not be the same as the one above, but very similar. Can you guess what this number is?


This number tells us the time, but not in the way you're used to. It gives you the number of seconds that have passed since the 1st of January 1970. Strange? That's how computers keep track of time, using a date as a reference.


Why is this useful? Let's try the following:

>>> start = time.time()
>>> time.time() - start
6.587711811065674

You are now storing the time in a box called start when the first line runs. On the second line, Python checks the time again (when you write time.time()) but then takes away the time it checked when the first line was run. The number that you see is the number of seconds it took me between pressing Return after the first line and pressing Return after typing the second line. Try it out…


Showing a Timer in a turtle Animation

You can now go back to the usual place where you write your Python code in which you can write all of your code and press Run when you want to run the program.


Start by importing the two modules we need to put a timer in a turtle program, and create two turtles:

import time
import turtle

player = turtle.Turtle()
timer_text = turtle.Turtle()

The first turtle can be the player that moves in a game. In this example you will just make the player move continuously but when you use the timer in your future programs, your player turtle is likely to do more than just move!


You will use the second turtle to write text on the screen, in this case you will write the time that has passed.


Set the player so that it moves and turns continuously, but before it starts, we want to start the timer:

start = time.time()
while True:
    player.forward(1)
    player.left(1)

Next, you can write the time on the screen each time the player moves by adding:

    timer_text.write(time.time() - start)

immediately after you make the player move (with the same indentation).


When you run this you'll notice something strange is happening. Each time the timer_text turtle writes the number of seconds elapsed on the screen, it writes the text on top of the previous text. Not good. Let's try adding

    timer_text.clear()

before you write the text.