2014-09-03 2 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

또한 순위 기능을 수정 하겠지만 포인트 옆에 있습니다 –

+0

y가 1과 같다고 확신합니까? y를 인쇄 해보고 원하는 것을 얻는 지보십시오. – PandemoniumSyndicate

+0

ok 시도해 보겠습니다. –

답변

2

: 당신이 입력을 얻을 때

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

, 그것은으로 돌아 올 것이다 끈. 문자열 "1"을 정수 1과 비교합니다. 둘은 같지 않으므로 firstlevel()은 호출되지 않습니다.

문자열을 정수로 변환하거나 정수를 문자열로 변경하여 동일한 유형의 두 객체를 비교해야합니다.

0

y 당신이 생각하는 것이 아니다 원인이 내기. 그것을 밖으로 인쇄하거나 값이 정수인지 확인하는 데에 중단 점을 넣어보십시오 1. 문제는이 세 줄입니다

0

문제는 여기에 있습니다 : 그것은 정수 1와 동일하지 않습니다 그래서

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

input()는 문자열을 반환합니다. 비교하기 전에 yint()을 사용하기 만하면됩니다.

관련 문제