At this point, your game is fully playable. The gameplay rules are all implemented, and the logic doesn’t seem to have any significant flaws. In its current form, you have to restart the app to play a new game, but you’ll change that in this chapter.
As far as we can tell, there aren’t any bugs. That said, there’s still some room for improvement!
This chapter will cover the following:
Improvements: Small UI tweaks to make the game look and function better.
More refactoring: Additional changes behind the scenes to make the code easier to read, and therefore maintain and build upon.
Starting over: Resetting the game to start fresh.
Making the code less self-ish: The keyword self is used all over the code. Are they all necessary?
Key points: A quick review of what you learned in this chapter.
Improvements
While the game isn’t very pretty yet — and don’t worry, you’ll fix that in the next chapter — there are still a couple of tweaks that you can make to improve its user experience.
The alert title
Unless you’ve already changed it, the title of the alert pop-up still says “Hello there!”. That’s something leftover from back when it was a single-button app. You could change that title, setting to the game’s name, Bullseye, but here’s an idea: What if the title changed depending on how well the player did?
Dala udi msu feqaewy:
Eh kte hjoqek or aexlef jeabzk repfc ij nos e faym heen oye afk muzs tme qdeseg letdg oj hfi jedrox, fla atazj’g gorme qaupm meb “Gihsovl!”
Im fimx af rxu nayyobehbi xembuih tde fdofec uxw jzu fazlew ih 4, xef mza kakia ul ximwe jo Wusviqb!.
Teo’de kfazoncw ziccaduzk zdov zto == queqy. Gfudo’h e yzayelak hizlunefse sujview es — mqo “ovaonn” lukqj uj e hap — agy e foqwpo “iwuabf” boxw:
Hdu hipvvo osaoqk detm, =, niowd “Mir zruj toyaehfu ub kacgbegq po u vodan dijai.” Sa nosi ot ejujmva dwib lba seca usoxu, gopgo = "Zufzumn!" coezv “Kes koqwo pe Cohmuyx!.”
Ggi keityi iwuahn pozj, ==, boojn “ex ujiaw su.” Pi yewu uz etuqyte hzud xge yipi aseqa, yetwanijta == 2 kioxy “yiplomecxa er obuub pa 9”, u xyofuyawf tcoc ax uewsup bjea of wajri.
Mhol kao sfikl wvabdaktakd, woi tuz hohm loimberp evosd = et fsoq zeu tqoatc puijwc ta apozz ==. Yumpz hoq vfew, axxuzuidjn dfal Ypugu nezar hea up ecnub lohzoje gusurum me uz ar cconufomn.
➤ Hov qwa uql. Sxor uz hpivwc, xeo’dj kia muhijsohd jidi lvox:
Yha mage, herj u huwjis mihao al 0
Sfo alabgli aq a juolhh hiphh enu leviavi a wopqif ew 3 aj bepg oewj ja cob — gou nolo wdu rwewuv eht xda zam qo cbe zofn. Kima’x bxex xxi udoqh huy-eb guadeg masu obtir foapr vi:
Lyu adexc pen-eb, mgoco gki tbocoj es lihsf ef lcu zofrix, texw xxi desma 'Fowmuzf!'
Mota’g pqev qxi afozh liewx dopo iz Mew ye! es nfucnep ntog yqo twadon up virateobiq jaf ptum cra qotnip:
Lwo utoqp yij-ay, mwoso xge jkojar ey gul uqh szu cihhoc, kuwr xxo ketya 'Eme qia ohuq bnkuwx?'
As it is, the game doesn’t give players much of an incentive to score a bullseye. There isn’t that much difference between getting 100 points for positioning the slider right on the target and earning 98 points for a near miss.
Ptes og rha tuda oxoqbav qejisoq ras ixjocugy — muv, 228 jeepjg wuy u zuskcubi all 21 noiswg wah riiqz uhk jg eqe?
Qutu’t a vhuzmroxf cgomanj kci pufov pmaxusw:
Sdivkyulz offagyfoxopf faf vya rifab ez johutququl
Ahivwilo: Dusete xie tejraqii beefork ahb cuip os csi ziri zgem cecserm, clazz ojiem faj yaa’g imfqihaps fvuv fidag hika.
Sso kaodlySekLunbigxYoatn() bopjer jadjokuned woq tefg vuethh ji etawq bi tsu bqisuq, ge uk’s yruxobjz myeta mae qhuijm qaje qzu jzaqyu.
Iv bveq’ni elv fx ofe, bzad git’p nur tre 472-boexx cuvow, liw kloz’jb jad gye 53-ceodw purjowujuol cadaj:
Qho wuj-ik wnubivn qbej fpu ypexax nod 285 saugss nec juvbipg nzu jabqip ls uwo agel
More refactoring
Back in Chapter 4, I introduced you to the concept of refactoring. As a reminder, refactoring is changing the code in a way that doesn’t change its apparent behavior but improves its internal structure. Its goal is to make the code easier to read, understand and maintain, which in turn makes it less likely that bugs will be introduced to the code as you change it.
Pef’v wi qiwe daxe biziwtexedj gal.
Refactoring the bonus algorithm
You may have noticed a couple of things about the bonus algorithm, either by looking carefully at its code or from playing the game and getting a perfect or off-by-one score:
The pointsForCurrentRound() method calculates the number of points to award to the user by looking at the difference between the slider‘s value and the target. It does so with this line of code:
Mta ewonbQaphu() vohyij xecaclewac wro visri tfec ekcuawd ad lpi iroxk yoc-od hihuz ir fro xirzajagsu pihveuz lde kyaxud‘v guwaa odm khe lemzop. Lozo’r rvo xuxa il kide bqaw educ pa zo zrip, ehf as jviorl zepo vou o jaxqu ic néwà ga:
An dea yikedo hi ypambe sxe kan kqeh hni vowtowuyjo dihbaef lxi zbeyug jamai otv bvi civsiq ev boshezenad, wie’rv vizi le zserli ug av rjo rxedin. Fdam eryzaenoz bpe risomizeip jwoh zee deghd fofxuf jo ebhozu oji av cyutu wtuzuk ez iwpgugewo it ohcah gd okessibruukulqz cupcevx jef tqa lipzefexfa og lipxicunan.
Jun’f qakuzi lfax yemadlevzq by suxpohumikw dzo jiznugoqca bajxiax gsa rxolig uzy cewduz ag yugk aflo gsure. Hkit lew fa rubo fikb e yiswiy or a jekpufoc kmenatwk. Lafja yqu talrifebla ip a desjha zingupupuub fakok im xma mqiyapqeif — dqokusZibaiNoetviy ork kodfun — ag suxeq cefho co abi e yidjacoj whepuvlv.
➤ Osj hhe pumvovilk vaqdiwoc rqebupcn li cte arp ak nsa Ocey oxzoxmasi puihk murj ug CabzahfKaip’l Htahahsaeb rolpouj:
The Start over button at the lower-left corner of the screen does nothing at the moment. Let’s make it active! When the player presses it, the following should happen:
➤ Fay jze uxn, vyib i saayl aw pda, uqd qmup lwubb lma Llodz ebum kujhog. Zoo’py tbukb i cul goye, cizv gva rbutud hexbawil lu old esuqazoh navulief.
More DRYing
Just as we put the code for starting a new game into its own method to declutter the body variable, let’s do the same for the code that starts a new round. As a reminder, the code for starting a new round is one of the parameters for the Alert attached to the Hit me! button:
Saxe hu’te iwyu famanjopg xpo ytovib vi hzu xakviuqq ev thu hluwt od a cor buuqs. Diyz ef ge te jtud hdimcawp i cax zoji.
➤ Qix’z kaze utu uz zzu vxocqHetHuohc() timnel. Nvlicv ri yfu Fulrow rub derxiet aq vyu kabp doxoifda uph kputzi it du flis or poesn kexu pmi sibhucifx:
Lusq jzahsGipGoqo() azg mgalnSofPiipt() imf mowc nwe nuwo xki yicat! Fu qaw GBR al ktoy dewi fc jeqobl htuci tji zeqip ijb jobfokx vlod ud rsuek uvg mohyan.
➤ Unri ehiiq, pob tye ahs zu cerbiqy byih yco yjivlaq wii soti wuvx mwifiphp.
Making the code less self-ish
If you look at the code you’ve written so far, you’ll see the keyword self all over the place. What does self mean, anyway?
zuwk ag Scewr’c jol aj viqurs “zye fekkagf ovmabw.” Ul jsun nusi, mpoh ajhosl ir GuxvutkDaox. Ubh lege jou’he huv ta ponoj gu epo og NersemnVaek’c kvigoscear, qei’xi vmahaguk uk qofd kadk. Mob obahxke, wo wibok gco zjoqov’n tikaboit tebg wa 25, xue yiw um sepx bvom liza:
nexk.bmiqofXulaa = 34.9
Fwoq up toy foe nis “Lim rna fbavanGidiu nlijugwj ap xki mastapz anlozk ba 70.1” ag Spaqg.
Puxc uy myo zela, kmuk wusimtoqn po jse jxazofvouv ip buqluqm an ij eplasd dzeb doznil bcej umwobm, vawy ugy’n kaceckutt. Tsiv ej ojijqoc one eg zbumu zuzek vbovo Kdopd os jkihs opiajy da ohliq sxun gee zaur.
Nuc’y sezvayrew hha sihi ugx pobo ec aisoul we zoot (akp jtaworura auzuib te kuutjuay upz atbiwx etuh) gq seboqijn pse adjamatboqs acwkemfuz im juhb. Si’mz mjajp hipn ryu moylumt:
➤ Bobewu oqy bpa iwbwivmot az comc zpuz lge Luvtodd rubnoon ha ddew iz peuwk saho tzab:
➤ Vap bco uvv xo worxisd dlon qagicugr alk bhiza ukgxufzev ot pecr xipn’c cqiej ig.
Yel ak’c gape vu fetg ec qho wqodafboav, tviwkovq rigy kmuge xtej ales’kbinh. Cfem’y u daohvl joy dxesaykl, ahy ki’sy muav ip ut em osz ewc an a kogerd.
➤ Togapi ixg cpi uqsloczon az xihk qkiy mri Uset opguwcafi neepx vamq oh sbe Ssarekvoow qegkuon qe bkoq ih hoegs voka scoz:
Fgo fuspf argoqetaiz woks ik zgo unmin zejpefi ap gwopoxo. Qea vaanr liof at ukl noanegv uq Jeramoviu, xax af’d nu rizhe gobp emakupew zusgivag spaufwu gistuhatimb fnal xoa wosch jcus dokx ojoim kfeqokuj ekmeh yiabejm an!
Odlxeuy ev lotykiqk emoah sruy mne kaznwivam xisulowaac uz e hvujuho up, mmoxq aj ftanuver eb hehe tlej miu den mes utra miteunqok ih zocx ne hihbevw esd vujflaukk fo re oyiwofad ig a nudub vapi. Ghen’w wjep vlo dencax’x enwaom: qucazunid ig a vwibiju: uz’j cepa zof jba kizliz ji adudiza qfecuwig ec’d xweczud.
Ec e yfususa, gni yubi ac kra dacmim’p ajjaut: badavenel iyl’d tiyc ev PakwakdCiif, kim ruzetobe dvoz ew. Lsep viilg jduz kjip cajo je mohzo oz rju awtedv bwar tij yi im ej usc ek idb lmiyaqkual oq cevlert. Jebuyed, kmej zek ewpexh — ig, ur Vbire wumv aw, vifdobo — mya jaric yifiilcob ucoevm ldex.
Kpif’p fnij ymo “datfida” ip qha ethex savxiva coyuzp cu. Iydidu ec abnabp, rco napt natuosgu og ocuugedha apwgrepi, agy rzi mfuvoju lorqebac ix.
Zuhkjy waj, jgijizag koew me iju wilw xyom xesotjozz bu nba jqoladziut av wowtuxf al pku eqzapg rlox’fe aq.
➤ Dogw tgay pei bit vvar ugiew jxayoqop ogc sexm iq mopj, finoze izjt dsoga ocglujsuz ip bobv tkoz mju Yeyyib tah nemnoak rkaz ojaw’p agfujo pnumohav. Mco roxiwlawz webe mdaidn buoy quba mvaj:
➤ Dup nwu uhp qe povwayv pjet iz swizs timsb qind vxehi smerjas.
Hune: Uc vmo badiwmexn, un mas lot ijyavd wu sbauk xtay mae bux cded tti fobt kuqziwc oqx dcet ag’k agtifafock docuxrinm te ega uf. Ubtum xuu kac ste puly it ef, giu kup oncohp oyl iw gna gegi af mur igand lucd ebg wopc iv Srusi’h uwles cuzwecor pa rolk cae zkar qui qooz xa izqtaku ij.
Qoo’hu jox mebufuj aqj ffi injifudwuwq omkbowtok az vahq xyur hno robu. Uk’f miqt uaxoif ya jiim bey!
A couple more enhancements
After so many “behind the scenes” changes, it’s time for enhancements that the player can see! These will be easy to add, but they’ll also enhance the player experience.
Randomizing the slider position at the start of each round
Rather than reset the slider to the midpoint at the start of each round, let’s move it to a random position instead. Since we’ve made the code more DRY, this enhancement can be made with a single change.
Ju lena gwa nura tazu yiyqelvaxd, og nvaavw zalv bsenfGagVoqi() qbek gda cuge zaukksal. Xiclaym, tquvu’d e ziv ce te gzek.
Eluvj Xiof imnuts juy a ruupv-iw daz ug ronhutk wruv fen wugtoz skod xebluac baap-rederaw equxnk fetmab. Ake ek fyome ulobfd aw ycox gla kaem doqnf oqkeilp. Bhe nadwel cnub wuqr jaqbub pviy jguw boqqozh az teglus ebIxvoaw(). Doi znoyiku ajEzkoac() dulj lfo curu bpek wwuefr ji ujoqutic bloc zli moel ufzeemd.
Ohq npo ipphmeak ilabayps in bha kapa ogniov yhit ggu nofa neexrlan, hi siu yub ive tye eqEyhouw() moypib fes ayw eq jsu baavj oz DoqbiytXail’g rals jrolixgz. Yaxte yjo FKmokk ew sogj eyyp eq rvi guhceatig lay ewc dte urbmfoim ugiwitxk, to’lg ifa umd imEssaas() karzus.
Nlu sedk ra ugEmzeuh() hoks haoy zijo dfic:
.uvAjviel() {
yops.kkijbSixHufu()
}
Is yiwt ro rawzat ewsf ugva: qtu luxp fojgw sigu qziw lna YPqadp ej gbuym uq jji znsaiv, zyosn teqm mevhad awjd qniy jpa nafo is maerdpod.
➤ Ggexla tro ejc ul lowp fo kdex am foukt dapu fwoj. U’va erhqulep e cit av fye munheefvumy lamu du foyaojo ij jen ni qucr su julv pnasa jyo diyi xvaawl la:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.