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实现的更多相关文章
- flappy bird游戏源代码揭秘和下载
转:http://blog.csdn.net/touchsnow/article/details/19071961 背景: 最近火爆全球的游戏flappy bird让笔者叹为观止,于是花了一天的时间山 ...
- Android游戏源代码合集(主要是AndEngine和Libgdx的)
近期在网络上看到有网友抱怨Android游戏源代码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源代码,以Eclipseproject的形式配置好环境,再陆续发出(某引擎避嫌,不在此 ...
- 飞天熊猫游戏源代码android文本
这款游戏是前一段时间完毕的一个项目,飞行熊猫游戏源代码android版.飞行熊猫基于cocos2d游戏引擎开发.包含了谷歌admob广告支持,没有不论什么版权问题.大家能够自由改动和上传应用商店. 1 ...
- 比較不错的一个ios找茬游戏源代码
找茬游戏源代码 .这个是一款很不错的ios找茬游戏源代码,该游戏的兼容性很好的.并且还能够支持ipad和iphone.UI界面设计得也很美丽,游戏源代码真的是一款很完美.并且又很完整的一款休闲类的游戏 ...
- ios打地鼠游戏源代码
打地鼠游戏源代码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源代码,这也是一款高质量的打地鼠游戏源代码,能够拥有逐步上升的关卡的设置,大家能够在关卡时设置一些商业化的模式来盈利的,很完美的 ...
- Beat 'Em Up Game Starter Kit (横版格斗游戏) cocos2d-x游戏源代码
浓缩精华.专注战斗! 游戏的本质是什么?界面?养成?NoNo! 游戏来源于对实战和比赛的模拟,所以它的本源就是对抗.就是战斗! 是挥洒热血的一种方式! 一个游戏最复杂最难做的是什么?UI?商城? ...
- 通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile
通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁 ...
- 通过游戏学python 3.6 第一季 第八章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数
通过游戏学python 3.6 第一季 第八章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账 ...
- 通过游戏学python 3.6 第一季 第二章 实例项目 猜数字游戏--核心代码--猜测次数 可复制直接使用 娱乐 可封装 函数
猜数字游戏--核心代码--猜测次数 #猜数字--核心代码--猜测次数 number=33 amount=3 count=0 while count<=amount: conversion ...
随机推荐
- .net mvc里AutoMapper更为便捷的使用方法
前言:AutoMapper的下载安装我就不多说了,网上百度一大堆.今天我就说说它的更为简单的使用,什么叫更为简单呢?按照一般的使用方法,我们首先建DTO,然后建每个对应的Profile,然后还要把每个 ...
- BZOJ1030: [JSOI2007]文本生成器(AC自动机)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5984 Solved: 2523[Submit][Status][Discuss] Descripti ...
- poj 2186 Popular Cows :求能被有多少点是能被所有点到达的点 tarjan O(E)
/** problem: http://poj.org/problem?id=2186 当出度为0的点(可能是缩点后的点)只有一个时就存在被所有牛崇拜的牛 因为如果存在有两个及以上出度为0的点的话,他 ...
- ABAP术语-Function Library
Function Library 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/14/1068642.html Central reposi ...
- 增删改查 报异常org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readO
可能是Spring配置文件 事务通知里面的方法 与实际方法不匹配 <tx:advice id="advice" transaction-manager="tran ...
- Lintcode算法
题目: 给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数. 样例 给出 [1, 20, 23, 4, 8],返回组合最大的整数应为8423201. 思路:直接交换两个数,然后判断交换之后的 ...
- 如何改变memcached默认的缓存时间?
我们在使用php的memcached的扩展来对memcached进行数据添加时,数据的有效时间有两种方式.如下图. 至于设置一个UNIX时间戳或 以秒为单位的整数(从当前算起的时间差)来说明 ...
- 《linux设备驱动开发详解》笔记——15 linux i2c驱动
结合实际代码和书中描述,可能跟书上有一定出入.本文后续芯片相关代码参考ZYNQ. 15.1 总体结构 如下图,i2c驱动分为如下几个重要模块 核心层core,完成i2c总线.设备.驱动模型,对用户提供 ...
- ruby Logger日志
1.logger创建 # 输出到标准输出 logger = Logger.new(STDERR) logger = Logger.new(STDOUT) # 输出到指定文件 logger = Logg ...
- POLYGON(动态规划)
学校老师布置的一道动规的题目,要求下次上课前AC.周一一放学就回家写,调试了一会儿OK了.在这边记录一下解题的思路和过程,也作为第一篇随笔,就是随便之一写,您也就随便之一看.有问题望你指出,多多包涵. ...