2014-09-03 60 views
0

那麼這裏的問題我的遊戲()函數將不會去我的firstlevel()函數,只是保持說退出退出代碼爲0的進程我不知道爲什麼我甚至嘗試更改函數名稱仍然沒有運氣我真的不知道做什麼,我只是個初學者...我的遊戲不會去我的功能

代碼:

import winsound 
import random as ran 
import pickle 


profile = {} 


def fightsound(): 
    winsound.PlaySound('fight2.wav', winsound.SND_FILENAME | winsound.SND_ASYNC) 



def ranking(): 
    if profile['xp'] >= 20: 
     profile['level'] += 1 
     if profile['xp'] >= 50: 
      profile['level'] += 1 
      if profile['xp'] >= 100: 
       profile['level'] += 1 
       game() 
      else: 
       game() 
     else: 
      game() 
    else: 
     game() 


def play_bgmusic(): 
    winsound.PlaySound('mk.wav', winsound.SND_FILENAME | winsound.SND_ASYNC) 


def load_game(): 
    global profile 
    profile = pickle.load(open("save.txt", "rb")) 
    game() 


def fatality(): 
    winsound.PlaySound('fatal2.wav', winsound.SND_FILENAME | winsound.SND_ASYNC) 


def game(): 
    global profile 
    print("Player: " + profile['player']) 
    print("XP: ", profile['xp']) 
    print("Level: ", profile['level']) 
    print("win: ", profile['win']) 
    print("loss: ", profile['loss']) 
    if profile['level'] >= 1: 
     print("1.) The ogre king...") 
     if profile['level'] >= 2: 
      print("2.) The realm of the witch!") 
    y = input("Select an option -> ") 
    if y == 1: 
     firstlevel() 


def firstlevel(): 
    global profile 
    fightsound() 
    enemyhp = 50 
    hp = 100 
    while enemyhp > 0: 
     print("Your hp: ", hp, " Enemy hp: ", enemyhp) 
     input("Press enter to attack...") 
     damage = ran.randint(0, 25) 
     enemyhp -= damage 
     damage = ran.randint(0, 25) 
     hp -= damage 
     if hp <= 0: 
      profile['xp'] += 5 
      profile['loss'] += 1 
      pickle.dump(profile, open("save.txt", "wb")) 
      print("You died, press enter to continue...") 
      game() 
    fatality() 
    profile['xp'] += 10 
    profile['win'] += 1 
    pickle.dump(profile, open("save.txt", "wb")) 
    input("You win! Press enter to continue...") 
    ranking() 


def new_game(): 
    global profile 
    player = input("Enter a player name -> ") 
    profile['player'] = player 
    profile['xp'] = 0 
    profile['level'] = 1 
    profile['win'] = 0 
    profile['loss'] = 0 
    pickle.dump(profile, open("save.txt", "wb")) 
    game() 


def main(): 
    play_bgmusic() 
    print(20 * "-") 
    print("|     |") 
    print("| 1.) New Game  |") 
    print("| 2.) Load Game |") 
    print("| 3.) Credits  |") 
    print("|     |") 
    print(20 * "-") 

    x = int(input("Select an option -> ")) 

    if x == 1: 
     new_game() 
    if x == 2: 
     load_game() 
    if x == 3: 
     pass 

main() 
+0

另外,我要解決我的排名功能,但這不在點 – 2014-09-03 17:01:38

+0

那麼你確定y等於1嗎?嘗試打印出來,看看你是否得到你想要的東西。 – PandemoniumSyndicate 2014-09-03 17:05:41

+0

好的我會嘗試 – 2014-09-03 17:07:13

回答

2

的問題是這三行:

​​

當你輸入時,它會回來的串。您正在比較字符串"1"與整數1。這兩個不相等,所以firstlevel()永遠不會被調用。

您應該將字符串轉換爲整數,或將整數更改爲字符串,以便比較兩個相同類型的對象。

0

要去打賭它的原因y是不是你認爲它是。嘗試打印出來或者放在一個斷點上,以確保它是值的整數1

0

的問題是在這裏:

y = input("Select an option -> ") 
if y == 1: 

input()返回一個字符串,所以它永遠不會等於整數1。在比較之前,只需在y上使用int()即可。