"Guess the number" game pys60 code sharing

Source: Internet
Author: User

A few days ago, I posted a Python version of the "Guess number" game in my blog. It is a pure character interface and runs on a PC. It cannot be played anytime, anywhere, and it is not fun. Although I have also played the same game on Windows Mobile, the s60 system is currently the mainstream. My mobile phone is s60. This version has game instructions and shortcut key operations. It is very convenient to use on mobile phones. If you need the SIS version, generate and sign it by yourself.

First a few running images:

Open the game and display the Game Description.

This is the menu input screen

Do I want to quit when I am lazy to find the answer? This is full screen

The following are all the pys60 source code for this game:

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] # 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]

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)

# The number type query dialog box automatically returns the number 0123 directly to 123. Add 0 to prevent this situation.

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 () # Move the input cursor to the end

# Display results in different colors

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 # Make the characters accidentally entered by the player invisible (the same as the background color)

Text. Add (U "")

# Two scenarios of game termination

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 # Make the characters accidentally entered by the player invisible (the same as the background color)

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 # Make the characters accidentally entered by the player invisible (the same as the background color)

Text. Add (U "")

Def exit_game ():

If appuifw. Query (U' Are you sure to quit? ', 'Query '):

App_lock.signal ()

Def keybind ():

# Define the middle key to open the input dialog box...

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 ))

# Define several font colors

Defaultcolor, white, red, blue, green = [(0,128, 0), (255,255,255), (255, 0), (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 ()

Conclusion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.