Colour game using PyQt5 in Python

This post will demonstrate how to use PyQt5 to construct a colour game. In order to get the most points in this game, the player must correctly identify the colour of the word that is provided. To further confuse the player, the text will have several colour names.

The multiplatform PyQt5 GUI toolkit includes Python bindings for Qt v5. The toolkit's features and simplicity make it very easy to construct an interactive desktop application. Installing it requires running the next command.

Steps for implementation of the GUI :

  1. Create a head label to display the game's name and specify its characteristics, such as alignment colour, etc.
  2. Make an instruction label to provide the user the instructions.
  3. Design a push button to launch or restart the game.
  4. Make a label to display the score.
  5. Create a line edit to collect user input.
  6. Create a label for a countdown of 30 seconds.

Implementation steps for the back end :

  1. Make a score value variable, a counter value variable, a list of colours, and a start flag.
  2. Make an object with a timer that calls a function after one second.
  3. Determine whether the start flag is set to true inside the timer method, then set the counter value to the counter label, and decrease the count value.
  4. If the counter variable equals zero, set the start flag to false and prevent line editing.
  5. Include a start button function.
  6. In the action for the start button, set its initial value as true, set thou count's value as 30 and clear the entire line edit text.
  7. Choose a random colour from the colour list and set it as the colour label.
  8. Once more, select a random option from the list, then add that text to the label.
  9. When enter is pressed, add an action to the line edit.
  10. Inside the action, verify the input text against a random choice, and if it matches, increase the score value and alter the colour label's text and colour to another random number.

Walking through the code :

first, we import all the required files into our program from the standard python library.

Next, we will create a window class to initialize the window and set its geometry and title. Then we will introduce the various labels, flag and variables and also set their initial values and give a list of random colours.

Next, we will create a new function for introducing the components for out window. We will then create new heading and various new labels and set their properties like geometry, fonts, and alignments and stylesheet properties too. We also added buttons and the functions for its action were also introduced.

Then, we created a clock timer label to show the time and mentioned its properties, Then, we created a function to display the time in that label and keep it updated after every second.

Then, we created a start action function to start the countdown and used the labels to display and update the text and buttons to click for functionality which is to chose a random colour from the list and if it is entered right in the text field and repeat the process otherwise break the process. And an input function was also used to take and display the input from the user and give the result.

Finally, we created an instance of the window and started our application by calling that instance.

Complete code :

Output:

The output of the code we explained above has been attached below as a screenshot.

Colour game using PyQt5 in Python




Latest Courses