这是我在Python中使用短跑游戏的代码。您必须尽可能快地点击“a”和“d”键才能跑100米。 **首先,我会感谢任何人的意见,我也有兴趣知道我是否可以改变我的程序以使其可以访问多个平台?
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 : ")
答案 0 :(得分:3)
是的,代码审查答案是正确的msvcrt
是一个专门用于Windows的模块,您可以通过检查代码运行的操作系统使代码跨平台,然后执行操作,这可以通过{{{ 1}}模块
plaform
答案 1 :(得分:0)
您的代码可以在任何具有Python解释器的平台上完全运行。