Hangman游戏源代码 --- python实现

#hangman.py

from PythonCard import model,dialog

import random

def find_letters(letter,a_string):
    locations = []
    start = 0

    while a_string.find(letter,start,len(a_string)) != -1:
        location = a_string.find(letter,start,len(a_string))
        locations.append(location)
        start = location + 1
    return locations

def replace_letters(string,locations,letter):
    new_string = ''
    for i in range(0,len(string)):
        if i in locations:
            new_string = new_string + letter
        else:
            new_string = new_string + string[i]
    return new_string


class Hangman(model.Background):
    def on_initialize(self,event):
        self.currentword = ""
        f = open("words.txt",'r')
        self.lines = f.readlines()
        f.close()
        self.new_game()

    def new_game(self):
        self.components.stYourGuesses.text = ""
        self.currentword = random.choice(self.lines)
        self.currentword = self.currentword.strip()
        self.components.stDisplayWord.text = ""

        for a in range(len(self.currentword)):
            self.components.stDisplayWord.text += "-"
        self.components.foot2.visible = False
        self.components.foot1.visible = False
        self.components.arm1.visible = False
        self.components.arm2.visible = False
        self.components.body.visible = False
        self.components.head.visible = False

    def on_btnGuessWord_mouseClick(self,event):
        result = dialog.textEntryDialog(self,'What is the word','Hangman','the word')
        self.components.stYourGuesses.text = self.components.stYourGuesses.text + " " + result.text + " "
        if (result.text).strip() == (self.currentword).strip():
            dialog.alertDialog(self,'You did it!','Hangman')
            self.new_game()
        else:
            self.wrong_guess()

    def wrong_guess(self):
        dialog.alertDialog(self,"WRONG!!",'Hangman')
        if self.components.head.visible == True:
            if self.components.body.visible == True:
                if self.components.arm1.visible == True:
                    if self.components.arm2.visible == True:
                        if self.components.foot1.visible == True:
                            if self.components.foot2.visible == True:
                                dialog.alertDialog(self,"You lost! Word was " + self.currentword,'Hangman')
                                self.new_game()
                            else:
                                self.components.foot2.visible = True
                        else:
                            self.components.foot1.visible = True
                    else:
                        self.components.arm2.visible = True
                else:
                    self.components.arm1.visible = True
            else:
                self.components.body.visible = True
        else:
            self.components.head.visible = True


    def on_btnGuessLetter_mouseClick(self,event):
        result = dialog.textEntryDialog(self,'enter the letter here:','Hangman','')
        guess = result.text

        if len(guess) == 1:
            self.components.stYourGuesses.text = self.components.stYourGuesses.text + " " + guess + " "
            if result.text in self.currentword:
                locations = find_letters(guess,self.currentword)
                self.components.stDisplayWord.text = replace_letters(self.components.stDisplayWord.text,locations,guess)
                if self.components.stDisplayWord.text.find('-') == -1:
                    dialog.alertDialog(self,'You win!!!!!','Hangman')
                    self.new_game()
            else:
                self.wrong_guess()
        else:
            dialog.alertDialog(self,'Type one letter only','Hangman')

def on_cmdNewGame_command(self,event):
    self.new_game()


app = model.Application(Hangman)
app.MainLoop()

 

资源文件

{'application':{'type':'Application',


          'name':'Template',
    'backgrounds': [
    {'type':'Background',
          'name':'bgTemplate',
          'title':u'Hangman',
          'size':(560, 373),
          

        'menubar': {'type':'MenuBar',
         'menus': [
             {'type':'Menu',
             'name':'menuFile',
             'label':'&File',
             'items': [
                  {'type':'MenuItem',
                   'name':'menuFileNewGame',
                   'label':'&New Game',
                   'command':'cmdNewGame',
                  },
                  {'type':'MenuItem',
                   'name':'menuFileExit',
                   'label':'E&xit',
                   'command':'exit',
                  },
              ]
             },
         ]
     },
         'components': [

{'type':'StaticText', 
    'name':'stYourGuesses', 
    'position':(28, 236), 
    'font':{'faceName': u'Tahoma', 'family': 'sansSerif', 'size': 10}, 
    'text':u'', 
    },

{'type':'StaticText', 
    'name':'StaticText1', 
    'position':(26, 200), 
    'font':{'faceName': u'Tahoma', 'family': 'sansSerif', 'size': 10}, 
    'text':u'Your Guesses:', 
    },

{'type':'StaticLine', 
    'name':'StaticLine2Copy', 
    'position':(86, 11), 
    'size':(4, 34), 
    'layout':'vertical', 
    },

{'type':'StaticLine', 
    'name':'StaticLine3', 
    'position':(87, 10), 
    'size':(69, 4), 
    'layout':'horizontal', 
    },

{'type':'StaticLine', 
    'name':'StaticLine2', 
    'position':(157, 10), 
    'size':(4, 160), 
    'layout':'vertical', 
    },

{'type':'StaticLine', 
    'name':'StaticLine1', 
    'position':(133, 171), 
    'size':(50, 4), 
    'layout':'horizontal', 
    },

{'type':'StaticText', 
    'name':'stDisplayWord', 
    'position':(247, 87), 
    'font':{'style': 'bold', 'faceName': u'Courier New', 'family': 'sansSerif', 'size': 14}, 
    'text':u'----------', 
    },

{'type':'Button', 
    'name':'btnGuessWord', 
    'position':(252, 128), 
    'size':(120, -1), 
    'label':u'Guess the word', 
    },

{'type':'Button', 
    'name':'btnGuessLetter', 
    'position':(250, 32), 
    'size':(120, -1), 
    'label':u'Guess a letter', 
    },

{'type':'StaticText', 
    'name':'foot2', 
    'position':(88, 115), 
    'enabled':False, 
    'font':{'faceName': 'Tahoma', 'family': 'sansSerif', 'size': 22}, 
    'text':u'\\', 
    },

{'type':'StaticText', 
    'name':'foot1', 
    'position':(69, 115), 
    'enabled':False, 
    'font':{'faceName': 'Tahoma', 'family': 'sansSerif', 'size': 22}, 
    'text':u'/', 
    },

{'type':'StaticLine', 
    'name':'body', 
    'position':(85, 65), 
    'size':(4, 55), 
    'font':{'style': 'bold', 'faceName': 'Tahoma', 'family': 'sansSerif', 'size': 8}, 
    'layout':'vertical', 
    },

{'type':'StaticLine', 
    'name':'arm2', 
    'position':(94, 79), 
    'size':(36, 4), 
    'layout':'horizontal', 
    },

{'type':'StaticLine', 
    'name':'arm1', 
    'position':(45, 79), 
    'size':(36, 4), 
    'layout':'horizontal', 
    },

{'type':'StaticText', 
    'name':'head', 
    'position':(75, 29), 
    'enabled':False, 
    'font':{'faceName': 'Tahoma', 'family': 'sansSerif', 'size': 20}, 
    'text':u'O', 
    },

] # end components
} # end background
] # end backgrounds
} }

 

posted @ 2014-07-28 20:45  lfsblack  阅读(4633)  评论(0编辑  收藏  举报