Описание
Руководство по скроллингу
Область знаний
Математика
Область использования (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.
when clicked broadcast Green Flag and wait broadcast Play Game and wait When I receive Play game forever broadcast Reset and wait broadcast Set Up and wait Game On repeat until EXIT > broadcast Tick end if EXIT = win then Win else Die end define Position go to x: x - SCROLL X y: y - SCROLL Y define Game On point in direction 90 set size to 100 % clear graphic effects set in air to 0 set rotation style left-right set sy to 0 set x to 0 set y to 0 set EXIT to set SCROLLL X to 0 set SCROLLL Y to 0 show When I receive Tick broadcast Ready Tick key Right arrow pressed? key Up arrow pressed? key Left arrow pressed? define Tick right up left if left then Change player X by -8 end if right then Change player X by 8 end if up and in air < 4 then set sy to 16 end change sy by -1 Change player Y by sy Test dying if SCROLL X < 0 then set SCROLL X to 0 end if SCROLL Y < 0 then set SCROLL Y to 0 end change SCROLL Y by round y - SCROLL Y / 10 change SCROLL X by round x - SCROLL X / 5 Position if y position < -180 then set EXIT to die end define Change player X by sx if sx < 0 then point in direction -90 else point in direction 90 end change x by sx Position if touching platforms ? then repeat 12 change y by 1 Position if not touching platforms ? then stop this script end end change y by -12 repeat until not touching platforms ? if sx > 0 then change x by -1 else change x by 1 end Position end end define Change player Y by sy change y by sy change in air by 1 Position repeat until not touching platforms ? if sy > 0 then change y by -1 else change y by 1 set in air to 0 end set sy to 0 Position end define Test dying if touching Danger ? then set EXIT to die end define Die set EXIT to repeat 4 hide wait 0.1 seconds show end wait 0.5 seconds define Win set rotation style don't rotate repeat 50 point towards Portal turn 65 degrees move distance to Portal / 2 steps change size by -1 change ghost effect by 2 end hide change LEVEL by 1 wait 1 seconds clear graphic effects set rotation style left-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.
when I receive Tick Position x - SCROLL X y - SCROLL Y when I receive Green flag show define Position x y go to x: x y: y if x = x position and y = y position then show else hide end When I receive Setup set x to 0 set y to 0 if LEVEL = 1 then switch costume to Level 1 1 Clone 450 0 Clone 450 0 Clone 450 0 else ... end define Clone x y create clone of myself change x by x change y by y next costume
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
when I receive Tick position x - SCROLL X y - SCROLL Y when I receive Green flag show when I receive Reset delete this clone define Position x y go to x: x y: y if x = x position and y = y position then show else hide end when I receive Setup set x to 0 set y to 0 if LEVEL = 1 then switch costume to Level 1 1 Clone 450 0 Clone 450 0 Clone 450 0 else ... end define Clone x y create clone of myself change x by x change y by y next costume
Coding the Portal
Now that everything is made, a way to go to the next level needs to be made.
When I receive Tick Position x - SCROLL X y - SCROLL Y + sin of timer * 100 * 20 if costume number = 2 and touching player ? then set EXIT to win end When I receive Setup set x to 0 set y to 0 if LEVEL = 1 then switch costume to Level 1 1 Position Portal at 450 180 else ... end define Position Portal at x y set x to x set y to y define Position x y go to x: x y: y if x = x position and y = y position then show else hide end
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:
When I receive Tick Position x - SCROLL X y - SCROLL Y + sin of timer * 300 * 5 if touching player ? then change COLLECTED by 1 if COLLECTED = COLLECTED MAX then broadcast Open portal start sound All coins collected else start sound Collect end delete this clone end When I receive Green flag show when m key pressed set MOUSE to join mouse x + SCROLL X join , mouse y + SCROLL Y When I receive Reset delete this clone define Position x y go to x: x y: y if x = x position and y = y position then show else hide end When I receive Setup set x to 0 set y to 0 if LEVEL = 1 then Clone 450 180 else ... end define Clone x y set x to x set y to y create clone of myself change COLLECTED MAX by 1
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:
when clicked forever Scroll 0 - x position * 0.8 -40 - y position * 0.8 ... end define Scroll x y change level x by round x change level y by round y change x by round x change y by round y when clicked forever go to x: level x y: level y end
See Also