Eng:Creating a Multiplayer Game

Multiplayer games are games in which more than one person can play at once at the same time, often from different computers. Multiplayer games can be made and played on the same computer (A local multiplayer) or with different computers from different places through the use of cloud variables.

Local Multiplayer
Since local multiplayer games are played on the same computer, there is no need to create cloud variables of any sort. Local multiplayer games are based off the following scripts:

A script is made for the first player: when green flag clicked forever if  move(-10) steps end if  move (10) steps end

A script for the second player is made. Notice the different keys both players press to move up and down. when green flag clicked forever if  move(-10) steps end if  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 has one side of the keyboard, such as the num keys or arrow keys, and the other player has the other side of the keyboard, such as the A,S,D,F keys. This makes the game easier to play.

Online Multiplayer
Online multiplayer games can be made with cloud data. There are many ways to make an online multiplayer game. In all of these examples, however, you must attain full scratcher status. New Scratchers can't do this because of the restrictions placed upon Cloud Variables.

Realistic online multiplayer games or MMO (such as Cloud Platformer Multiplayer Fun by griffpatch) are scarce due to cloud data limits and non-existence of cloud lists. However this tutorial https://scratch.mit.edu/projects/77775784/, and the one below simplify the basics of a multiplayer.

Variables
First off, one needs to create some cloud variables for movement and detecting if a player is still active.

These variables should be created:

(☁ cloud check)//checks to see if the two players are connected. set to 1. (☁ Player1 check)//checks if player is still on (☁ Player2 check)//same with this one (☁Player1 coords)//sends coordinates (☁Player2 coords)//again here (local player1 check)//compare with the cloud variable (local player2 check)//ditto (Player ID)//just telling which sprite you control

Coding
Once those variables are made, we need to see if anyone else is on the project and connect the player to the cloud with a slot.

Backdrop when green flag clicked broadcast [start v]//just have it so that there is only 1 green flag clicked wait (2) secs//make the project is the connected to the cloud if<(☁ cloud check)=[1]>//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//waiting because if a player is active, the player1 and 2 cloud variables will change every second so we just make sure by waiting 5 if <(local player1 check)=(☁player1 check)>//if the cloud check has not changed, we know nobody is playing as player 1, we set the player as player1 set [player ID v] to [1]//not cloud because we will be updating the cloud variable for player1 so that other computers know that there is a player1 set [☁player1 check v] to [0]//just so we do not do not get that variable up to a million eventually set [☁player1 coords v] to[500500]//the coordinate system we use adds 500 to each coordinate because then every number is a positive 3 digit number and we do not need an encoder or decoder, basically, that is 0,0 broadcast [joined v]//tell the other sprites to do their stuff else if<(local player2 check)=(☁player2 check)>//doing the same thing as above but if there already is a player1, we check for a player2 set [player ID v] to [1] set [☁player1 check v] to [0] set [☁player1 coords v] to[500500] broadcast [joined v] else broadcast [full v]//if both slots are taken we tell a sprite to tell the player the game is full end end else broadcast [not connected v]//if the cloud test we ran at the beginning fails, we tell them they are not connected to the cloud or are a new scratcher end

We add this script for Player1's sprite. when I receive [joined v] wait (1) secs//just so that the sprite that tells you stuff has time if <(playerID)=[1]> forever//just the movement script if //if you do not know how this works, you shouldn't be making this change x by (3) end if  change x by (-3) end if  change y by (-3) end if  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 before 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 we subtract because these are the ones we receive 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 <(playerID)=[1] forever change [☁player1 check v] by (1)//if you remember way back to the beginning when we first run the program we wait 5 seconds, for this variable to change, because then we know there is a player1 wait (1) secs end This script is for Player2's sprite. We change all instances of player1 variables to player2. when I receive [joined v] wait (1) secs if <(playerID)=[1]> forever if  change x by (3) end if  change x by (-3) end if  change y by (-3) end if  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 <(playerID)=[1] forever change [☁player2 check v] by (1) wait (1) secs end

This script is for telling players of errors (such as an error connecting). when gf clicked go to x:(0) y:(0)//make it center and so we can see it go to front show switch costume to [connecting v]//just a costume that says "connecting" when I receive [full v] switch costume to [full v]//says something like "sorry, there are no open slots, please try again later when I receive [not connected v] switch costume to [not connected v]//sorry you are either a new scratcher or are not connected to the cloud when I receive [joined v] if<<<(playerID)=[1]>and<(☁player2 check)=(local player2 check)>>or<<(playerID)=[2]>and<(☁player1 check)=(local player1 check)>>>//if the other player wasn't active switch costume to [no one else on v]//says "slot found and connected but no one else is online else switch costume to [someone else found v]//says slot found and connected some one else is online end wait (1) secs//let them have time to read it hide//Let the gameplay commence!