zoukankan      html  css  js  c++  java
  • Python 实现 Hangman 小游戏

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

    打印Hangman

    def printHangman(N):
        #   第一行
        print("	____")
        #   第二行
        print("	|  |")
        #   第三行
        print("	|  ", end="")
    
        if N > 0:
            #   第三行 第一笔
            print("O")
        else:
            #   第三行 换行
            print()
    
        #   第四行
        print("	| ", end="")
    
        if N > 2:
            #   第四行 第三笔
            print("/", end="")
        else:
            #   第四行 换行
            print(" ", end="")
    
        if N > 1:
            #   第四行 第二笔
            print("|", end="")
        else:
            #   第四行 换行
            print(" ", end="")
    
        if N > 3:
            #   第四行 第四笔
            print("\")
        else:
            #   第四行 换行
            print(" ")
    
        #   第五行
        print("	| ", end="")
    
        if N > 4:
            #   第五行 第五笔
            print("/ ", end="")
        else:
            #   第五行 换行
            print("  ", end="")
    
        if N > 5:
            #   第五行 第六笔(画完结束)
            print("\")
        else:
            #   第五行 换行
            print(" ")
    
        #   第六行
        print("	|")
        #   第七行
        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

    <br/>
    ### 开始游戏
    

    给定一个需要猜测的单词开始游戏

    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)
    
    <br/>
    ### 结果
    ![](https://img2018.cnblogs.com/blog/821307/201911/821307-20191121214223726-785597182.png)
    
    
    <br/><br/><br/>
  • 相关阅读:
    调试跳转动态打印
    PHP对redis操作详解
    SSL证书没有绿锁您与此网站建立的连接并非完全安全解决办法
    63. Unique Paths II
    62. Unique Paths
    40. Combination Sum II
    60. Permutation Sequence
    59. Spiral Matrix II
    批量修改名字的脚本
    57. Insert Interval
  • 原文地址:https://www.cnblogs.com/jiyu-hlzy/p/11908634.html
Copyright © 2011-2022 走看看