2016-07-07 123 views
-2

我想写一个打字挑战游戏,玩家必须在时间限制内尽可能快地输入一个单词。在game()函数结束时,它应该在计时器达到0时执行round1()函数。然而,没有任何反应,它只停留在号码1。任何想法是什么导致这种行为?我不明白为什么我的功能不会发生

这是我使用的代码:你做了功能round1

import random 
import time 
global timer 
timer = 20 
global counting 
counting = 10 
global rounds 
rounds = 0 
def menu(): 
    print ("Main Menu\nType in 'start' to begin the typing challenge") 
    start = input() 
    if start == "start": 
     game() 
    else: 
     menu() 
def game(): 
    global counting 
    choices = ["snazzy", "pizzas", "sizzle", "jackal"] 
    global word 
    word = (random.choice(choices)) 
    print ("The word you must type is", word) 
    print ("You will be timed on how long it takes you to type the word.") 
    print ("Each round you will have a slightly smaller amount of time to type the word") 
    time.sleep(10) 
    print ("Starting in...") 
    for count in range(10): 
     print (counting) 
     time.sleep(1) 
     counting -=1 
    round1() 
def round1(): 
    useless = 100 
    global rounds 
    global word 
    global timer 
    while useless > 1: 
     for count in range(20): 
      time.sleep(1) 
      timer -=1 
    print ("Type", word) 
    attempt = input() 
    if attempt == word and timer > 0: 
     rounds = rounds+1 
     round2() 
    else: 
     lose() 

回答

1

,但一旦你在那里,你陷入了一个无限循环,因为变量useless永远不会改变。

即使您取出while循环,您也永远无法获胜,因为直到计时器耗尽后才输入。

+0

这里没有需要并发的简单流程游戏 –

+0

没错,不需要并发。我没有想到只是运行一个计时器。 – arewm

0

从第1轮就试试这个代码:

def round1(): 
    import sys, select 
    global rounds 
    global word 
    global timer 
    print ("Type", word) 
    input, out, error = select.select([sys.stdin], [], [], timer) 
    if (input): 
     attempt = sys.stdin.readline().strip() 
     if attempt == word: 
      rounds = rounds+1 
      round2() 
     else: 
      lose() 
    else: 
     lose() 

if __name__ == '__main__': 
    menu() 

需要注意的是,作为 - 是,这将失败,因为NameError你没有定义的round2()功能。更好的解决方案是推广你的功能,如round_x(number, word, time_to_answer);这样你可以重复使用相同的代码,而不必导入全局变量。

+0

请注意,这在Windows上不起作用,但在Linux上它工作得很好。 – arewm

0

我被打了一下,你的游戏你做了太多开销和错误 那里,所以我只是改变了它的结构简化:

import random 
import time 
current_milli_time = lambda:int(time.time() * 1000) 
timer = 20 #suppose user has 20 secs to enter a word 
counting = 10 
requested_word = "" 
choices = ["snazzy", "pizzas", "sizzle", "jackal"] 
rounds = 0 
def menu(): 
    print ("Main Menu\nType in 'start' to begin the typing challenge") 
    game() if input().lower() == "start" else menu() 
#There is no need to split game and round but I didn't want to mess with 
#your program structure too much 
def game(): 
    global requested_word 
    try: 
     requested_word = (random.choice(choices)) 
     choices.remove(requested_word) 
    except IndexError: 
     print "Game is finished" 
     return 
    print ("The word you must type is", requested_word) 
    print ("You will be timed on how long it takes you to type the word.") 
    print ("Each round you will have a slightly smaller amount of time to type the word") 
    print ("Starting typing in in...") 
    for count in range(10,-1,-1): 
     print (count) 
     time.sleep(1) 
    round() 
def round(): 
    global timer 
    start = current_milli_time() 
    word = input("Enter word -> ") 
    stop = current_milli_time() 
    time_delta = stop-start #this is in milliseconds 
    if time_delta< timer*1000 and word == requested_word : 
     timer = timer-5 #lower the time to enter a word 
     game() 
    else: 
     print("Game OVER") 
menu() 

当提示“输入字 - >”出现在用户他一直都想输入一个单词,但是在time_delta被计算出来之后,如果它超过了限制,那么对他来说就是游戏结束了。

相关问题