943,865 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2362
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 13th, 2008
0

Re: Projects for the Beginner

Expand Post »
Anyway, sometime back, I wrote a little and simple GUI application (using wPython) which I call the Jumbled Words game. This was my first step into wxPython. Not a serious project at all, but hey, a nice project for a Python newbie like me at that time.

You can download it (Windows executable; setup/installer) from here:
http://anurag.granularproject.org/20...ame-downloads/

Any suggestions to improve the code are welcome.

The source code goes like:

python Syntax (Toggle Plain Text)
  1. # Author: Anurag Bhandari | Contact: anurag.bhd@gmail.com
  2. #
  3. # Program: Jumbled Words Game | Version: 1.0 | Licence: GPLv2
  4. #
  5. # It's sure an interesting thing to do in free time
  6.  
  7. import wx
  8.  
  9. # First, we define a dictionary of jumbled words
  10. dict = {"special":"licapes", "verify":"rfvyie", "guava":"augav", "begin":"ngibe",
  11. "compression":"serniospmoc", "praying":"igyaprn", "linux":"uilxn",
  12. "opposite":"seopopit", "depricated": "cpdirdaete",
  13. "leonardo da vinci":"o draconian devil", "the mona lisa":"oh lame saint",
  14. "dreaming":"earimngd", "tormented":"nemortted", "flabbergasted": "tasedbgaberlaf",
  15. "meticuluos":"ecusoulmit", "rectify":"fitecry", "mobile":"ibelmo",
  16. "computer":"mertopcu", "granular":"lrgaruna", "metro":"torem",
  17. "outrageous":"gretsouauo", "nothing":"hotning", "appreciable":"celbepiapra", "vandalism":"misdanlav", "tropical":"ptorlica"}
  18.  
  19. def func():
  20. 'This is the core function of the program'
  21. # The result of popitem() method is a tuple
  22. # The following is an example of "sequence unpacking"
  23. word, jumbled = dict.popitem()
  24. return word, jumbled
  25.  
  26. def guess(event):
  27. ans = input_word.GetValue()
  28. if(ans == query[0]):
  29. result.SetLabel(label="Congrats! You got it right.")
  30. else:
  31. result.SetLabel(label="Sorry, wrong answer. Better luck next time!")
  32.  
  33. def next(event):
  34. # After a person clicks the Start button for the first time, this will happen
  35. nextButton.SetLabel("Next")
  36. guessButton.Enable()
  37. hintButton.Enable()
  38. input_word.SetValue("")
  39. # Unless we define the variable "query" as global, the function "guess" won't be able to access it
  40. global query
  41. query = func()
  42. if(dict!={}):
  43. question.SetLabel(label="The jumbled word is: "+query[1])
  44. result.SetLabel(label="Waiting for your input...")
  45. else:
  46. question.SetLabel(label="Game Over!")
  47. result.SetLabel(label="Yup, the game is finally over!")
  48. guessButton.Disable()
  49. nextButton.Disable()
  50. hintButton.Disable()
  51.  
  52. def hint(event):
  53. input_word.SetValue(query[0])
  54.  
  55. app = wx.App()
  56. # The definition of splash screen is done after an object for the wx.App class has been defined. Same applies to all the other widgets
  57. splash_image = wx.Image("splash.bmp", wx.BITMAP_TYPE_BMP)
  58. bmp = splash_image.ConvertToBitmap()
  59. wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1)
  60. wx.Yield()
  61. win = wx.Frame(None, title="Jumbled Words Game", size=(460, 345))
  62. win.SetIcon(wx.Icon('star.ico', wx.BITMAP_TYPE_ICO))
  63. win.CenterOnScreen()
  64. bkg = wx.Panel(win)
  65. guessButton = wx.Button(bkg, label='Guess')
  66. guessButton.Bind(wx.EVT_BUTTON, guess)
  67. guessButton.SetDefault()
  68. # Unless the player has pressed the Start button, the Guess button will be disabled
  69. guessButton.Disable()
  70. nextButton = wx.Button(bkg, label='Start')
  71. nextButton.Bind(wx.EVT_BUTTON, next)
  72. hintButton = wx.Button(bkg, label='Hint')
  73. hintButton.Bind(wx.EVT_BUTTON, hint)
  74. hintButton.Disable()
  75. input_word = wx.TextCtrl(bkg)
  76. question = wx.StaticText(bkg, label="Welcome to jumbled words game\nTotal words: 25", style=wx.ALIGN_CENTER)
  77. # We define some stylish fonts for the welcome message / game questions
  78. font = wx.Font(pointSize=18, family=wx.DECORATIVE, style=wx.NORMAL, weight=wx.NORMAL)
  79. question.SetFont(font)
  80. result = wx.StaticText(bkg, label="Waiting for the initial result...", style=wx.ALIGN_CENTER)
  81. hbox = wx.BoxSizer()
  82. hbox.Add(input_word, proportion=1, flag=wx.EXPAND)
  83. hbox.Add(guessButton, proportion=0, flag=wx.LEFT, border=5)
  84. hbox.Add(nextButton, proportion=0, flag=wx.LEFT, border=5)
  85. hbox.Add(hintButton, proportion=0, flag=wx.LEFT, border=5)
  86. vbox = wx.BoxSizer(wx.VERTICAL)
  87. vbox.Add(question, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  88. vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
  89. vbox.Add(result, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
  90. bkg.SetSizer(vbox)
  91. win.Show()
  92. app.MainLoop()
Editor: Was moved to the main Python forum, to allow posting of improvements!
Last edited by vegaseat; Sep 13th, 2008 at 6:53 pm. Reason: was moved
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamgame is offline Offline
9 posts
since Sep 2008
Sep 14th, 2008
0

Re: Improve scrambled text game

It would be cool if you could have a text file full of all of the words so the user could add their own.

Your text file could look like this:
Python:yothnp
spam:apms

This was the user could add their own and you could just import it to a dictionary at the start like:
python Syntax (Toggle Plain Text)
  1. f = open("Words.txt")
  2. for thing in f.split(":"):
  3. dictionary[thing[0]]=thing[1]
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Sep 14th, 2008
0

Re: Improve scrambled text game

I was thinking on the same lines, Paul. Thanks for the suggestion and the code.

I'll implement this in the program as soon as I can and post an update here after that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamgame is offline Offline
9 posts
since Sep 2008
Sep 14th, 2008
0

Re: Improve scrambled text game

Nice game!
Just a few cosmetic things, there is a lot of empty space, so I would set the height of the frame to 180, and also use a little color with:
bkg.SetBackgroundColour('yellow')
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 14th, 2008
0

Re: Improve scrambled text game

Ok, cosmetics are in; I'll note them too.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamgame is offline Offline
9 posts
since Sep 2008
Sep 14th, 2008
0

Re: Improve scrambled text game

You can also let Python do the jumbling of the words and create the dictionary of word:jumble pairs:
python Syntax (Toggle Plain Text)
  1. # create a dictionary of random jumbled words for a game
  2.  
  3. import random
  4.  
  5. def jumble_word(word):
  6. # create a list of characters of the word
  7. char_list = list(word)
  8. # shuffle sequence in place
  9. random.shuffle(char_list)
  10. # joint to form a word again
  11. return "".join(char_list)
  12.  
  13. # add more words to the list as needed
  14. words = ['armada', 'bubble', 'letter', 'banana', 'radio', 'hammer',
  15. 'spark', 'pearl', 'target', 'zappy', 'zipper', 'organist',
  16. 'kitchen', 'ruler', 'motorist', 'polar', 'garage', 'window']
  17.  
  18. # create a list of jumbled words
  19. jumbles = []
  20. for word in words:
  21. jumbles.append(jumble_word(word))
  22.  
  23. # create a dictionary from the two list
  24. words_dict = dict(zip(words, jumbles))
  25.  
  26. print words_dict
  27.  
  28. """
  29. possible output -->
  30. {'polar': 'aropl', 'target': 'rgaett', 'ruler': 'rleur',
  31. 'garage': 'eaaggr', 'bubble': 'beblub', 'window': 'nowdiw',
  32. 'pearl': 'aprel', 'zipper': 'pperzi', 'radio': 'idoar',
  33. 'motorist': 'rtotimos', 'letter': 'rettel', 'zappy': 'pzpay',
  34. 'armada': 'ardaam', 'spark': 'akprs', 'kitchen': 'theinck',
  35. 'hammer': 'mrameh', 'banana': 'bnanaa', 'organist': 'soiatgnr'}
  36. """
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 15th, 2008
0

Re: Improve scrambled text game

Automatic jumbling seems like a really nice idea. I wanted to do that earlier also, but had no clue on how to begin.

The module "random" seems an interesting lot to play with.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamgame is offline Offline
9 posts
since Sep 2008
Sep 21st, 2008
0

Re: Improve scrambled text game

I think at one time you asked about module MD5 to send on a password, you might want to take a look at this post:
http://www.daniweb.com/forums/post220840-56.html
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 21st, 2008
0

Re: Improve scrambled text game

Click to Expand / Collapse  Quote originally posted by vegaseat ...
I think at one time you asked about module MD5 to send on a password, you might want to take a look at this post:
http://www.daniweb.com/forums/post220840-56.html
Well yes, I did ask about md5 once and the question was something like "is it possible to reverse md5 hashes to original text?". Now, what I know is that this isn't possible, but you wrote about "decoding" the password encrypted using md5 in one of your posts:

Quote ...
With more and more folks thinking it their business to snoop around in your e-mail, write a little Python program to encode regular text. It is assumed that you will send this text to a friend, who has Python installed and can run a program you send beforehand to decode the encrypted text (after using the correct password of course). Use Python module md5 to encrypt the password too.
Now, as we can see in the program (http://www.daniweb.com/forums/post220840-56.html), the person in not actually decoding the md5 hash, but is comparing his own "md5"ed input with the md5 hash generated by the other person at first place.

So, I guess I can assume that md5 is not reversible, right?
Last edited by iamgame; Sep 21st, 2008 at 6:13 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamgame is offline Offline
9 posts
since Sep 2008
Sep 21st, 2008
0

Re: Improve scrambled text game

An alternative to using a password to communicate with your friend is to send them a picture of your favorite pet and to use the sha128 digest of the picture as a key, together with a standard encryption algorithm (RSA or a better one). This way, only the people with your pet's picture can decipher your messages, which is quite secure I think.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: pygame zoom/rotate
Next Thread in Python Forum Timeline: Import Main problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC