Python Programming: An Introduction to Computer Science

(Nora) #1
216 CHAPTER12. OBJECT-ORIENTEDDESIGN

Dice: [3, 2, 1, 1, 1]
Enter list of whichto change ([] to stop) [0,1]
Dice: [5, 6, 1, 1, 1]
Enter list of whichto change ([] to stop) [0,1]
Dice: [1, 5, 1, 1, 1]
Four of a Kind. You win$15.
You currently have$109.
Do you wish to try yourluck? n


Thanks for playing!


Youcanseehow thisinterfaceprovidesjustenoughsothatwecantestoutthemodel.Infact,we’ve gota
gamethat’s alreadyquitea bitoffuntoplay!


12.3.5 Developinga GUI


Now thatwehave a workingprogram,let’s turnourattentionto a nicergraphicalinterface.Ourfirststepmust
betodecideexactlyhow wewantourinterfacetolookandfunction.Theinterfacewillhave tosupportthe
variousmethodsfoundinthetext-basedversionandwillalsoprobablyhave someadditionalhelpermethods.


DesigningtheInteraction


Let’s startwiththebasicmethodsthatmustbesupportedanddecideexactlyhowinteractionwiththeuser
willoccur. Clearly, ina graphicalinterface,thefacesofthediceandthecurrentscoreshouldbecontinuously
displayed. ThesetDiceandsetMoneymethodswillbeusedtochangethosedisplays. Thatleaves
oneoutputmethod,showResult, thatweneedtoaccommodate.Onecommonwaytohandlethissortof
tranisentinformationis witha messageat thebottomofthewindow. Thisis sometimescalledastatusbar.
To getinformationfromtheuser, wewillmake useofbuttons.InwantToPlay, theuserwillhave to
decidebetweeneitherrollingthediceorquitting.We couldinclude“RollDice”and“Quit”buttonsforthis
choice.Thatleavesuswithfiguringouthow theusershouldchoosedice.
To implementchooseDice, wecouldprovidea buttonforeachdieandhave theuserclickthebuttons
forthedicethey wanttoroll. Whentheuseris donechoosingthedice,they couldclickthe“RollDice”
buttonagaintorolltheselecteddice. Elaboratingonthisidea,it wouldbeniceif weallowedtheuserto
changehisorhermindwhileselectingthedice.Perhapsclickingthebuttonofa currentlyselecteddiewould
causeit tobecomeunselected.Theclickingofthebuttonwillserve asa sortoftogglethatselects/unselects
a particulardie.Theusercommitstoa certainselectionbyclickingon“RollDice.”
OurvisionforchooseDicesuggestsa coupleoftweaksfortheinterface.First,weshouldhave some
wayofshowingtheuserwhichdicearecurrentlyselected.Therearelotsofwayswecoulddothis.One
simpleapproachwouldbetochangethecolorofthedie.Let’s “grayout”thepipsonthediceselectedfor
rolling. Second,weneeda goodwayfortheusertoindicatethatthey wishtostoprolling.Thatis,they
wouldlike thedicescoredjustasthey stand.We couldhandlethisbyhavingthemclickthe“RollDice”
buttonwhennodiceareselected,henceaskingtheprogramtorollnodice.Anotherapproachwouldbeto
providea separatebuttontoclickthatcausesthedicetobescored. Thelatterapproachseemsa bitmore
intuitive/informative. Let’s adda “Score”buttontotheinterface.
Now wehave a basicideaofhow theinterfacewillfunction.We stillneedtofigureouthow it willlook.
Whatis theexactlayoutofthewidgets?Figure12.2is a sampleofhow theinterfacemightlook.I’msure
thoseofyouwitha moreartisiticeyecancomeupwitha morepleasinginterface,butwe’ll usethisoneas
ourworkingdesign.


ManagingtheWidgets


Thegraphicalinterfacethatwearedevelopingmakesuseofbuttonsanddice. Ourintentistoresuethe
ButtonandDieViewclassesforthesewidgetsthatweredevelopedinpreviouschapters.TheButton
classcanbeusedasis,andsincewehave quitea numberofbuttonsto manage,wecanusea listofButtons,
similartotheapproachweusedinthecalculatorprogramfromChapter11.

Free download pdf