2016-07-07 121 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被計算出來之後,如果它超過了限制,那麼對他來說就是遊戲結束了。

相關問題