Scrolling Platformer Tutorial

Материал из Поле цифровой дидактики
Описание Руководство по скроллингу
Область знаний Математика
Область использования (ISTE) Computational Thinker
Возрастная категория 12


Поясняющее видео
Близкие рецепту понятия
Среды и средства для приготовления рецепта:

Coding the Player Sprite

To code the movement of the player, the following scripts need to be made. All scripts in this section are to go in the 'Player' sprite.


whenclickedbroadcastGreenFlagandwaitAwaytoonlyneed1flagblock.Flagblockscreatelag,sothefewerthebetter.{{cn}}broadcastPlayGameandwaitBroadcaststotheotherspritesthatthegamehasstarted.WhenIreceivePlaygameforeverbroadcastResetandwaitbroadcastSetUpandwaitGameOnSetsupthevariables.repeatuntilEXIT>Thismeanstheplayerhaseitherdiedorwon.broadcastTickendifEXIT=winthenWinelseDieIftheplayerdidnotwin,theywouldhavedied.enddefinePositiongotox:x-SCROLLXy:y-SCROLLYdefineGameOnpointindirection90setsizeto100%cleargraphiceffectssetinairto0setrotationstyleleft-rightsetsyto0setxto0setyto0setEXITtoThisshouldbeblank.setSCROLLLXto0setSCROLLLYto0showWhenIreceiveTickbroadcastReadyTickkeyRightarrowpressed?keyUparrowpressed?keyLeftarrowpressed?Checksifakeyhasbeenpressed.defineTickrightupleftifleftthenIftheleftkeywaspressed,itmovestheplayerleft.ChangeplayerXby-8endifrightthenIftherightkeywaspressed,itmovestheplayerright.ChangeplayerXby8endifupandinair<4thenIftheupkeywaspressed,itchecksiftheyarestillintheair.Iftheyarenot,itmakestheplayerjump.setsyto16endchangesyby-1Setsytoanynegativenumber.ChangeplayerYbysyActuallymovestheplayer'sYposition.TestdyingChecksiftheplayerhashitadangersprite.ifSCROLLX<0thensetSCROLLXto0endifSCROLLY<0thensetSCROLLYto0endchangeSCROLLYbyroundy-SCROLLY/10changeSCROLLXbyroundx-SCROLLX/5PositionPositionstheplayer'slocationcorrectly.ifyposition<-180thenChecksiftheplayerhasfallenintotheabyss.setEXITtodieenddefineChangeplayerXbysxMovestheplayer'sXaxis.ifsx<0thenIftheplayerismovingleft.pointindirection-90Theypointleft.elsepointindirection90Theypointright.endchangexbysxChangestheXvariablebytheamountofmovementneeded(sx).PositionPositionstheplayer.iftouchingplatforms?thenWillcheckiftheplayerhashitawalloraslope.repeat12Willcheckifthewall/slopeis12pixelssteepornot.changeyby1Movestheplayerup1pixel.PositionPositionstheplayer.ifnottouchingplatforms?thenThatmeansitwasaslope,sotheplayercanstoptrying.stopthisscriptendendchangeyby-12Correctsforthe12thatwasusedabove.repeatuntilnottouchingplatforms?Repeatedlychecksistheplayerhasranintoawall.ifsx>0thenToseeiftheplayerismovingleftofright.changexby-1Theplayeristryingtomoveright,soweneedtomovelefttonotgointothewall.elsechangexby1Theplayeristryingtomoveleft,soweneedtomoverighttostayoutofthewall.endPositionReposistionstheplayeronthescrollingplane.endenddefineChangeplayerYbysychangeybysyChangestheyvariableby(sy).changeinairby1Thisissothatweknowifweareintheairorontheground.Withoutthis,theplayerwouldbeabletoholdjumpandjustfly.PositionReposistionstheplayer.repeatuntilnottouchingplatforms?ifsy>0thenchangeyby-1elsechangeyby1setinairto0endsetsyto0PositionenddefineTestdyingiftouchingDanger?thensetEXITtodieenddefineDiesetEXITtoThisisempty.repeat4hidewait0.1secondsshowendwait0.5secondsdefineWinsetrotationstyledon'trotaterepeat50pointtowardsPortalturn65degreesmovedistancetoPortal/2stepschangesizeby-1changeghosteffectby2endhidechangeLEVELby1wait1secondscleargraphiceffectssetrotationstyleleft-right

Coding the Platforms Sprite

Once the player's code has been completed, the ground sprite (called 'platforms' here) needs to be added in order to create a scrolling effect. The following scripts go in the 'platforms' sprite.

whenIreceiveTickPositionx-SCROLLXy-SCROLLYwhenIreceiveGreenflagshowdefinePositionxygotox:xy:yifx=xpositionandy=ypositionthenshowelsehideendWhenIreceiveSetupsetxto0setyto0ifLEVEL=1thenswitchcostumetoLevel11Clone4500Clone4500Clone4500else...Ifthereisanotherscene,itshouldbeclonedhereusinganotherifelseblock.enddefineClonexycreatecloneofmyselfchangexbyxchangeybyynextcostume

Coding the Danger Sprite

To code the danger sprite, simply duplicate the "platforms" sprite and replace all the levels only with the dangerous elements that would hurt the player. Шаблон:Note

whenIreceiveTickpositionx-SCROLLXy-SCROLLYwhenIreceiveGreenflagshowwhenIreceiveResetdeletethisclonedefinePositionxygotox:xy:yifx=xpositionandy=ypositionthenshowelsehideendwhenIreceiveSetupsetxto0setyto0ifLEVEL=1thenswitchcostumetoLevel11Clone4500Thisclonesanewlevel.Dothisasmanytimesasthenumberoflevels,inthiscase3isusedasanexample.Clone4500Clone4500else...Ifthereisanotherscene,thenitshouldbeclonedhereusinganotherifelseblock.enddefineClonexycreatecloneofmyselfchangexbyxchangeybyynextcostume

Coding the Portal

Now that everything is made, a way to go to the next level needs to be made.

WhenIreceiveTickPositionx-SCROLLXy-SCROLLY+sinoftimer*100*20ifcostumenumber=2andtouchingplayer?thensetEXITtowinendWhenIreceiveSetupsetxto0setyto0ifLEVEL=1thenswitchcostumetoLevel11PositionPortalat450180else...Ifthereisanotherscene,thentheportalneedstobepositionedhereusinganotherifelseblock.enddefinePositionPortalatxysetxtoxsetytoydefinePositionxygotox:xy:yifx=xpositionandy=ypositionthenshowelsehideend

Adding Collectibles

Collectibles can be added to the game to make it more interesting. They are objects that need to be collected in order to achieve a certain goal. The code to make them is shown below:

WhenIreceiveTickPositionx-SCROLLXy-SCROLLY+sinoftimer*300*5iftouchingplayer?thenchangeCOLLECTEDby1ifCOLLECTED=COLLECTEDMAXthenbroadcastOpenportalstartsoundAllcoinscollectedelsestartsoundCollectenddeletethiscloneendWhenIreceiveGreenflagshowwhenmkeypressedsetMOUSEtojoinmousex+SCROLLXjoin,mousey+SCROLLYWhenIreceiveResetdeletethisclonedefinePositionxygotox:xy:yifx=xpositionandy=ypositionthenshowelsehideendWhenIreceiveSetupsetxto0setyto0ifLEVEL=1thenClone450180else...Ifthereisanotherscenethancoinsneedtobeclonedhereusinganotherifelseblock.enddefineClonexysetxtoxsetytoycreatecloneofmyselfchangeCOLLECTEDMAXby1

Transforming a Basic Platformer

Often, Scratchers make basic platformers before they make a scrolling platformer, and they may want to use their old script to make a scrolling platformer. To do this, the "ground" sprite would move with the player. Making a script to do this is simple:

whenclickedThisgoesintheplayersprite.foreverScroll0-xposition*0.8-40-yposition*0.80and-40arethedesiredxandypositionsoftheplayer....Thebasicplatformerscriptgoeshere.enddefineScrollxyThisgoesintheplayersprite.changelevelxbyroundxRoundthevariablessothelevelmovescorrectly.changelevelybyroundychangexbyroundxWhenthelevelmoves,theplayermustmovewithit.changeybyroundywhenclickedThisgoesinthegroundsprite.forevergotox:levelxy:levelyend

See Also