尹科峰 2009年08月20日 星期四 13:53 | 1611次浏览 | 0条评论
前几天我在
先上几张运行时的图片:
打开游戏,显示游戏说明 游戏时的画面
这是菜单 输入画面
偷懒查知答案时的情形 想要退出吗?
以下是这个游戏的全部Pys60源码了:
import appuifw
import e32
import random
from key_codes import *
Howtoplay=u"""\tHOW TO PLAY
Guess a number with 4 different digits in 8 choices.
The guess result shows like 'xAyB' means there are x digits and positions are all correct,and y digits are right but positions not.When '4A0B',Congratulations,You win!
Choose [New game] in menu to start.Try your best!"""
class Bingle:
"""
Generate a random answer,and record guess times,and judge it
"""
A,B=(0,0) #Match falg
AttemptTimes=8 #Geuss times
Answer=[0,0,0,0] #The Answer
def GetAnswerStr(self):
AnswerStr=""
for data in self.Answer:
AnswerStr=AnswerStr + str(data)
return AnswerStr
def BuildAnswer(self):
random.seed()
while 1:
digit = random.randint(0, 9999)
self.Answer[0]=digit/1000
self.Answer[1]=digit%1000/100
self.Answer[2]=digit%100/10
self.Answer[3]=digit%10
if self.Answer[0]!=self.Answer[1] and self.Answer[0]!=self.Answer[2] and self.Answer[0]!=self.Answer[3] and self.Answer[1]!=self.Answer[2] and self.Answer[1]!=self.Answer[3] and self.Answer[2]!=self.Answer[3]:
return
def __init__(self):
Answer=[0,0,0,0]
AttemptTimes=0
self.BuildAnswer()
def IsTryStringOK(self,TryString):
if TryString.isdigit() and len(TryString)==4:
if TryString[0]!=TryString[1] and TryString[0]!=TryString[2] and TryString[0]!=TryString[3] and TryString[1]!=TryString[2] and TryString[1]!=TryString[3] and TryString[2]!=TryString[3]:
return 1
return 0
def Judge(self,TryString):
for i in range(4):
if(TryString[i]==str(self.Answer[i])):
self.A=self.A+1
else:
for j in range(4):
if(TryString[i]==str(self.Answer[j])):
self.B=self.B+1
ReturnStr = "%dA%dB"%(self.A,self.B)
self.A=0
self.B=0
return ReturnStr
#guess the number
def guess(DefaultValue=0):
global B,IsGameOver
if IsGameOver:
ContinueGame(u"Game is over!\nStart a new game?")
return
data = appuifw.query(u"4 different digits:", "number",DefaultValue)
if data!=None:
data=str(data)
#number类型的query对话框会自动将0123这种的数字直接返回为123.在前面补0以防止这种情况发生
if len(data)==3:
data=u"0"+data
if B.IsTryStringOK(data):
JudgeResult = B.Judge(data)
B.AttemptTimes = B.AttemptTimes - 1
Text.set_pos(Text.len())#将输入光标移到最后
#用不同的颜色显示结果
Text.color=Green
Text.add("\n"+str(8 - B.AttemptTimes).encode("utf_8")+u": ")
Text.color=Blue
Text.add(data+u" ")
Text.color=Red
Text.add(JudgeResult+u" ")
Text.color=White#让玩家无意中输入的字符不可见(白色和背景色一样)
Text.add(u" ")
#游戏结束的两种情况
msg=""
if JudgeResult=="4A0B":
msg=u"Congratulations!You win!\nDo you want to continue?"
ContinueGame(msg)
elif B.AttemptTimes == 0:
msg=u"Game over!\nAttempt 8 times.\nThe answer is "+ B.GetAnswerStr()+"\nDo you want to continue?"
ContinueGame(msg)
else:
appuifw.note(u"Invalid number,try again please!", "error")
guess()
def ContinueGame(msg):
global IsGameOver,B
IsGameOver=True
if appuifw.query(msg, "query"):
new_game()
else:
exit_game()
def new_game():
global data,B,IsGameOver
Text.color=DefaultColor
Text.set(u"Guess Result List: ")
Text.color=White #让玩家无意中输入的字符不可见(白色和背景色一样)
Text.add(u" ")
B=Bingle()
IsGameOver=False
guess()
def answer():
global B,IsGameOver
if IsGameOver:
ContinueGame(u"Game is over!\nStart a new game?")
return
appuifw.note(u"The answer is: " + B.GetAnswerStr(), "info")
def SwitchScreen():
if appuifw.app.screen=="full":
appuifw.app.screen="normal"
else:
appuifw.app.screen="full"
return
def HowToPlay():
Text.color=DefaultColor
Text.set(Howtoplay)
Text.color=White#让玩家无意中输入的字符不可见(白色和背景色一样)
Text.add(u" ")
def exit_game():
if appuifw.query(u'Are you sure to quit?','query'):
app_lock.signal()
def KeyBind():
#定义中键打开输入对话框...
Text.bind(EKeySelect,lambda:guess())
Text.bind(EKeyStar,lambda:SwitchScreen())
Text.bind(EKeyHash,lambda:answer())
Text.bind(EKeyBackspace,lambda:new_game())
Text.bind(EKeyYes,lambda:HowToPlay())
Text.bind(EKey9,lambda:guess(9))
Text.bind(EKey8,lambda:guess(8))
Text.bind(EKey7,lambda:guess(7))
Text.bind(EKey6,lambda:guess(6))
Text.bind(EKey5,lambda:guess(5))
Text.bind(EKey4,lambda:guess(4))
Text.bind(EKey3,lambda:guess(3))
Text.bind(EKey2,lambda:guess(2))
Text.bind(EKey1,lambda:guess(1))
Text.bind(EKey0,lambda:guess(0))
#定义几种字体颜色
DefaultColor,White,Red,Blue,Green=[(0,128,0),(255,255,255),(255,0,0),(0,0,255),(0,128,128)]
appuifw.app.title = u"Lucker Bingle" # Title
app_lock = e32.Ao_lock()
Text=appuifw.Text()
appuifw.app.body=Text
Text.focus=False
appuifw.app.menu=[(u'Guess (OK)',guess),
(u'New game (C)',new_game),
(u'Answer (#)',answer),
(u'Switch Screen (*)',SwitchScreen),
(u'How to play (Dail)',HowToPlay),
(u'Exit',exit_game)]
KeyBind()
HowToPlay()
IsGameOver=True
appuifw.app.exit_key_handler=exit_game
app_lock.wait()
《完》
Zeuux © 2024
京ICP备05028076号
暂时没有评论