...

Click Counter Game

Welcome to the Click Counter Game!
In this fun and interactive game, we'll learn how to count clicks and challenge ourselves to see how many clicks we can make in just 10 seconds. We'll use a magical turtle to help us count and display the results on the screen.
As part of this project you will learn:

The Turtle library

What is Turtle library?

In Python, a library is a reusable chunk of code you can include in your programs and use pre-existing functionality.

Turtle is a graphic library for Python. Using Turtle, we can easily draw in a drawing board.

First, we import the turtle module. Then create a window, next we create a turtle object and using the turtle method we can draw on the drawing board.

Next
Please ensure all required fields are filled in
Step 1: Let's start coding
  • In your logged-in trinket account click on New Trinket -> Python

  • Or Open the blank Python template trinket: Open New.

  • Prev Next
    Please ensure all required fields are filled in
    Step 2: Create a window
  • The first step is to import the Turtle module. Place import statements at the top of your code.
  • Next, define the screen and specify its dimension. Type the code as follows:
  • done() command must be the last statement in a turtle graphics program.
  • Let's add the background colour to the screen. Follow the code below:
  • The hideturtle() command will hide the cursor
  • Now, let's explore some essential Turtle functions. We'll start by drawing a circle and filling it with a colour.
    We will use the following commands:

  • speed(number) - set the speed of your drawing from 0 to 10
  • penup() - This command lifts the turtle's writing pen, so it won't leave a trace while moving.
  • pendown() - With this command, we put the turtle's writing pen back down, allowing it to draw on the screen.
  • goto(x,y) - Use this command to navigate the turtle to a specific coordinate on the screen. The (x, y) values represent the horizontal and vertical positions.
  • color("colourName") - This command sets the colour of the turtle's pen or fill to the specified colour name, such as "blue" in our case.
  • begin_fill() and end_fill() - These commands mark the beginning and end of the shape that we want to fill with the selected colour.
  • forward(number) - Use this command to move the turtle forward by a specified number of pixels.
  • right(angle) and left(angle) - These commands allow the turtle to turn right or left by the specified angle.
  • Prev Next
    Please ensure all required fields are filled in
    Step 3: Let's add Click Button

    Next, we will replace our plane background with the button image.

  • Download the calendar image -> click here
  • Add an image to your trinket account
  • Click on the Image icon -> Image Library button->Upload New Image
  • Select the button.gif image that you just downloaded
  • Click Done button
  • We can now set the image instead of the background colour by replacing screen.bgcolor("green") with the following as shown in the picture:
  • Prev Next
    Please ensure all required fields are filled in
    Step 4: Adding Click Counter

    We want to count how many times you click on the screen, like having a helper friend who keeps track for you! Whenever you click on the screen, the counter will count your clicks.

  • We need a way to count your clicks, so we create a variable called counter to remember the number of clicks
  • When you click, we need to create a function to increase counter by 1

  • Add the following code as below:

    The "time" Library

    We also have another special library called "time." This library helps us deal with time-related things, like waiting for a few seconds. We'll use it to add a 10 seconds wait.

  • First, import it at the top of the code using import time
  • Next, use time.sleep(10) to add 10 seconds wait after the onscreenclick() function
  • Prev Next
    Step 5: Display the Result

    After the 10 seconds are up, we will show the total number of clicks you made during the game on the screen.

                                #Display Result
                                pu()
                                goto(-50, -150)
                                clear()
                                write("Total clicks: " + str(counter), align="center", font=("Arial", 16, "normal"))
                                

    Prev Next
    Please ensure all required fields are filled in
    Step 6: Let's do some improvements

    In this improved version of Click Counter we've added:

  • Live Count of Clicks and Time Display
  • Result Display moved to a function
  • Click Count Starts after the first click
  • Prev Next
    Please ensure all required fields are filled in
    Step 7: Save your work

  • If you don't have a Trinket account, click the down arrow and then click Link. This will give you a link that you can save and come back to later.
  • If you have a Trinket account, you can click Remix to save your own copy of the trinket.
  • Prev Next
    Step 8: Complete and Share

    Submit your final project



    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste link into the field
  • Prev