Making with Code

Typing Game #

In this lab, you will create a typing game! You will be re-introduced to the Terminal, Python syntax, and logical flow.

๐Ÿ’ป Let's start by playing: MonkeyType

โœ๏ธ Once you understand the game, re-arrange the pseudocode in the correct order. Check with a teacher before moving on to the setup.


[0] Setup #

๐ŸŒ Github Repo: github.com/Making-with-Code/lab_typing_game

๐Ÿ’ป cd into the lab

cd lab_typing_game
๐Ÿ‘พ ๐Ÿ’ฌ Windows Users

I suggest not copying the path command, and instead using cd and ls to ensure you are in the correct making_with_code folder.

๐Ÿ’ป Enter the Poetry Shell. We will also run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D

๐Ÿ’ป Take a look at the files inside with: ls

  • typing_game.py - will run the game
  • prompts.py - stores the typing prompts
  • helpers.py. - helper functions for the games

๐Ÿ’ป Open the folder in Visual Studio Code: code .

In the Terminal, We will use code now instead of atom


[1] Typing Game #

๐Ÿ’ป Start by running the game: python typing_game.py. As you can see the welcome screen, rules, and countdown to the start of the game already coded. Now that you understand how the logical flow of the game, it’s up to you to finish it!


[Game Logic] #

๐Ÿ’ป Translate the pseudocode to Python code in typing_game.py. The pseudocode is in the correct order in the comments of the file.

๐Ÿ‘€A successful game should run like this:
๐Ÿ‘พ ๐Ÿ’ฌ Be sure to constantly play test your game!

Do not be afraid to run your game after every change you make to the code: python typing_game.py

Testing is the key to successful debugging!


๐Ÿ‘€ Here are a few useful functions you will need to successfully code a working typing game:

input()

  • parameters: string
user_answer = input("Do you like pizza? (yes/no): ")

choice() - random library

  • parameter: a list
  • return value: a random item from a list
animal_list = ['dog','cat','bird']
random_animal = choice(animal_list)

time() - time library

  • return value: the current time
current_time = time()

calculate_wpm() - this function is written in helpers.py

  • parameters: prompt, start_time, end_time
  • return value: the words per minute
wpm = calculate_wpm('hello i am a dog', start_time, end_time)

round()

  • parameters: number to round, number of decimal places
number_rounded = round(2.555,2)

[Calculate the user’s accuracy] #

Now that you have the logic of the game complete, let’s communicate the accuracy of the user’s input. Words per Minute (WPM) means nothing without accuracy!

๐Ÿ’ป In helpers.py complete the function calculate_accuracy() to calculate user’s typing accuracy. Typing accuracy is calculated by dividing the correct characters typed in by the total total characters typed. For example, if you typed 9 out of 10 characters correctly, you would have .90 accuracy or 90% accuracy.

  • parameters: chosen_prompt, user_typed_prompt
  • return value: accuracy rounded to decimal place

๐Ÿ’ญ Be sure to consider the following:

  • how do you loop through each letter of the user_typed_prompt and compare it to the chosen_prompt?
  • how do you keep track of correct characters?
  • how do you find the total number of characters?
  • what if the user presses enter/return before the end of the prompt?

๐Ÿ’ป Integrate calculate_accuracy() into typing_game.py so it reports both the wpm and accuracy to the user. You should print the user’s accuracy as a percentage after the WPM. Consider, how will you convert a decimal to a percentage?

WPM: 80.5
Accuracy: 92.2%

[4] Deliverables #

โšกโœจ

Once you’ve successfully completed the game be sure to fill out this Google form.

๐Ÿ’ป Push your work to Github:

  • git status
  • git add file_name.py file_name2.py
  • git status
  • git commit -m “describe your drawing and your process here”

    be sure to customize this message, do not copy and paste this line

  • git push


[3] Extension #

๐Ÿ‘€ Choose from any of the extension activities below. Be sure to push to Github any work you do on either of the extensions.


[Adjusted WPM] #

Adjusted Words Per Minute is the typing speed adjust for the user’s accuracy. It is calculated by WPM*Accuracy. For example:

wpm = 50.4
accuracy = 80.3%
adjusted_wpm = 40.5

๐Ÿ’ป Calculate the user’s adjusted_wpm and print it to the user.

WPM: 80.5
Accuracy: 92.2%
Adjusted WPM: 74.2

[Generate random prompts] #

Typing games often increase the difficulty by having the prompts be random words that do not tell a story. For example the prompt may be something like, “score wear question continue friend”. It is up to you to write a function that does just that.

๐Ÿ’ป In helpers.py, finish the function generate_prompt().

  1. It should return a string with a prompt of a random length between 10-15.
  2. It should get the words from the common_word_list in prompts.py.

๐Ÿ’ป Integrate generate_prompt() into typing_game.py.


[Difficult levels] #

As you may have noticed, the prompts are quite simple. There is no punctuation, hardly any capital letters, no numbers, or special symbols. It is up to you to create an easy, medium, and hard difficulty level of the game.

๐Ÿ’ป In prompts.py, fill in the prompt_dictionary with differing prompts for easy, medium, and hard. You may get the prompts from online, just be sure to cite your sources in the comments of the file.

๐Ÿ’ป Integrate the difficulty levels into typing_game.py.

  • the user should be able to choose which difficulty they want
  • based on that difficulty level, the game should pick a random prompt from that difficulty level.