#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. beep版千与千寻主题曲(转载自Ice_watermelon233)

    #include <bits/stdc++.h> #include <windows.h> #define qdo 262 #define qre 294 #define qm ...

  2. C/C++使用Socket通信UDP

    接收端 #include <stdio.h> #include <WinSock2.h> #pragma comment(lib,"WS2_32.lib") ...

  3. ABAP术语-Lock Object

    Lock Object 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/29/1085742.html Object type in the ...

  4. EF core Code First 简单的使用方法

    好吧,我又回来了,其实一直都想写一篇关于EF core 的文章去记录自己在开发时候遇到的问题. 为什么要使用EF框架呢,因为原始的ADO.NET需要编写大量的数据访问代码,所以使用EF会更方便.但是今 ...

  5. round函数在oracle和mysql中用法

    1.oracle和mysql通用方法 #round(字段1,小数位数) 四舍五入select round('11.123456',4);结果:11.1235 2.mysql的另外2种保留小数位数方法# ...

  6. raid概述与CentOS7.4中raid5的搭建与测试

    前言 一.raid的定义与作用 raid(独立冗余磁盘阵列).raid技术通过把多个硬盘设备组合成一个容量更大的,安全性更好的磁盘阵列.把数据切割成许多区段后分别放在不同的物理磁盘上,然后利用分散读写 ...

  7. python的pymysql模块简介

    一.介绍 在python中用pymysql模块来对mysql进行操作,该模块本质就是一个套接字客户端软件,使用前需要事先安装 pip3 install pymysql 二.操作简介 import py ...

  8. 在ReactNative中使用Typescript

    在ReactNative中使用Typescript 少侠放心,跟着我的这个步骤走,保你完美在RN项目中使用Typescript,废话不多说,走你 1.全局安装create-react-native-a ...

  9. python2.7入门---异常处理

        python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.我们可以使用该功能来调试python程序. 异常处理. 断言(Assertions).     首先来看py ...

  10. myeclipse 安装pydev插件后svn插件失效

    为了将python的IDE集成到myeclipse,按照教程安装了myeclipse插件pydev插件,但是按照完后发现,先前安装的svn不见了,解决办法如下: 1. 关闭myeclipse, 2. ...