Python 实现 Hangman 小游戏

Hangman--游戏简介--> 百度百科

打印Hangman

def printHangman(N):
    #   第一行
    print("\t____")
    #   第二行
    print("\t|  |")
    #   第三行
    print("\t|  ", end="")

    if N > 0:
        #   第三行 第一笔
        print("O")
    else:
        #   第三行 换行
        print()

    #   第四行
    print("\t| ", end="")

    if N > 2:
        #   第四行 第三笔
        print("/", end="")
    else:
        #   第四行 换行
        print(" ", end="")

    if N > 1:
        #   第四行 第二笔
        print("|", end="")
    else:
        #   第四行 换行
        print(" ", end="")

    if N > 3:
        #   第四行 第四笔
        print("\\")
    else:
        #   第四行 换行
        print(" ")

    #   第五行
    print("\t| ", end="")

    if N > 4:
        #   第五行 第五笔
        print("/ ", end="")
    else:
        #   第五行 换行
        print("  ", end="")

    if N > 5:
        #   第五行 第六笔(画完结束)
        print("\\")
    else:
        #   第五行 换行
        print(" ")

    #   第六行
    print("\t|")
    #   第七行
    print("-------------")


# printHangman(0)
# printHangman(1)
# printHangman(2)
# printHangman(3)
# printHangman(4)
# printHangman(5)
# printHangman(6)


其它

#   生成一个仅包含星号的长度相同的字符串
def hide(s):
    return '*' * len(s)


#   计算星号
def hide_num(secret):
    num = 0
    for i in secret:
        if i == "*":
            num += 1
    return num


#   记录猜测错误的词
def record_wrong(wrong, wrong_in):
    wrong = wrong + wrong_in
    return wrong


#   打印已经猜错的词
def print_wrong(wrong):
    print("You've entered (wrong):", end=' ')
    for i in wrong:
        if i != wrong[-1]:
            print(i, end=',')
        else:
            print(i)


#   将猜对的词在对应位置显示出来
def show_correct(word, secret, correct_in):
    index = 0
    temp = ''
    for i in word:
        #   将每个词和输入的进行匹配
        if i == correct_in:
            temp = temp + correct_in
        #   将已经猜对的词继续显示
        elif secret[index] != '*':
            temp = temp + secret[index]
        #   没猜中的继续以 * 表示
        else:
            temp = temp + '*'
        index += 1
    return temp


开始游戏

#   给定一个需要猜测的单词开始游戏
def start_game(word):
    #   已经猜错的词
    wrong = ''
    #   将未猜出的以 * 显示
    secret = hide(word)
    #   记录还剩多少个 * ,如果为0,则为全部猜中
    secret_num = hide_num(secret)
    #   猜错的步数,6步画完小人,表示失败
    hang = 0

    #   6步之内而且还有 * 未猜出
    while secret_num > 0 and hang < 7:
        print('You word looks like this:')
        print(secret)
        printHangman(hang)
        
        if hang < 6:
            print('Choose a letter:', end='')
            char_in = input()

            # 判断猜的词是否在单词中
            if char_in in word:
                secret = show_correct(word,secret, char_in)
            else:
                wrong = record_wrong(wrong, char_in)
                hang = hang + 1

            secret_num = hide_num(secret)
            # 代表得分,假如abbc,猜b,得两分
            score = len(word) - secret_num
            print('Your points so far:', score)
            if wrong != '':
                print_wrong(wrong)
            print()
        else:
            #   小人画完
            break
            
    #   胜利
    if secret_num == 0:
        print('WIN')
        print('Word:', word)
    #   失败
    else:
        print('Defeat')
        print('Word:', word)


结果




猜你喜欢

转载自www.cnblogs.com/jiyu-hlzy/p/11908634.html