There are many ways to keep the score in a game, we will show you how to write a score onto the canvas. First make a score component: Examplevar myGamePiece; The syntax for writing text on a canvas element is different from drawing a rectangle. Therefore we must call the component constructor using an additional argument, telling the constructor that this component is of type "text". In the component constructor we test if the component is of type "text", and use the Examplefunction component(width, height, color, x, y, type) { At last we add some code in the updateGameArea function that writes the score onto the canvas. We use the Our quiz game will be asking questions to the player to which player has to reply with the right answer. Each question will have 3 attempts. If the player fails to answer the question within 3 attempts then the game will move on to the next question and the player will receive zero points. But if the player gives the right answer to the question then, he will get 1 point. At the end of the game, the total points scored by the player are displayed. I hope the abstract working of the game is clear to everyone, now let's move on to the project setup. Project SetupBefore we start coding this project, we need some questions and answers for our game. In our case, we are going to use some easy superhero based questions. Feel free to use your own questions or answers for the game. Our questions and answers will be stored in a separate python file in a form of a python dictionary. Here how it looks:
Enter fullscreen mode Exit fullscreen mode You can learn more about python dictionaries from here. We won't be able to cover much about dictionaries in this tutorial but basically it is a data structure that can be used to store data as a single, organized & easy to access form. You can think of the dictionary as the list. But there are some key differences between lists & dictionaries:
You must make sure that your dictionary should be in the same format as above or else you may need to make necessary changes to the code to make it work for you. Feel free to ask questions on my social handles or post your question below in discussions/comments. Now I assume that you have your questions & answers ready. Make sure that your Q&A python file is in the same folder as your main quiz game python file which we will start coding in just a second. Now let's jump to coding. Let's CodeThe first thing we always do is import required modules into our code. Luckily for this project, we don't need any specific module. However, we still need to import the Q&A python file we created in the previous step. We have named our Q&A python file as 7. Here's how we will import it:
Enter fullscreen mode Exit fullscreen mode We are asking python to import the 8 dictionary which contains our question & answers from the file 7.Now let's get to the structure of our game... Pay close attention! As this might feel a bit complicated... Now we are going to initialize a variable to keep track of the score.
Enter fullscreen mode Exit fullscreen mode Now it's time to ask the questions to our player. For that, we need to create an 0 loop which will iterate through all the questions.
Enter fullscreen mode Exit fullscreen mode Now as previously mentioned, the player will have 3 attempts for each question to get the right answer. Let's create a variable to keep track of the attempts.
Enter fullscreen mode Exit fullscreen mode Now let's create an 1 loop within our 0 loop, which will run only until player has attempts left.
Enter fullscreen mode Exit fullscreen mode Great! Now let's print the questions and take the response from our player. We'll use our good old 3 & 4 functions for that.
Enter fullscreen mode Exit fullscreen mode Awesome! The response of the player will be stored in the 5 variable.Now we will use a function which will check if the answer provided by the player is right or wrong. We will name that function as 6. For now, let's focus on our 0 loop and then we will see how this function works.
Enter fullscreen mode Exit fullscreen mode We will pass 4 parameters to our function, which are:
We will store the output of our function in 3 variable.Now we are going to use 4 statements to increase score if the answer provided by the player is right.
Enter fullscreen mode Exit fullscreen mode Here if the answer given by the player is right then the score will increase by 1 and the 1 loop will break and the 0 loop will move on to the next question.But if the answer is wrong, then the player will lose one attempt, and the while loop will continue until either right answer is provided by the player or player runs out of attempts. Here finally, our 0 loop ends!Are we forgetting something? 🤔 Oh we forgot the implementation of our 6 function... Let's cover that quickly!
Enter fullscreen mode Exit fullscreen mode Here is our function... Let's break it down! Here an 4 statement will compare the answer provided by the player with the correct answer from our dictionary.If the answer is right then it will return 0 or else it will return 1.Let's add a few print statements to notify the player if his answer is right or wrong. 0Enter fullscreen mode Exit fullscreen mode Here looks good right! You did it! Be proud of yourself 🤩 Some Ideas to try outHere are some quick ideas you can try with this project.
Source CodeYou can find the complete source code of this project here - mindninjaX/Python-Projects-for-Beginners SupportThank you so much for reading! I hope you found this beginner project useful. If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you. Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D |