#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实现的更多相关文章

  1. flappy bird游戏源代码揭秘和下载

    转:http://blog.csdn.net/touchsnow/article/details/19071961 背景: 最近火爆全球的游戏flappy bird让笔者叹为观止,于是花了一天的时间山 ...

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

    近期在网络上看到有网友抱怨Android游戏源代码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源代码,以Eclipseproject的形式配置好环境,再陆续发出(某引擎避嫌,不在此 ...

  3. 飞天熊猫游戏源代码android文本

    这款游戏是前一段时间完毕的一个项目,飞行熊猫游戏源代码android版.飞行熊猫基于cocos2d游戏引擎开发.包含了谷歌admob广告支持,没有不论什么版权问题.大家能够自由改动和上传应用商店. 1 ...

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

    找茬游戏源代码 .这个是一款很不错的ios找茬游戏源代码,该游戏的兼容性很好的.并且还能够支持ipad和iphone.UI界面设计得也很美丽,游戏源代码真的是一款很完美.并且又很完整的一款休闲类的游戏 ...

  5. ios打地鼠游戏源代码

    打地鼠游戏源代码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源代码,这也是一款高质量的打地鼠游戏源代码,能够拥有逐步上升的关卡的设置,大家能够在关卡时设置一些商业化的模式来盈利的,很完美的 ...

  6. Beat 'Em Up Game Starter Kit (横版格斗游戏) cocos2d-x游戏源代码

    浓缩精华.专注战斗! 游戏的本质是什么?界面?养成?NoNo!    游戏来源于对实战和比赛的模拟,所以它的本源就是对抗.就是战斗! 是挥洒热血的一种方式! 一个游戏最复杂最难做的是什么?UI?商城? ...

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

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

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

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

  9. 通过游戏学python 3.6 第一季 第二章 实例项目 猜数字游戏--核心代码--猜测次数 可复制直接使用 娱乐 可封装 函数

      猜数字游戏--核心代码--猜测次数   #猜数字--核心代码--猜测次数 number=33 amount=3 count=0 while count<=amount: conversion ...

随机推荐

  1. ES6 语法学习总结

    第一节:什么是ES6?   ES6是什么?跟JavaScript有什么关系? JavaScrip由三部分组成:分别是ECMAScript,BOM和DOM. 1)由此看出,ECMAScript是Java ...

  2. 数论(一)LOJ1282

    1.题目来源LOJ1282 You are given two integers: n and k, your task is to find the most significant three d ...

  3. 构建高可靠hadoop集群之4-权限指引

    此文翻译自http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/HdfsPermissionsGuide.html ...

  4. web前端总结面试问题<CSS&HTML问题>

    一个父元素div,一个未知宽度.高度的子元素div [上下左右居中方法总结] //1.position布局,position设为absolute,其他同情景一 2.display:table 父级元素 ...

  5. PHP中对字符串的一些操作

    php中判断字符串在另一个字符串中是否存在(strpos): if(strpos('www.baidu.com', 'www') !== false){ // 存在 }else{ // 不存在 } p ...

  6. PHP一些常用魔术方法

    魔术方法                          调用方法                                     作用__set                   有两个 ...

  7. laravel自定义返回错误方法

    返回视图传递错误信息 function withInfoErr($msg){ return back()->with('error',$msg); } 返回视图提示消息 function wit ...

  8. Laravel -- Blade模板

    {{--流程控制--}} @if($name == '1') this is 1 @elseif($name == '2') this.is 2 @else who am i? @endif @for ...

  9. HBase学习(三):数据模型

    和传统的关系型数据库类似,HBase以表(Table)的方式组织数据.HBase的表由行(Row)和列(Column)共同构成,与关系型数据库不同的是HBase有一个列族(ColumnFamily)的 ...

  10. go web cookie和session

    cookie是存储在浏览器端,session是服务器端 cookie是有时间限制的,分会话cookie和持久cookie,如果不设置时间,那周期就是创建到浏览器关闭为止.这种是会话cookie,一般保 ...