尹科峰

尹科峰的博客

他的个人主页  他的博客

"猜数字"游戏PYS60版代码分享啦

尹科峰  2009年08月20日 星期四 13:53 | 1519次浏览 | 0条评论

前几天我在博客里贴出了Python版的"猜数字"游戏,那是纯字符界面的,而且是在PC上运行的,不能随时随地的玩,玩起来不过瘾。虽然我也做过一个在Windows Mobile上运行的同样的游戏,但S60系统才是真正的主流。我自己的手机就是S60的。这个版本有游戏说明,还有快捷键操作,在手机上使用起来应该算是十分方便的了。需要SIS版本的朋友就自己生成并签名吧。

先上几张运行时的图片:

      

         打开游戏,显示游戏说明                        游戏时的画面

   

                  这是菜单                                   输入画面

   

           偷懒查知答案时的情形                       想要退出吗?

以下是这个游戏的全部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哲思注册吗?现在 注册 !

暂时没有评论

Zeuux © 2024

京ICP备05028076号