Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python Typing Game

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
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
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(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
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.
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 631 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Java] by EnskyMar 24, 2017 06:26 AM 1 249 Mar 24, 2017 06:26 AM
Last Post: Ensky
[Python] by EnskyMar 23, 2017 01:27 PM 0 171 Mar 23, 2017 01:27 PM
Last Post: Ensky