#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. MySql学习笔记06

    课程回顾 一对一关联 案例1:查询每个员工的名字和主管领导的名字 select e.ename 员工姓名,m.ename 领导姓名 from emp e join emp m on e.mgr=m.e ...

  2. canvas画布——画八卦图

    实例 创建一个圆形: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d") ...

  3. MAC系统如何显示隐藏文件解决方法

    苹果Mac OS 操作系统下,隐藏文件默认为隐藏状态,隐藏文件是否显示有多种方法可以设置. 方法一: 打开终端,输入命令行 1.显示Mac隐藏文件的命令: defaults write com.app ...

  4. Leecode刷题之旅-C语言/python-104二叉树最大深度

    /* * @lc app=leetcode.cn id=104 lang=c * * [104] 二叉树的最大深度 * * https://leetcode-cn.com/problems/maxim ...

  5. Vee-validate学习

    Vee-validate使用方法 首先引入 <script src="https://cdn.bootcss.com/vee-validate/2.0.9/vee-validate.j ...

  6. Java8新特性(二)——强大的Stream API

    一.强大的Stream API 除了Lambda表达式外,Java8另外一项重大更新便是位于java.util.stream.*下的Stream API Stream 是 Java8 中处理集合的关键 ...

  7. kudu是什么

    Apache Kudu Overview 建议配合[Apache Kudo]审阅本文(http://kudu.apache.org/overview.html) 数据模式 Kudo是一个列式存储的用于 ...

  8. P2340 奶牛会展(状压dp)

    P2340 奶牛会展 题目背景 奶牛想证明它们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行 了面试,确定了每头奶牛的智商和情商. 题目描述 贝西有权选择让哪些奶牛参加展览.由 ...

  9. Thymeleaf 常用th标签基础整理

    (一)Thymeleaf 是个什么?      简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...

  10. Vue-router用法

    #全局守卫- router.beforeEach(to,from,next){} #全局后置钩子- router.afterEach(to,from){} #路由独享守卫- beforeEnter(t ...