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
} }

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

时间: 07-28

Hangman游戏源代码 --- python实现的相关文章

比較不错的一个ios找茬游戏源代码

找茬游戏源代码 .这个是一款很不错的ios找茬游戏源代码,该游戏的兼容性很好的.并且还能够支持ipad和iphone.UI界面设计得也很美丽,游戏源代码真的是一款很完美.并且又很完整的一款休闲类的游戏的. 演示环境:xcode 4.3编译通过.iphone4(ios5.1)真机执行正常 <ignore_js_op> <ignore_js_op> <ignore_js_op> <ignore_js_op> <ignore_js_op> 具体说明:h

ios打地鼠游戏源代码

打地鼠游戏源代码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源代码,这也是一款高质量的打地鼠游戏源代码,能够拥有逐步上升的关卡的设置,大家能够在关卡时设置一些商业化的模式来盈利的,很完美的一款完整版休闲类游戏源代码. 源代码下载:http://code.662p.com/view/6306.html <ignore_js_op> <ignore_js_op> <ignore_js_op> <ignore_js_op> 具体说明:http://io

高仿精仿安卓疯狂猜图游戏源代码

给大家分享一款不错的高仿精仿安卓疯狂猜图游戏源代码,喜欢的朋友能够下载看看. 游戏 <ignore_js_op> 源代码下载 http://code.662p.com/view/2960.html 具体说明:http://android.662p.com/thread-359-1-1.html

javascript开发植物大战僵尸网页版游戏源代码下载

原文:javascript开发植物大战僵尸网页版游戏源代码下载 源代码下载:http://www.zuidaima.com/share/1550463429364736.htm javascrip实现的植物大战僵尸

java swing实现俄罗斯方块游戏源代码下载

原文:java swing实现俄罗斯方块游戏源代码下载 源代码下载:http://www.zuidaima.com/share/1550463372790784.htm 源代码截图:

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能&#39;menufile

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile 1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile' 2 #!usr/bin/env python 3 #-*-c

Android游戏源代码合集(主要是AndEngine和Libgdx的)

近期在网络上看到有网友抱怨Android游戏源代码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源代码,以Eclipseproject的形式配置好环境,再陆续发出(某引擎避嫌,不在此列). 虽说这些游戏,主要是由Libgdx与AndEngine开发的源代码组成.但其实,能算游戏的,开源的,举凡有点价值的Android游戏源代码,小弟也会陆续收集(比方Replicaisland没用不论什么引擎,可代码有价值,所以这次也放进来了),更会在博客中一一给出. 只是,有两类游戏不在此列,

java swing开发打飞机的小游戏源代码下载

原文:java swing开发打飞机的小游戏源代码下载 源代码下载地址:http://www.zuidaima.com/share/1550463716084736.htm 这是我为了熟悉java设计模式而练习的代码.其中肯定有很多不足的地方.希望各位牛人们 能多多指点.谢谢

java swing开发扫雷游戏源代码

原文:java swing开发扫雷游戏源代码 源代码下载地址:http://www.zuidaima.com/share/1550463547886592.htm java 扫雷 游戏源码 源代码截图: