![]() |
It has been suggested that this page's contents be merged with the page Scrolling (sprites). You can discuss this on the page's talk page. (February 2022) |
A "scrolling game" is a game that, rather than the main sprite, which would move in a normal movement game, the map itself moves. So far, the code required works with every version of Scratch. In the example thumbnail, there are enemies following the main sprite.
Map Movement
In order to create a scrolling game, you need an original player and a map. Create two new sprites, one labeled "player" and one labeled "map" In the map sprite, create two costumes, a blank costume, for sizing, and a costume that will be the main map, it should be the same size as the costume workspace, and should be centered. In the map code space, create four variables.
set [X v] to (0) set [Y v] to (0) set [SCROLL X v] to (0) set [SCROLL Y v] to (0)
Next, create a string for the map
when green flag clicked switch costume to (blank v) //Switching the costume to blank, allows the size to be changed to bigger sizes than if you were to just have a big costume, because then it wouldn't change the size, or it would set the size even smaller. set size to (500) % switch costume to (map v) //Setting it back to the big costume will make it even bigger, and wont change the size. set [X v] to (0) set [Y v] to (0) set [SCROLL X v] to (0) set [SCROLL Y v] to (0) forever go to [back v] layer if <key (w v) pressed?> then change [SCROLL Y v] by (5) end if <key (s v) pressed?> then change [SCROLL Y v] by (-5) end if <key (a v) pressed?> then change [SCROLL X v] by (-5) end if <key (d v) pressed?> then change [SCROLL X v] by (5) end change [X v] by ((SCROLL X)*(-1)) change [Y v] by ((SCROLL Y)*(-1)) go to x: (X) y: (Y) if <not <<key (w v) pressed?> or <key (s v) pressed?>>> then set [SCROLL Y v] to [0] end if <not <<key (a v) pressed?> or <key (d v) pressed?>>> then set [SCROLL X v] to [0] end
In your player sprite, create a costume that will be used to resemble the player. Next, add a very simple script in the player sprite.
when green flag clicked forever go to [front v] layer ::looks point towards (mouse-pointer v) ::motion end
Now you have the map/player movement.
Enemies
Create a new sprite, named enemies. Inside the sprite, create a costume that you want your enemies to have.
Create a new variable for this sprite only.
set [clone id v] to [0]
Put a script to hide the variables (optional).
when green flag clicked hide variable [SCROLL X v] hide variable [SCROLL Y v] hide variable [X v] hide variable [Y v] hide variable [clone id v]
Create another script, and create a custom block. Run this custom block without screen refresh. Also create two lists.
delete all of [Clone X v] delete all of [Clone Y v]
when green flag clicked Generate Clones :: custom
define Generate Clones delete all of [Clone X v] delete all of [Clone Y v] show set [clone id v] to [0] repeat [5] add [] to [Clone X v] add [] to [Clone Y v] go to x: (pick random (-350) to (350)) y: (pick random (-350) to (350)) create clone of (myself v) change [clone id v] by (1) end hide
Create one more script.
when I start as a clone forever change x by ((SCROLL X) * (-1)) change y by ((SCROLL Y) * (-1)) if <not <touching (Player v) ?>> then change x by ((2.5) * ([sin v] of (direction))) change y by ((2.5) * ([cos v] of (direction))) end replace item (clone id) of [Clone X v] with (x position) replace item (clone id) of [Clone Y v] with (y position) go to [front v] layer point towards (Player v) end