#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. chromium之non_thread_safe

    先看看介绍 // A helper class used to help verify that methods of a class are // called from the same thre ...

  2. Struts2拦截器说明

    有关于Struts2的拦截器的原理 在此共设置了两个拦截器,firstInterception.SecondInterception package struts2_inteception; publ ...

  3. 【ppp-chap,pap,mp,mp-group】

    PPP链路端口验证(单){ PAP(明文): 主验证方: {local-user user_name:配置本地用户; password {simple||cipher}:配置验证密码; service ...

  4. 用bootstrap框架弄的网站。(首页)

    网站的每一处代码都加上注解,以便浏览! 效果图: <!doctype html>   <html lang="zh-cn">   <head> ...

  5. 【Hbase二】环境搭建

    此笔记仅用于作者记录复习使用,如有错误地方欢迎留言指正,作者感激不尽,如有转载请指明出处 Hbase环境搭建 Hbase环境搭建 hadoop为HA的Hbase配置 Zookeeper集群的正常部署并 ...

  6. ARM串口控制终端命令

    配置开发板eth0网络: # ifconfig eth0 10.70.12.168

  7. Java学习笔记十一:Java中的方法

    Java中的方法 一:什么是方法: 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 学过C语言或者其他语言的应该都知道函数这个东西,在Java中,其实方法就是函数,只不过叫法不同,在 ...

  8. hack游戏攻略(梦之光芒黑客小游戏)

    2019.2.11 继续玩~~还是黑客游戏闯关类的 地址:http://monyer.com/game/game1/ 直接查看页面代码: first.php就是了: 查看源代码: 这里尝试输入 两个空 ...

  9. go 操作数据库

    假设有了数据库,创建表 CREATE TABLE `userinfo` ( `uid` INT(10) NOT NULL AUTO_INCREMENT, //自增字段 `username` VARCH ...

  10. Python的入坑之路(1)

    (故事背景:由于涉及到机密的原因,暂时不方便透露,待后期再写.) 国庆长假过完之后,回来上班第二天下午,Boss跟龙哥把我叫了出去,问我要不要转人工智能.一脸懵逼的我,带着一脸懵逼听Boss说人工智能 ...