View Single Post
Join Date: Sep 2008
Posts: 10
Reputation: iamgame is an unknown quantity at this point 
Solved Threads: 0
iamgame's Avatar
iamgame iamgame is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #1
Sep 13th, 2008
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:

  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
Reply With Quote