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/2008/05/jumbled-words-game-downloads/
Any suggestions to improve the code are welcome. :idea:
The source code goes like:
# Author: Anurag Bhandari | Contact: anurag.bhd@gmail.com
#
# Program: Jumbled Words Game | Version: 1.0 | Licence: GPLv2
#
# It's sure an interesting thing to do in free time
import wx
# First, we define a dictionary of jumbled words
dict = {"special":"licapes", "verify":"rfvyie", "guava":"augav", "begin":"ngibe",
"compression":"serniospmoc", "praying":"igyaprn", "linux":"uilxn",
"opposite":"seopopit", "depricated": "cpdirdaete",
"leonardo da vinci":"o draconian devil", "the mona lisa":"oh lame saint",
"dreaming":"earimngd", "tormented":"nemortted", "flabbergasted": "tasedbgaberlaf",
"meticuluos":"ecusoulmit", "rectify":"fitecry", "mobile":"ibelmo",
"computer":"mertopcu", "granular":"lrgaruna", "metro":"torem",
"outrageous":"gretsouauo", "nothing":"hotning", "appreciable":"celbepiapra", "vandalism":"misdanlav", "tropical":"ptorlica"}
def func():
'This is the core function of the program'
# The result of popitem() method is a tuple
# The following is an example of "sequence unpacking"
word, jumbled = dict.popitem()
return word, jumbled
def guess(event):
ans = input_word.GetValue()
if(ans == query[0]):
result.SetLabel(label="Congrats! You got it right.")
else:
result.SetLabel(label="Sorry, wrong answer. Better luck next time!")
def next(event):
# After a person clicks the Start button for the first time, this will happen
nextButton.SetLabel("Next")
guessButton.Enable()
hintButton.Enable()
input_word.SetValue("")
# Unless we define the variable "query" as global, the function "guess" won't be able to access it
global query
query = func()
if(dict!={}):
question.SetLabel(label="The jumbled word is: "+query[1])
result.SetLabel(label="Waiting for your input...")
else:
question.SetLabel(label="Game Over!")
result.SetLabel(label="Yup, the game is finally over!")
guessButton.Disable()
nextButton.Disable()
hintButton.Disable()
def hint(event):
input_word.SetValue(query[0])
app = wx.App()
# 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
splash_image = wx.Image("splash.bmp", wx.BITMAP_TYPE_BMP)
bmp = splash_image.ConvertToBitmap()
wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1)
wx.Yield()
win = wx.Frame(None, title="Jumbled Words Game", size=(460, 345))
win.SetIcon(wx.Icon('star.ico', wx.BITMAP_TYPE_ICO))
win.CenterOnScreen()
bkg = wx.Panel(win)
guessButton = wx.Button(bkg, label='Guess')
guessButton.Bind(wx.EVT_BUTTON, guess)
guessButton.SetDefault()
# Unless the player has pressed the Start button, the Guess button will be disabled
guessButton.Disable()
nextButton = wx.Button(bkg, label='Start')
nextButton.Bind(wx.EVT_BUTTON, next)
hintButton = wx.Button(bkg, label='Hint')
hintButton.Bind(wx.EVT_BUTTON, hint)
hintButton.Disable()
input_word = wx.TextCtrl(bkg)
question = wx.StaticText(bkg, label="Welcome to jumbled words game\nTotal words: 25", style=wx.ALIGN_CENTER)
# We define some stylish fonts for the welcome message / game questions
font = wx.Font(pointSize=18, family=wx.DECORATIVE, style=wx.NORMAL, weight=wx.NORMAL)
question.SetFont(font)
result = wx.StaticText(bkg, label="Waiting for the initial result...", style=wx.ALIGN_CENTER)
hbox = wx.BoxSizer()
hbox.Add(input_word, proportion=1, flag=wx.EXPAND)
hbox.Add(guessButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(nextButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(hintButton, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(question, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
vbox.Add(result, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()
Editor: Was moved to the main Python forum, to allow posting of improvements!