Hack Forums
[Python] Python Typing Game - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Python Typing Game (/showthread.php?tid=5845098)



Python Typing Game - WhiteTiger. - Jun 5, 2018

For those who are bored: here is a simple typing game.

Best experience if played directly in command prompt.
To do this; go to the path where you saved the file and type the following:

Code
python main.py

If Python isn't recognized as a command make sure you add Python to the PATH environmental variable.

Here's the main.py you can copy paste:

Code
import time
import random
import os

chars = "abcdefghijklmnopqrstuvwxyz"
TIME = 60

if sys.platform == "linux" or sys.platform == "linux2":
    CLEAR = "clear"
elif sys.platform == "win32":
    CLEAR = "cls"


def generate_string():
    string_length = random.randint(3, 5)

    string = ""
    for i in range(string_length):
        char_index = random.randint(0, len(chars)-1)
        char = chars[char_index]
        string += char

    return string


def get_title(wpm):
    if wpm <= 5:
        return "Terrible"
    elif 5 < wpm <= 10:
        return "Bad"
    elif 10 < wpm <= 15:
        return "Average"
    elif 15 < wpm <= 20:
        return "Good"
    elif 20 < wpm <= 25:
        return "Expert"
    elif 25 < wpm <= 30:
        return "God"
    elif 30 < wpm <= 100:
        return "Devil"
    elif 100 < wpm:
        return "Hacker"


def show_info(wpm, cpm, clock, string):
    print("Current WPM:", wpm)
    print("Current CPM:", cpm)
    print("Time left: ", TIME - clock)
    print(string)


def show_results(wpm, cpm, title):
    print("-----TIME IS UP-----")
    print("Your WPM:", wpm)
    print("Your CPM:", cpm)
    print("Your Title:", title)
    print("--------------------")


def game():
    os.system(CLEAR)    

    wpm = 0
    cpm = 0
    clock = 0

    while clock < TIME:
        clock = time.clock()
        string = generate_string()

        show_info(wpm, cpm, clock, string)

        correct_input = False
        while correct_input is False:

            user_input = input()

            if user_input == string:
                wpm += 1
                cpm += len(string)

            correct_input = True

        os.system(CLEAR)

    title = get_title(wpm)
    show_results(wpm, cpm, title)


game()

(If you're not on windows: change 'cls' to 'clear')

I also do realize that this doesn't give your real wpm because of the random strings but I do think it's still a great time waster.

Feel free to give feedback/share/modify.


Kind regards,

~WT


RE: Python Typing Game - koolkid - Jun 6, 2018

well regarding your code you could make it more AI functionality to it by instead of the user changing the cls to clear you could write and extra 4 lines of code to check the os and switch it .

PHP Code

<?php 
#sudo code

if OS =='windows':
     
operation = 'cls'
elif OS == 'linux':
      
operation = 'clear' 


i hope u get the concept


RE: Python Typing Game - WhiteTiger. - Jun 6, 2018

(Jun 6, 2018 05:03 AM)koolkid Wrote:  well regarding your code you could make it more AI functionality to it by instead of the user changing the cls to clear you could write and extra 4 lines of code to check the os and switch it .

PHP Code

<?php 
#sudo code

if OS =='windows':
 
    operation = 'cls'
elif OS == 'linux':
 
     operation = 'clear' 


i hope u get the concept

Thanks for your feedback. I changed it now.

I also came to the conclusion that the time.clock() doesn't work in Unix as I thought it would making this game "unplayable" in Linux.
You can play but the time drops with 0.001 seconds every second or so.

Any proper way to fix this other then to write a time functions specific for Linux?

Thanks in advance,

~WT


RE: Python Typing Game - koolkid - Jun 6, 2018

write the function for linux and call it if the os is linux. also instead of generating random string, u could use a list of actual words.