我的游戏不会去我的功能

时间:2014-09-03 16:59:13

标签: python function python-3.x

这里的问题是我的game()函数不会转到我的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()

3 个答案:

答案 0 :(得分:2)

问题在于这三行:

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

当您获得输入时,它将以字符串形式返回。您正在将字符串"1"与整数1进行比较。两者不相等,因此永远不会调用firstlevel()

您应该将字符串转换为整数,或将整数更改为字符串,以便比较两个相同类型的对象。

答案 1 :(得分:0)

打赌它的原因y不是你想象的那样。尝试将其打印出来或在其上放置一个断点,以确保它是一个值为1的整数。

答案 2 :(得分:0)

问题在于:

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

input()返回一个字符串,因此它永远不会等于整数1。在您进行比较之前,只需在int()上使用y,就可以了。