Thisis a nodtooneofmyfavoriteYouTubeiswho's justgot a displaythatworksinthisresolution, and I'm hopingthatit'llimplementthisrubbishonhisdisplaynow.
Thereisn't verymuchcodeforFlappyBird, so I thinkit's usefultounderstandwhatthecodeshoulddobeforewestartcodingit.
We'llstartwiththeflappyphysics.
Thisis a birdonourbirds.
Positionisrepresentedbyan X and A Y coordinate.
Ifwedifferentiatewithrespecttotimethepositionofthebirdwegetthe X and D y, whichisalsovelocity, andifwetakethesecondderivativeofftheposition I d Squared X and D squared y Wehavetheaccelerationand I'vecoveredposition, velocityandaccelerationatgreatlengthinthemuchmoreinterestingcodeityourselfasteroidsvideo.
So I suggestyougoandhave a lookatthat.
Ifyou'renotfamiliarwiththeseterms, wecansimplifythisfurtherbecauseourbirdisn't actuallygoingtomovealongthe X direction.
Inthisinstance, F gravityisconsideredjerkorjolt, dependingonwhereyoufilmon, becausewedon't wantthebirdgettingoutofcontrol.
I'm goingtoputin a hardconstraintthatifthebirdsaccelerationisevergreaterthangravity, becausethisisanaccumulationoffsomeforce, then I'm goingtocapittobegravity.
However, whenthebirdflapsitswings, I'm goingtosettheaccelerationtozero.
But I'm goingtosetthevelocitydirectlytobe a proportionoffthegravity, andyoucansee I'veincluded a minus.
This, ineffect, removesanyfallingduetogravityandartificiallyforcesthebirdin a upwardthrust.
Beforewedoanydrawing, I'llclearthescreen.
Let's have a lookhowthisfeelssothebirdfallsanditacceleratesitsfallson.
As I smashedthespacekey, thebirdflapsitswingsanditincreasesitsdistanceoffthescreen.
Thedownsideis I canrepeatedlypressspaceandwecouldseethebirdmovessmoothlyupwards.
Thisistoosimplefortheplayer, Solet's makethismoredifficultbyaddinganadditionalclausethatthespacekeeppressisonlyvalidwhenthebirdsvelocityisgreaterthan a proportionofthegravityfallingdown.
Now.
When I smashedthespacekey, wecanseethebirdhastofall a littlebitbeforeitcontinuesonitsupwardjourney.
Nowwe'vedealtwiththephysics.
Let's dealwithworld.
I'm goingtocreatetwovariables F sectionwidth, whichjustdefinesthewidthofthesectioninthefloatingpointdomainon a listoffinterjuice, whicharetheobstacleheightson.
Andthat's becausedependingonthespeedoftheplatformlevelpositionmayhaveincreasedin a stepsizesufficientenoughtomakethisresetlook a littlebitjerky.
Atthispoint, thesectionatthefrontofthelistwillhavegoneoffthelefthandsideofthescreen, so I'm goingtopopitfromthefrontofthelist.
We'redonewithit, thengoingtochoose a randomvaluetorepresentthesizeoftheobstacle.
And I don't wantmyobstaclestobepreciselyonthezeroroworthescreenheightrow.
So I'm adding a minus 20 inheretomakesurethatthereisalwayssomeobstacleonthescreen, andthenjusttomakeitinterestingfortheplayer I'm goingtotest, foriftherandomnumberislessthanorequalto 10 inthiscase, I'm goingtosettheheightoftheobstacletozero, whichmeanswewon't drawanobstacle.
Itallthiswillgiveanemptyspaceforthatsection.
Thisallowstheplayertohave a bitof a restrecuperateorgetoutof a particularlyawkwardtrajectory, althoughitiscompletelyrandom, whichmeansthere's noskeletaltothishorriblegame.
Once I'vedecidedwhatthenewsectionsobstacleshouldbeaddedtothebackoffthelistafterwe'veclearedthescreenistimetodrawthesections.
I alwaysuse a littleforLuketodothis, becauseall I needtodoisautomaticallyiteratedthroughthelist.
Sothecollisiondetectionisgoingtobe a oneline, but I'vebrokenituphere, soit's easiertoseewhat's goingup.
Wehave a bullionvalue, behascollidedandthey'regoingtobefourcasesinwhichthisisgoingtobetrue.
Eitherthebirdhasgoneoffthetopofthescreenalmostbecause I don't wanttocrashintothisscore, whichwilldrawupthere.
Ortheboardhasfallenoffthebottomofthescreen.
Orthecornerofthebirdhasintersectedwithsomethingotherthan a blankspaceinthecharacterray, representingtheoutputdisplaytotheconsulondhe.
Unusuallyforoneofmyvideoswereaccessingthescreen, butfordirectlyinthiscasewhere I'm takingthe Y, coordinateoffthebirdtimesthievesscreenwithPlus X, somethingwe'vedoneinprettymucheveryonelonecode a videoeveron.
I'm justcheckingWhatisthecharacteratthatlocationonthenexttohave a plussixonthembecausetheseareforthefrontofthebird.
Whenthereis a Clinton, I don't wantthegame's justimmediatelyrestartthelevel.
Theplayisgoingtoneedanopportunitytotake a break.
Everthinkaboutwhattheyjustdidwrong?
I havetothinkaboutwhythey'reevenplayingthisrubbishon, thenstartthegameattheirownpaceandsincethismeansthewholegamehastopause, I'llhavetoputthewholegamesofarintotheresultof a NIFconditionbasedonthehascollidedvariable.
Gameonthisresetgameflagwillbetestedoneachloop, andit'llbethefirstthingthisistestedanditspurposeisjusttoresetabsolutelyeverythingto a knownstartingpoint.
It's quitenicehavingthisresetflagherebecauseitmeans I canalsojustcallitinonusercreate, Aikensaid.
It's a trueand I knowthegamewillstartupintheknownstate.
Sincewe'rescoringthenumberofflapsbeingcounted, I'm goingtoaddtothewhentheuserpressesthespacekeyforflapping, I'm goingtoincreasetheendflatcountvariable, and I'm goingtocheckitagainst a maximumandifweregreaterthanthemaximumthanthemaximumbecomesthecurrentcount.