How To Create A Scrolling Background In Scratch
This article covers a step-by-step process nigh how to make a Scrolling Platformer. A scrolling platformer is a type of platformer where the camera follows the player as they move through the level(s).
Coding the Histrion
To code the movement of the thespian, the following scripts need to be made. All scripts in this section are to go in the 'Role player' sprite.
Note: | Variables in caps (eastward.g. "SCROLL X" and "SCROLL Y") are global variables, and those lowercase (e.g. "ten" and "y") are made with the "For this sprite only" choice selected. |
When green flag clicked broadcast (Green Flag v) and expect//A way to simply need 1 flag block. Flag blocks create lag, then the fewer the better. broadcast (Play Game v) and wait//Broadcasts to the other sprites that the game has started When I receive [Play game 5] forever broadcast (Reset 5) and wait broadcast (Ready Upward five) and wait Game On::custom//sets up the variables repeat until <(EXIT)>()>//means the actor has either died or won broadcast (Tick v) stop if <(Go out)=(win)> and so Win::custom else Die::custom//if the role player had not won, they would've had to have died finish define Position get to x:((x)-(SCROLL X)) y: ((y) - (Curlicue Y) define Game On//Run without screen refresh bespeak in management (90) set size to (100)% clear graphic effects set [in air five] to (0) set rotation fashion [left-right v] gear up [sy 5] to (0) set [x v] to (0) set [y 5] to (0) set [EXIT v] to () // Nothing set [SCROLLL X 5] to (0) set [SCROLLL Y v] to (0) show When I receive [Tick v] broadcast (Ready five) Tick <fundamental (Right arrow five) pressed?> <key (Up arrow five) pressed?> <central (Left arrow v) pressed?>::custom//checks if a key has been pressed ascertain Tick <right> <up> <left> if <left> and so//If the left fundamental was pressed, it moves the player left Change player X by [-viii]::custom end if <correct> and so//If the correct key was pressed, it moves the player right Change histrion X by [eight]::custom end if <<upward> and <(in air)< (4)>> then//If the up primal was pressed, it checks if they are in the air still, if they are not, information technology makes the player leap set [sy v] to (16) end change [sy v] by (-1)//Gravity,prepare it to any negative number Change player Y by (sy)::custom//Really moves the role player's Y position Exam dying::custom//Checks if the player has hit a danger sprite if <(Scroll X)<(0)> then prepare [Curl X v] to (0) end if <(SCROLL Y)<(0)> then set [Roll Y v] to (0) end modify [SCROLL Y 5] by (round(((y)-(SCROLL Y))/(10))) modify [SCROLL Ten v] by (round(((x)-(SCROLL X))/(five))) Position::custom//Positions the player'south location correctly if <(y position) < (-180)> then//If the player has fallen into the abyss gear up [EXIT 5] to [die] end ascertain Change player Ten by (sx)//Moves the player's X axis if <(sx::custom)<(0)> then//If the thespian is moving left indicate in management (-ninety)//They indicate left else betoken in direction (90)//They point right end modify [ten 5] by (sx::csutom)//Changes the X variable by the amount of movement needed (sx) Position::custom//Positions the player if <touching (platforms v) ?> then//Will check if the player has hit a wall, or a slope echo (12)//Will check if the wall/slope in question is 12 pixels steep or not change [y v] by (1)//Moves the role player up 1 pixel Position::custom//Positions the histrion if <not<touching (platforms 5) ?>> then//Then, that ways it was a gradient, and then we tin can finish trying stop [this script v] end end modify [y v] by (-12)//Corrects for the 12 that was used above echo until <not<touching (platforms 5)?>> //Repeatedly checks is the role player has ran into a wall if <(sx)>(0)> and so //To come across if the histrion is moving left of right alter [x v] by (-i) //The player is trying to move right, and then we need to move left to non go into the wall else alter [x v] past (one) //The actor is trying to move left, so we need to motion right to stay out of the wall cease Position::custom //Reposistions the role player on the scrolling airplane cease cease define Change player Y by (sy) change [y v] past (sy::custom) //Changes the y variable by an corporeality (sy) change [in air v] by (ane) //This is and so that we know if we are in the air, or on the ground. Without this, you would be able to hold leap and just fly. Position::custom //Reposistions the player repeat until <not<touching (platforms v)?>> if <(sy::custom)>(0)> and then change [y 5] by (-1) else change [y v] by (one) ready [in air v] to (0) stop set [sy five] to (0) Position::custom end define Test dying if <touching (Danger v)?> so ready [EXIT v] to [die] finish define Die prepare [Leave v] to ()//nothing echo (iv) hibernate wait (0.one) seconds show stop wait (0.5) seconds define Win set rotation style [don't rotate v] repeat (50) indicate towards (Portal v) turn cw (65) degrees move ((distance to (Portal v))/(2)) steps change size by (-1) modify [ghost v] result by (2) terminate hide change [LEVEL v] by (i) wait (1) seconds clear graphic furnishings fix rotation way [left-correct v]
Coding the Platforms Sprite
One time the thespian'south lawmaking has been completed, lawmaking for the ground sprite (called 'platforms' here) needs to be added in order to create a scrolling outcome. The following scripts become in the 'platforms' sprite.
when I receive [Tick 5] Position ((x)-(Curlicue X)) ((y) - (SCROLL Y) when I receive [Dark-green flag v] testify define Position (10) (y) go to x: (x::custom) y: (y::custom) if <<(x::custom)= (x position)> and <(y::custom) = (y position)>> then show else hide stop When I receive [Setup v] ready [x v] to (0) set up [y v] to (0) if <(LEVEL)=(ane)> then switch costume to (Level 1 1) Clone (450) (0) // This clones a new level. This needs to be done every bit many times as the number of levels. In this case, 3 is used as an example. Clone (450) (0) Clone (450) (0) else ...::grey // If there is another scene then information technology should be cloned here using another if else block. terminate ascertain Clone (ten) (y) create clone of (myself 5) change [x v] past (x::custom) change [y v] by (y::custom) adjacent costume
Coding the Danger Sprite
To code the danger sprite only duplicate the "platforms" sprite and supervene upon all the levels with only the dangerous elements that would hurt the player.
Annotation: | Make sure to call the danger sprite "danger" because the script higher up already makes the actor sense for a sprite of that proper name. |
when I receive [Tick v] position ((x)-(Coil 10))((y)-(Roll Y))::custom when I receive [Dark-green flag 5] show when I receive [Reset five] delete this clone define Position (x) (y) go to x: (ten::custom) y: (y::custom) if <<(x::custom)= (x position)> and <(y::custom) = (y position)>> then bear witness else hibernate terminate when I receive [Setup five] set [ten v] to (0) set [y v] to (0) if <(LEVEL)=(i)> then switch costume to (Level i ane) Clone (450) (0) // This clones a new level, do this as many times as the number of levels, in this case three is used as an example. Clone (450) (0) Clone (450) (0) else ...::grey // If there is another scene then it should exist cloned hither using another if else block. end define Clone (x) (y) create clone of (myself v) alter [ten five] by (ten::custom) modify [y v] by (y::custom) 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 v] Position ((x)-(Roll Ten)) (((y) - (Coil Y)) + ((([sin v] of (timer)) *(100))*(20)))::custom if <<(costume [number v]) = (two)> and <touching (player v)?>> then ready [EXIT 5] to [win] end When I receive [Setup v] prepare [x v] to (0) fix [y v] to (0) if <(LEVEL)=(1)> then switch costume to (Level one 1) Position Portal at (450) (180)::custom else ...::grey // If there is some other scene than the portal needs to be positioned hither using some other if else cake. stop ascertain Position Portal at (x) (y) prepare [x 5] to (10::custom) set [y v] to (y::custom) ascertain Position (x) (y) go to ten: (x::custom) y: (y::custom) if <<(x::custom)= (ten position)> and <(y::custom) = (y position)>> then show else hide finish
Adding Collectibles
Collectibles can be added to the game to make information technology more interesting. They are objects that need to be collected in club to attain a sure goal. The code to make them is shown beneath:
When I receive [Tick v] Position ((10)-(SCROLL X)) (((y) - (SCROLL Y)) + ((([sin five] of (timer)) *(300))*(5)))::custom if <touching (player 5)?> and so alter [COLLECTED five] by (1) if <(COLLECTED)=(COLLECTED MAX)> then broadcast (Open up portal 5) start audio (All coins nerveless v) else start sound (Collect v) end delete this clone end When I receive [Green flag v] prove when [m 5] key pressed fix [MOUSE v] to (join ((mouse x) + (Scroll X))(join [,]((mouse y) + (SCROLL Y)))) When I receive [Reset v] delete this clone define Position (10) (y) go to 10: (ten::custom) y: (y::custom) if <<(10::custom)= (x position)> and <(y::custom) = (y position)>> and then prove else hide stop When I receive [Setup 5] set [x v] to (0) set up [y five] to (0) if <(LEVEL)=(1)> then Clone (450) (180)::custom else ...::grey // If there is another scene than coins need to exist cloned here using another if else cake. end define Clone (ten) (y) set [x five] to (x::custom) set [y 5] to (y::custom) create clone of (myself v) modify [COLLECTED MAX v] by (i)
Transforming a Basic Platformer
Frequently, Scratchers make bones platformers earlier they make a scrolling platformer, and they may want to use their onetime script to make a scrolling platformer. To do this, the "footing" sprite would move with the player. Making a script to do this is elementary:
when greenish flag clicked//This goes in the role player sprite. forever Scroll (((0) - (x position)) * (0.8)) (((-40) - (y position)) * (0.8))//0 and -forty are the desired x and y positions of the player. ...//The basic platformer script goes hither. stop define Coil (x) (y)//This goes in the histrion sprite. change [level x v] by (round (10))//Round the variables so the level moves correctly. alter [level y v] past (round (y)) change x by (round (x))//When the level moves, the actor must move with it. alter y by (round (y)) when light-green flag clicked//This goes in the footing sprite. forever get to x:(level x) y:(level y) end
Come across Too
- How to Make a Basic Platformer
- Advanced Platformer Physics
The Wiki is working on a nighttime theme, and you're seeing it! Got feedback? Post on the Wiki Forum Topic.
How To Create A Scrolling Background In Scratch,
Source: https://en.scratch-wiki.info/wiki/Scrolling_Platformer_Tutorial
Posted by: ochoascang1935.blogspot.com
0 Response to "How To Create A Scrolling Background In Scratch"
Post a Comment