-1

Here is my code for a sprinting game in Python. You have to tap the 'a' and 'd' keys as fast as you can to run 100 meters. **Firstly, I would appreciate anyone's opinion, and I would also be interested to know if I can alter my program to make it accessible to multiple platforms?

import msvcrt
import time
high_score = 50
name = "no-one"
while True:
    distance = int(0)
    print("\n--------------------------------------------------------------")
    print('\n\nWelcome to the 100m sprint, tap a and d rapidly to move!')    
    print('* = 10m')
    print("\n**Current record: " + str(high_score) + "s, by: " + name)
    print('\nPress enter to start')
    input()
    print('Ready...')
    time.sleep(1)
    print('GO!')
start_time = time.time()
while distance < 100:           
    k1 = msvcrt.getch().decode('ASCII')
    if k1 == 'a':
        k2 = msvcrt.getch().decode('ASCII')
        if k2 == 'd':
            distance += 1
            if distance == 50:
                print("* You're halfway there!")
                elif distance % 10 == 0:
                    print('*')
fin_time = time.time() - start_time
fin_time = round(fin_time,2)
print('Well done you did it in...'+str(fin_time))
if fin_time < high_score:
    print("Well done you've got a new high score ")
    name = input("Please enter your name : ")
D.C
  • 120
  • 9
  • http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user shows how you can get input cross-platform – Adam Smith Feb 18 '16 at 17:51

2 Answers2

3

Yes the code review answer is right msvcrt is a module specifically for windows, you could make your code cross platform by checking the os your code is running on before carrying out an operation this can be done with plaform module

import platform
if platform.system() == "Windows":
    import msvcrt
    # do windows only stuff

if platform.system() == "Linux":
    # do some linux stuff
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
danidee
  • 9,298
  • 2
  • 35
  • 55
0

Your code is perfectly runnable on any platform that has a Python interpreter.

Jaron Thatcher
  • 814
  • 2
  • 9
  • 24
  • 1
    On code review I was told this: If you didn't know, your code is not cross platform. This is as msvcrt is MS Windows only. To allow use on Linux and OSX you should use sys, tty and termios, as shown in this SO answer. Doing this will allow more users to use your program. – D.C Feb 17 '16 at 21:46
  • I thought that I could adapt it to make it more widely available – D.C Feb 17 '16 at 21:46
  • 1
    Apologies, I didn't realize that was where `getch` was coming from. What is the specific reason you are using msvcrt? Why not just use the standard `getch` found in https://pypi.python.org/pypi/getch – Jaron Thatcher Feb 17 '16 at 21:49
  • to be honest, I am not sure, all I know is that it worked, so I used it. Haha sorry – D.C Feb 17 '16 at 21:52
  • 1
    @Jaron from the Pypi page: "Platform: Linux/UNIX (Windows unknown...)" – Adam Smith Feb 18 '16 at 17:45