Quảng Cáo

Cpm Affiliation : the cpm advertising network

Hướng dẫn viết game treo cổ bằng Python

Category: , Written by Nam Phạm Ngọc Quang / 22:36

Hướng dẫn viết game treo cổ bằng Python

import random
board = [
'  +---+   \n  |   |   \n      |   \n      |   \n      |   \n      |   \n========= \n',
'  +---+   \n  |   |   \n  0   |   \n      |   \n      |   \n      |   \n========= \n',
'  +---+   \n  |   |   \n  0   |   \n  |   |   \n      |   \n      |   \n========= \n',
'  +---+   \n  |   |   \n  0   |   \n /|   |   \n      |   \n      |   \n========= \n',
'  +---+   \n  |   |   \n  0   |   \n /|\\  |   \n      |   \n      |   \n========= \n',
'  +---+   \n  |   |   \n  0   |   \n /|\\  |   \n /    |   \n      |   \n========= \n',
'  +---+   \n  |   |   \n  0   |   \n /|\\  |   \n / \\  |   \n      |   \n========= \n'
]
class Hangman:
    def __init__(self,word):
        self.word = word
        self.missed_letters = []
        self.guessed_letters = []

    def print_game_status(self):
        print board[len(self.missed_letters)]
        print "Word: "+ self.hide_word()
        print "Letters Missed: ",
        for letter in self.missed_letters:
            print letter,
        print
        print "Letters Guessed: ",
        for letter in self.guessed_letters:
            print letter,
        print

    def hide_word(self):
        rtn = ""
        for letter in self.word:
            if letter not in self.guessed_letters:
                rtn += "_"
            else:
                rtn += letter
        return rtn

    def guess(self,letter):
        if letter in self.word and letter not in self.guessed_letters:
            self.guessed_letters.append(letter)
        elif letter not in self.word and letter not in self.missed_letters:
            self.missed_letters.append(letter)
        else:
            return False
        return True

    def hangman_won(self):
        if "_" not in self.hide_word():
            return True
        return False

    def hangman_over(self):
        return self.hangman_won() or len(self.missed_letters) == 6

def main():

    game = Hangman(rand_word())
    while not game.hangman_over():
        game.print_game_status()
        user_input = raw_input('\nEnter a letter: ')
        game.guess(user_input)

    game.print_game_status()
    if game.hangman_won():
        print "\nCongratulations! You are the winner of Hangman!"
    else:
        print "\nSorry, you have lost in the game of Hangman..."
        print "The word was " + game.word

    print "\nGoodbye!\n"

def rand_word():

    bank = ['python','vietnam','.info','tutorial','by','nampnq']
    return bank[random.randint(0,len(bank)-1)]

if __name__ == "__main__":
    main()

Đầu tiên bạn cần 7 trạng thái của hình người treo cổ, nguồn nguyên liệu các bạn có thể tìm ở đây
http://www.asciiworld.com/

Mỗi lần đón sai 1 kí tự thì sẽ thêm 1 kí tự vào missed_letters, ngược lại với đoán đúng

Thank for sharing!

About The Author


NamPNQ

Không biết viết gì ở đây :D

Quảng cáo