Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (June 2018)
Reason: This article contains inconcise language.
(☁ var) This article or section uses Cloud Data. Users who are New Scratchers or are using the Offline Editor cannot make projects using Cloud Data or use it in other users' projects. To use Cloud Data, the Scratcher status is needed and the Online Editor has to be used.

Multiplayer games are games that are designed to be played by multiple people at the same time. They can be made and played on the same computer (local multiplayer) or with different computers from different places through the use of cloud variables.

Note Note: Please remember the 256-character limit that was imposed on Cloud Variables with the release of Scratch 3.0.

Local Multiplayer

Since local multiplayer games are played on the same computer for several players, they don't use cloud variables. Local multiplayer games are based off the following scripts:

A script is made for the first player:

when green flag clicked
forever
if <key (left arrow v) pressed?> then
move(-10) steps
end
if <key (right arrow v) pressed?> then
move (10) steps
end

A script for the second player is made. Notice the different keys both players press to move left and right.

when green flag clicked
forever
if <key (a v) pressed?> then
move(-10) steps
end
if <key (d v) pressed?> then
move (10) steps
end

The scripts can be programmed differently depending on what is needed for the project. When creating a local multiplayer game, one player usually controls one side of the keyboard, such as the number keys or arrow keys, and the other player uses the other side of the keyboard, such as the W, A, S, D, and/or F keys. This makes the game easier to play. However, a downside is that on some laptop computers, a keyboard may only input a certain number of keys before reaching a maximum and not detecting any newer keys being pressed.

Online Multiplayer

Online multiplayer games use cloud data. There are many ways to make an online multiplayer game, however each way requires that you attain full scratcher status. Scratchers with the New Scratcher status cannot do this because of the restrictions placed upon cloud variables.

Realistic online multiplayer games or MMOGs are not very common due to cloud data limits and the non-existence of cloud lists. However the code below simplifies the basics of a multiplayer game.

Variables

Firstly, cloud variables need to be created to help with movement and detecting if a player is still active.

These variables have to be created:

Note Note: It is possible to use less variables by using a variable for 2 purposes, but that can be more complex.
(☁ cloud check)//This checks to see if the two players are connected. set to 1.
(☁ Player1 check)//This checks if player1 is still on
(☁ Player2 check)//This checks if player2 is on
(☁ Player1 coords)//This sends coordinates from player1
(☁ Player2 coords)//This sends coordinates from player2
(local player1 check)//This checks if player1 is still on (not cloud)
(local player2 check)//This checks if player2 is still on (not cloud)
(Player ID)//This says what sprite you are controlling

Coding

Once those variables are created, it is needed to check if anyone else is on the project and to connect the player to the cloud with a slot if needed.

Backdrop
Note Note: The connection test does not work if you are not signed in, disconnected from the Internet, or a New Scratcher. However, it can still see cloud data. You still have to make the variable be 0 in the editor, though.
when green flag clicked
broadcast (start v)//Have it so that there is only 1 green flag clicked
wait (2) secs//This makes sure the project is connected to the cloud
set [☁ cloud check v] to [0]//If the player is a new scratcher or offline this should not work
set [☁ cloud check v] to [1]//Then this won't work either
if<(☁ cloud check)=[1]> then//If it equals 1, they are connected, if not, they are not
set [local player1 check v] to (☁ Player1 check)
set [local player2 check v] to (☁ Player2 check)
wait (5) secs //It's waiting because if a player is active, the player 1 and 2 cloud variables will change every second so it is needed to make sure  by waiting 5
if <(local player1 check)=(☁ Player1 check)> then//If the cloud check has not changed it means that nobody is playing as player 1, therefore, the player has been set as player1
set [Player ID v] to [1]//Not cloud because the cloud variable for player1 will be updated so that other computers know that there is a player1
set [☁ Player1 check v] to [0]//So it does not get that variable up to a million eventually
set [☁ Player1 coords v] to [500500]//The coordinate system adds 500 to each coordinate because then every number is a positive 3 digit number and an encoder or decoder is not necessary, basically, that is 0,0
broadcast (joined v)//This sends a message to every sprite telling them to do their code
else
if<(local player2 check)=(☁ Player2 check)> then//This is doing the same thing as above but if there already is a player1, it is needed to check for a player2
set [Player ID v] to [2]
set [☁ Player2 check v] to [0]
set [☁ Player2 coords v] to[500500]
broadcast (joined v)
else
broadcast (full v)//If both slots are taken a sprite is needed to tell the player the game is full
end
end
else
broadcast (not connected v)//if the cloud test run at the beginning fails, it is needed to tell them they are not connected to the cloud or are a new scratcher
end
set [☁ cloud check v] to (0)

Add this script for Player1's sprite.

when I receive [joined v]
wait (1) secs//So that the sprite that tells you stuff has time
if <(Player ID)=[1]> then
forever//This is the movement script
if <key (right arrow v) pressed?> then//This code is for the movement of Player1.
change x by (3)
end
if <key (left arrow v) pressed?> then
change x by (-3)
end
if <key (down arrow v) pressed?> then
change y by (-3)
end
if <key (up arrow v) pressed?> then
change y by (3)
end
set [☁ Player1 coords v] to (join((x position)+(500))((y position)+(500)))//Telling all the other computers player1's position with the plus 500 method mentioned earlier
end
else
forever//If the player ID isn't one the sprite of player1 will go to the coordinates, again using the 500 method, but this time it is needed to subtract because these are the ones received from the other computer so they have already had 500 added
go to x:((join (letter (1) of (☁ Player1 coords))(join (letter (2) of (☁ Player1 coords))(letter (3) of (☁ Player 1 coords))))-[500])y:((join (letter (4) of (☁ Player1 coords))(join (letter (5) of (☁ Player1 coords))(letter (6) of (☁ Player 1 coords))))-[500])
end
end
when I receive [joined v]
if <(Player ID)=[1]> then
forever
change [☁ Player1 check v] by (1)//At the beginning, when the project is first run there is a five second wait for this variable to change, because after that whether there is a player1 or not is recorded
wait (1) secs
end

This script is for Player2's sprite. All of player1's variables are transferred to player2's variables.

when I receive [joined v]
wait (1) secs
if <(Player ID)=[2]> then
forever
if <key (right arrow v) pressed?> then
change x by (3)
end
if <key (left arrow v) pressed?> then
change x by (-3)
end
if <key (down arrow v) pressed?> then
change y by (-3)
end
if <key (up arrow v) pressed?> then
change y by (3)
end
set [☁ Player2 coords v] to (join((x position)+(500))((y position)+(500)))
end
else
forever
go to x:((join (letter (1) of (☁ Player2 coords))(join (letter (2) of (☁ Player2 coords))(letter (3) of (☁ Player 2 coords))))-[500])y:((join (letter (4) of (☁ Player2 coords))(join (letter (5) of (☁ Player2 coords))(letter (6) of (☁ Player 2 coords))))-[500])
end
end
when I receive [joined v]
if <(Player ID)=[2]> then
forever
change [☁ Player2 check v] by (1)
wait (1) secs
end

This script is for telling players of errors (such as an error connecting to the game).

when gf clicked
go to x:(0) y:(0)//Makes the sprite go to the center so it's easier to see
go to [front v] layer
show
switch costume to (connecting v)//Costume that tells the player "connecting"
when I receive [full v]
switch costume to (full v)//costume that tells the player "Sorry, there are no open slots, please try again later"
when I receive [not connected v]
switch costume to (not connected v)//Costume that tells the player "sorry you are either a new scratcher or are not connected to the cloud"
when I receive [joined v]
if<<<(Player ID)=[1]>and<(☁ Player2 check)=(local player2 check)>>or<<(Player ID)=[2]>and<(☁ Player1 check)=(local player1 check)>>> then//If the other player wasn't active
switch costume to (no one else on v)//Costume that tells the player "slot found and connected but no one else is online"
else
switch costume to (someone else found v)//Tells the player "slot found and connected someone else is online"
end
wait (1) secs//The player(s) will get a second to read this message
hide//Gameplay begins

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.