<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Multi-player 2D game &lt;Candy Truck Battle&gt; - Difficulty 6]]></title><description><![CDATA[<p dir="auto"> </p>
<h2><a class="anchor-offset" name="introduction"></a>Introduction</h2>
<p dir="auto"> </p>
<p dir="auto">In this tutorial, you’ll build a 2-player game called <strong>Candy Truck Battle</strong>, where two candy trucks collect rewards and fire donuts at each other. Each player controls one truck on their own computer, but they’ll both see the full game scene on their screens.</p>
<p dir="auto">To connect the two players across computers, we’ll use the <strong>enhanced cloud variables</strong> provided by CreatiCode. Here’s a quick preview of the final result:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8594a3cd-60d8-45e8-b9d5-bb9f102568ce.gif" alt="finaldemo.gif" class=" img-responsive img-markdown" width="1110" height="411" /></p>
<p dir="auto"> <br />
 <br />
 <br />
 </p>
<h1><a class="anchor-offset" name="module-1-setting-up-the-game-session"></a>Module 1 - Setting Up the Game Session</h1>
<p dir="auto"> </p>
<p dir="auto">In this section, you’ll set up a game session that connects two players running the same project on different computers. We’ll use <strong>widgets</strong> for the user interface, and <strong>cloud variables</strong> for real-time communication between the two players.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-1-remix-the-starter-project"></a>Step 1 - Remix the Starter Project</h2>
<p dir="auto"> </p>
<p dir="auto">Start by remixing this project:</p>
<p dir="auto"><a href="http://play.creaticode.com/projects/68543e0c23ae1eb9ca386f59" target="_blank" rel="noopener noreferrer nofollow ugc">play.creaticode.com/projects/68543e0c23ae1eb9ca386f59</a></p>
<p dir="auto"> <br />
It contains everything you’ll need: trucks, trees, donuts, rewards, and other assets.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-2-display-the-game-name-input"></a>Step 2 - Display the Game Name Input</h2>
<p dir="auto"> </p>
<p dir="auto">Open the <strong>“Setup”</strong> sprite, which manages the connection between two players.</p>
<p dir="auto">Create a new custom block called <code>show setup screen</code> and run it when the green flag is clicked:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/749cbf91-9605-491e-a7c2-b676bbc12597.png" alt="c84d3e16-232a-4353-9eea-465b711736d7-image.png" class=" img-responsive img-markdown" width="172" height="133" /><br />
 </p>
<p dir="auto">Inside this block, start by displaying a <strong>label</strong> and an <strong>input box</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6d8d9da9-0109-43a2-a986-0e08173ce40c.png" alt="6bf5c536-45f9-4c6e-9ee4-0f02c503228c-image.png" class=" img-responsive img-markdown" width="1147" height="392" /></p>
<p dir="auto"> <br />
The label has a <strong>fully transparent background and 0 border</strong>, so we directly see the white text over the green stage. The input box has a default value of <strong>1234</strong> as an example.</p>
<p dir="auto">This game name will be used later as a “session ID” to create a new game session. Each pair of players will share a common session ID, so that the cloud variables will only be kept in sync between them. This way, players in different sessions won’t interfere with each other.<br />
 <br />
Here’s what it looks like:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/830b0fed-8a29-4902-b1ae-36ef751f4372.png" alt="d06b0b8a-d4ec-406a-98f8-e524d520a37e-image.png" class=" img-responsive img-markdown" width="487" height="362" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-3-show-2-action-buttons"></a>Step 3 - Show 2 Action Buttons</h2>
<p dir="auto"> </p>
<p dir="auto">To connect just these two players using cloud variables, one player needs to <strong>create</strong> the session and the other must <strong>join</strong> it using the same session ID.</p>
<p dir="auto">Under the input box, add a label and two buttons:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/baf71f7d-9da6-4506-b5ad-3a2784e66aa4.png" alt="97f1210a-f1c5-4be8-a781-9f2f5da27a2a-image.png" class=" img-responsive img-markdown" width="478" height="358" /></p>
<p dir="auto"> <br />
Here are the blocks you’ll add inside the <code>show setup screen</code> block:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d09bda9e-f2fb-4c1e-b581-c26cda629c51.png" alt="8a3775a1-d738-4602-b67d-fd4bbc4a3891-image.png" class=" img-responsive img-markdown" width="977" height="444" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-4-handle-the-create-session-button"></a>Step 4 - Handle the “Create Session” Button</h2>
<p dir="auto"> </p>
<p dir="auto">When the “create session” button (“button1”) is clicked, we will first set a few key variables:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2e8b2ec6-23ac-4809-a7e7-549af4b5ad39.png" alt="1762b11d-8f2d-4604-a3da-0542f6c24eaf-image.png" class=" img-responsive img-markdown" width="512" height="227" /></p>
<p dir="auto"> </p>
<ul>
<li>Player 1 sets <code>my team = 1</code> and <code>opponent team = 2</code>.</li>
<li>Store the session ID in a variable like <code>session input</code>.</li>
</ul>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-5-create-the-session-and-handle-errors"></a>Step 5 - Create the Session and Handle Errors</h2>
<p dir="auto"> </p>
<p dir="auto">Use the <code>create cloud session</code> block. If it fails (usually because that session ID already exists), show an error and let the player try a different ID.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8fd7c9b5-4f15-4f57-8bfe-10995f6a04b2.png" alt="5f9a234b-ed1e-42ce-8cbe-01ea925cc7b6-image.png" class=" img-responsive img-markdown" width="1061" height="521" /><br />
 <br />
Here’s the error message shown to the user:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/748c6786-797d-486a-84b1-424641e0dd4c.png" alt="bb2e38bc-3cf4-490f-8fb8-32e73c4104b1-image.png" class=" img-responsive img-markdown" width="488" height="367" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-6-show-a-waiting-message"></a>Step 6 - Show a waiting message</h2>
<p dir="auto"> </p>
<p dir="auto">If player 1 successfully creates the session, they’ll wait for the second player to join.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/28272f1b-7fc0-4dd5-8907-b9fb5c1d0d2b.png" alt="975c5066-aa8b-4d9e-82f4-38e540b43d3a-image.png" class=" img-responsive img-markdown" width="1053" height="459" /></p>
<p dir="auto"> <br />
The message looks like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/997f3353-8991-4db0-8388-6aad18160cb8.png" alt="62027797-0808-4fa8-8b64-cab54fe2af30-image.png" class=" img-responsive img-markdown" width="485" height="365" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-7-wait-for-code-ready2-code-from-player-2"></a>Step 7 - Wait for <code>☁ ready2</code> from Player 2</h2>
<p dir="auto"> </p>
<p dir="auto">We’ll use the cloud variable <code>☁ ready2</code> to let player 1 know when player 2 has joined. Initially, set it to 0.</p>
<p dir="auto">Once player 2 joins, they’ll set <code>☁ ready2 = 1</code>, and player 1 will detect this change.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4cacc2a4-1636-4882-936e-75a8ea06af94.png" alt="2e26f356-7a3c-410b-a0c8-3c6c5b1ac7b5-image.png" class=" img-responsive img-markdown" width="287" height="192" /></p>
<p dir="auto"> <br />
<img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f04be772-3104-4c0b-b361-c983e7c51448.png" alt="2681b78e-f496-4af3-842b-4aabf26d32f8-image.png" class=" img-responsive img-markdown" width="554" height="725" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-8-handle-player-2-joining-the-session"></a>Step 8 - Handle Player 2 Joining the Session</h2>
<p dir="auto"> </p>
<p dir="auto">When the second player clicks <strong>Button 2</strong> (“Join Session”):</p>
<ul>
<li>Set <code>my team = 2</code> and <code>opponent team = 1</code></li>
<li>Save the session ID</li>
<li>Try to join the existing cloud session</li>
</ul>
<p dir="auto">If successful:</p>
<ul>
<li>Show a “Waiting for player 1…” message</li>
<li>Set <code>☁ ready2 = 1</code> (this triggers player 1’s next step)</li>
</ul>
<p dir="auto">If it fails, player 2 probably used the wrong ID or the session isn’t created yet.</p>
<p dir="auto"> <br />
<img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4b7a12ee-e8ce-49bd-81a5-621834617109.png" alt="ecea004a-9699-443b-901c-7f95988cbcaf-image.png" class=" img-responsive img-markdown" width="1050" height="713" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-9-show-start-game-button-for-player-1"></a>Step 9 - Show “Start Game” Button for Player 1</h2>
<p dir="auto"> </p>
<p dir="auto">Once <code>☁ ready2 = 1</code> is detected on player 1’s side, we hide the setup screen and show a new <strong>“Start Game”</strong> button.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/99475457-279f-4b00-9016-3f7c0066b7f9.png" alt="bfb57002-a68e-44f9-b2e9-653b5d9b71d3-image.png" class=" img-responsive img-markdown" width="1031" height="328" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-10-start-the-game"></a>Step 10 - Start the Game</h2>
<p dir="auto"> </p>
<p dir="auto">When player 1 clicks “Start Game,” set the cloud variable <code>☁ game state = 1</code> to let both players know the game has started.</p>
<p dir="auto">Start with <code>☁ game state = 0</code> when the project loads:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e7791c53-d1ec-4fe7-914a-781ccf299f6f.png" alt="1a5818e5-b4c1-4571-801d-45a88bc618cd-image.png" class=" img-responsive img-markdown" width="304" height="218" /></p>
<p dir="auto"> <br />
Then update it when the button is clicked:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/85c4748a-394b-41b9-a60d-6fc65542fdb5.png" alt="3d591b58-9c4f-4cc4-97c9-8da9517c5bd0-image.png" class=" img-responsive img-markdown" width="315" height="133" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-11-wait-for-code-game-state-code-1"></a>Step 11 - Wait for <code>☁ game state</code> = 1</h2>
<p dir="auto"> </p>
<p dir="auto">Once <code>☁ game state = 1</code>, both players proceed.</p>
<p dir="auto">For player 1:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4b38856b-32be-40b7-bd5c-bce11a5801ab.png" alt="1c28f136-528f-4164-b8d1-c37791f5e95e-image.png" class=" img-responsive img-markdown" width="525" height="558" /></p>
<p dir="auto"> <br />
And for player 2:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b5edcab7-9439-4e1f-bbda-2b842143f9fe.png" alt="d2286f7d-bd46-4591-b6d8-f2a02a23ee94-image.png" class=" img-responsive img-markdown" width="438" height="597" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-12-show-life-count"></a>Step 12 - Show Life Count</h2>
<p dir="auto"> </p>
<p dir="auto">After the game begins, show each player’s <strong>life count</strong>, starting at 3. Create a block called <code>show life count</code>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6a4e9904-1ab4-4969-ac2b-a0c78512af36.png" alt="9a99bf8d-4348-422d-90b0-ecfea1ca4fad-image.png" class=" img-responsive img-markdown" width="972" height="530" /></p>
<p dir="auto"> </p>
<ul>
<li>Use <code>lives_1</code> and <code>lives_2</code> as label names for updates.</li>
</ul>
<p dir="auto">Call this block for both players:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/99ee5b21-f258-4b2e-9974-abcc92e05125.png" alt="2f432994-d3f6-4135-b6a6-14b10d54c012-image.png" class=" img-responsive img-markdown" width="349" height="173" /></p>
<p dir="auto"> <br />
<img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/67878007-bf18-4bdb-9385-c27753450b21.png" alt="eb9d5444-49c0-4b2e-a68c-a940563e5520-image.png" class=" img-responsive img-markdown" width="356" height="212" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-13-recap"></a>Step 13 - Recap</h2>
<p dir="auto"> </p>
<p dir="auto">Let’s walk through the full connection process:</p>
<ol>
<li><strong>Player 1</strong> enters a unique session ID (like “1234”) and creates a cloud session. They wait for <code>☁ ready2 = 1</code>.</li>
<li><strong>Player 2</strong> uses the same session ID to join. If successful, they set <code>☁ ready2 = 1</code> and wait for the game to start.</li>
<li>When <strong>player 1</strong> sees <code>☁ ready2 = 1</code>, they get a “Start Game” button. Clicking it sets <code>☁ game state = 1</code>.</li>
<li>When <strong>both players</strong> see <code>☁ game state = 1</code>, they clear the setup screen and display lives.</li>
</ol>
<p dir="auto">To test the setup on one computer:</p>
<ol>
<li>Save and share the project.</li>
<li>Open a new <strong>incognito window</strong>, log in to <a href="http://creaticode.com" target="_blank" rel="noopener noreferrer nofollow ugc">creaticode.com</a>, and open the project link.</li>
<li>Arrange the two windows side by side.</li>
</ol>
<p dir="auto">Or test with a friend on a different device.</p>
<p dir="auto"> <br />
Here is what it looks like on 2 browsers:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2e4476f9-ac9a-4fae-ab80-a9592eac9a87.gif" alt="setupsession.gif" class=" img-responsive img-markdown" width="1110" height="411" /></p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<h1><a class="anchor-offset" name="module-2-adding-game-objects"></a>Module 2 - Adding Game Objects</h1>
<p dir="auto"> </p>
<p dir="auto">In this module, we’ll add the main objects to the game scene, starting with the trucks and trees. To keep both players’ views identical, we’ll rely on <strong>cloud variables</strong> to make sure these objects are created in exactly the same way on both computers.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-1-broadcast-the-add-trucks-message"></a>Step 1 - Broadcast the “add trucks” message</h2>
<p dir="auto"> </p>
<p dir="auto">After the life count is displayed on both computers, we broadcast a message called <strong>“add trucks”</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ffec55f6-0a3d-403e-b768-9f8ff69929bf.png" alt="ec50b2dc-4ab9-40d7-a791-1ac8701ce5c2-image.png" class=" img-responsive img-markdown" width="368" height="237" /></p>
<p dir="auto"> <br />
<img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a2b34179-4053-4b58-a660-3610026bb5db.png" alt="484bc863-b60b-4491-8425-56a342499f04-image.png" class=" img-responsive img-markdown" width="357" height="267" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-2-create-truck-clones"></a>Step 2 - Create Truck Clones</h2>
<p dir="auto"> <br />
Switch to the <strong>Truck</strong> sprite.</p>
<p dir="auto">Since we only use <strong>clones</strong> for gameplay, hide the original sprite when the green flag is clicked. When the sprite receives the <code>"add trucks"</code> message, it will create <strong>two clones</strong>, one for each player:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5fb0072f-64c4-4a1b-800e-b7ba3f51ffe0.png" alt="43d93ad4-a13e-4bf6-a05d-5f4556771446-image.png" class=" img-responsive img-markdown" width="343" height="307" /></p>
<p dir="auto"> <br />
Each clone has a unique <strong>clone ID</strong>:</p>
<ul>
<li><code>1</code> represents Player 1’s truck</li>
<li><code>2</code> represents Player 2’s truck</li>
</ul>
<p dir="auto">In total, there are <strong>4 clones running across both computers</strong>: each player controls their own truck locally, and the other player sees a “mirror” clone of that truck on their computer.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-3-initialize-the-truck-clone-state"></a>Step 3 - Initialize the Truck Clone State</h2>
<p dir="auto"> </p>
<p dir="auto">Every truck clone will start with a <code>☁ my state</code> variable with a value of <code>0</code>. This value indicates which direction the truck is moving:</p>
<ul>
<li><code>0</code>: Not moving</li>
<li><code>1</code>: Moving up</li>
<li><code>2</code>: Moving down</li>
<li><code>3</code>: Moving left</li>
<li><code>4</code>: Moving right</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ba5576ec-9cbb-47ba-ad36-974aa64b61ee.png" alt="e907c86f-297a-4f5c-a510-598b575370dd-image.png" class=" img-responsive img-markdown" width="264" height="132" /></p>
<p dir="auto"> <br />
Note that the <code>☁ my state</code> variable is a cloud variable, and each truck clone has its own <code>☁ my state</code> variable. The value of this variable is synchronized between the 2 clones with the same ID on the 2 computers. For example:</p>
<p dir="auto">If Player 1 sets clone 1’s state to <code>3</code> on their computer, the state of clone 1’s mirror on Player 2’s computer is also updated to <code>3</code>, so the clone 1 truck will start moving left on <strong>both computers</strong>. Meanwhile, clone 2’s state is unaffected.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-4-additional-truck-initialization"></a>Step 4 - Additional Truck Initialization</h2>
<p dir="auto"> </p>
<p dir="auto">Each truck clone should also be initialized with:</p>
<ul>
<li>A <strong>life count</strong> of <code>3</code></li>
<li>A <strong>speed</strong> of <code>40</code> (feel free to change the speed)</li>
<li>A <strong>costume</strong> based on its ID (<code>1</code> for yellow, <code>2</code> for red)</li>
<li>And it should be <strong>shown</strong>, since the original Truck sprite stays hidden</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/be1763db-96ad-445b-a6fc-ff105a31c0cf.png" alt="bd3d5356-86db-42fa-9387-c2cb0b6f5337-image.png" class=" img-responsive img-markdown" width="281" height="319" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-5-set-starting-positions"></a>Step 5 - Set Starting Positions</h2>
<p dir="auto"> </p>
<p dir="auto">Now place the two truck clones in opposite corners:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d2fdb032-3f1d-48d3-bd35-b87f90fc7a9e.png" alt="6ec4b79a-a6e9-417e-ad27-6588a8a67b0c-image.png" class=" img-responsive img-markdown" width="305" height="426" /></p>
<p dir="auto"> <br />
After running the project, you’ll see the two trucks appear like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/40adbf79-e570-42e1-bbbc-6f1cc168e656.png" alt="07fe0e07-97e0-4d66-b65d-4b6c3ffb0855-image.png" class=" img-responsive img-markdown" width="1098" height="411" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-6-broadcast-the-add-objects-message-from-setup"></a>Step 6 - Broadcast the “add objects” message from “Setup”</h2>
<p dir="auto"> </p>
<p dir="auto">Back in the <strong>Setup</strong> sprite, after the trucks are added (on <strong>Player 1’s</strong> computer only), we send the <code>"add objects"</code> broadcast:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/38d18fe9-0269-4390-8732-5cde9e8727ff.png" alt="f5437ada-b0b5-4b21-9c35-08898f321269-image.png" class=" img-responsive img-markdown" width="372" height="277" /></p>
<p dir="auto"> <br />
Only Player 1 sends this message. Player 1’s program will decide when and where to add the objects, and then share those details with Player 2 using cloud variables.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-7-configure-the-tree-count"></a>Step 7 - Configure the Tree Count</h2>
<p dir="auto"> </p>
<p dir="auto">Trees are static obstacles that block both trucks and the donuts they shoot. Use the variable <code>tree count</code> to define how many trees to add.</p>
<p dir="auto">Set this value in the <strong>Setup</strong> sprite:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/82c78de1-ec2c-4d57-bae8-a1b5711bc83b.png" alt="d07b4906-39d1-44f2-ad5b-3db0a6763287-image.png" class=" img-responsive img-markdown" width="354" height="326" /></p>
<p dir="auto"> <br />
Then, in the <strong>Tree</strong> sprite, when it receives the <code>"add objects"</code> message, it checks if <code>tree count &gt; 0</code>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/826eb8d3-2e30-4a11-a49d-3d094350ca50.png" alt="aac7c68b-01a6-4f95-836a-52dc0ee02fb3-image.png" class=" img-responsive img-markdown" width="319" height="198" /></p>
<p dir="auto"> <br />
Again, this part of the code runs <strong>only on Player 1’s computer</strong> after the “Start Game” button is clicked.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-8-generate-a-tree-list"></a>Step 8 - Generate a Tree List</h2>
<p dir="auto"> </p>
<p dir="auto">To ensure trees appear in the same places for both players, we generate their positions on Player 1’s computer, then share them using a cloud variable.</p>
<p dir="auto">We use the format <code>"x_y"</code> for each position. A list of 2 tree positions might look like:</p>
<pre><code>120_-23_100_70
</code></pre>
<p dir="auto">Here’s how to build that list step by step:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/367285f3-a8d2-41d6-bbd4-993a90afe5fe.png" alt="fad951e8-dfb4-4e92-98fd-914c6fecaefc-image.png" class=" img-responsive img-markdown" width="1101" height="357" /></p>
<p dir="auto"> <br />
The <code>tree list</code> variable starts as empty, and then we repeatedly append a random x position and a random y position to it, all using the “_” separator.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-9-share-the-list-using-code-all-trees-code"></a>Step 9 - Share the List Using <code>☁ all trees</code></h2>
<p dir="auto"> </p>
<p dir="auto">Once Player 1 has the full list, assign it to the cloud variable <code>☁ all trees</code>.<br />
Then both computers will extract the same data and generate trees accordingly:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/871222dc-1b38-49cb-93d9-9564de04d7c7.png" alt="2da8c79b-82f0-4f42-9669-8754be44bc89-image.png" class=" img-responsive img-markdown" width="813" height="616" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-10-create-tree-clones"></a>Step 10 - Create Tree Clones</h2>
<p dir="auto"> </p>
<p dir="auto">To create each clone:</p>
<ol>
<li>Extract the x and y positions from the list using the <code>"part of by"</code> block</li>
<li>Move the Tree sprite to that location</li>
<li>Create the clone</li>
</ol>
<p dir="auto">Each tree takes up <strong>2 entries</strong> in the list (x and y). So tree #1 uses part 1 and 2, tree #2 uses part 3 and 4, and so on.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4d84ad1e-5d98-4787-92d2-5e0a04d77e77.png" alt="154735de-dd3e-4c0f-924d-65b936531c69-image.png" class=" img-responsive img-markdown" width="380" height="73" /></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/61075324-233c-4296-8803-361119483076.png" alt="4f58af6c-7060-485f-ae93-4dbd5192a4c0-image.png" class=" img-responsive img-markdown" width="372" height="75" /></p>
<p dir="auto"> </p>
<p dir="auto">Use a loop to process each tree:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2ba74c1c-8806-4590-9dda-5037e713d078.png" alt="5c5daf47-07c1-4284-a559-e2572533ef01-image.png" class=" img-responsive img-markdown" width="1201" height="322" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-11-hide-the-original-tree-sprite"></a>Step 11 - Hide the Original Tree Sprite</h2>
<p dir="auto"> </p>
<p dir="auto">We’ll only use <strong>clones</strong> for the trees. Hide the original sprite and show each clone when it’s created:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/631694a1-3c31-4fd9-bcc6-b76adb6349a5.png" alt="hide-tree-original.png" class=" img-responsive img-markdown" width="499" height="177" /></p>
<p dir="auto">Now, test the game—you should see identical trees on both screens:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d5f85385-3e22-4852-a497-453465224f8d.png" alt="trees-both-sides.png" class=" img-responsive img-markdown" width="1104" height="412" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-12-add-sugar-objects"></a>Step 12 - Add Sugar objects</h2>
<p dir="auto"> </p>
<p dir="auto">Sugar objects restore a truck’s life when collected.</p>
<p dir="auto">Unlike trees, Sugars appear dynamically during gameplay, and may also disappear if not collected. So they’re handled differently.</p>
<p dir="auto">In the <strong>Sugar</strong> sprite, when <code>"add objects"</code> is received:</p>
<ul>
<li>Start a loop to randomly generate Sugar positions</li>
<li>Each Sugar gets a unique ID</li>
<li>A cloud variable <code>☁ new sugar info</code> stores <code>"ID_x_y"</code> for the next clone</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d01ceacb-e4d7-4855-81df-d6e5db238370.png" alt="sugar-gen-code.png" class=" img-responsive img-markdown" width="1212" height="372" /></p>
<p dir="auto">Key points:</p>
<ul>
<li>IDs start at 1 and increment</li>
<li>Wait a random 30–90 seconds between Sugars</li>
<li>This loop runs only on Player 1’s computer</li>
</ul>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-13-create-sugar-clones"></a>Step 13 - Create Sugar Clones</h2>
<p dir="auto"> </p>
<p dir="auto">On <strong>both computers</strong>, whenever <code>☁ new sugar info</code> changes, split it to get the ID and coordinates, then create a clone.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2d65653f-8177-48ee-badc-fe97fa177626.png" alt="69e6217b-8bde-49ff-91de-a34ec224fa30-image.png" class=" img-responsive img-markdown" width="853" height="283" /></p>
<p dir="auto"> <br />
Before cloning, check the <strong>clone ID</strong> is “originalsprite” to avoid duplicate clones from existing ones.</p>
<p dir="auto">Use the first part (ID) to uniquely identify each clone.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-14-hide-the-original-sugar-sprite"></a>Step 14 - Hide the Original Sugar Sprite</h2>
<p dir="auto"> </p>
<p dir="auto">Just like with trees, hide the original Sugar sprite and show only its clones:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/631694a1-3c31-4fd9-bcc6-b76adb6349a5.png" alt="hide-tree-original.png" class=" img-responsive img-markdown" width="499" height="177" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-15-auto-delete-sugar-clones"></a>Step 15 - Auto-Delete Sugar Clones</h2>
<p dir="auto"> </p>
<p dir="auto">Each Sugar clone will wait 15–25 seconds. If it’s not collected, it deletes itself:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/affd3954-85d0-40a9-9b4c-59f3d3e02e75.png" alt="74fca837-90a1-4927-b23a-b740087c11f7-image.png" class=" img-responsive img-markdown" width="411" height="237" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-16-add-gas-objects-speed-boosts"></a>Step 16 - Add Gas Objects (Speed Boosts)</h2>
<p dir="auto"> </p>
<p dir="auto">The <strong>Gas</strong> sprite works just like Sugar, but boosts the truck’s <strong>speed</strong> instead of restoring life.</p>
<p dir="auto">Just duplicate the Sugar sprite’s code and adjust the variables:</p>
<ul>
<li><code>next gas ID</code></li>
<li><code>new gas info</code></li>
<li>Any references to “Sugar” should be changed to “Gas”</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/24512a27-70dd-4f1b-843e-37e199b27efc.png" alt="112d7dd6-db6d-4168-90ce-e1ae88d64e3b-image.png" class=" img-responsive img-markdown" width="945" height="872" /></p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<h1><a class="anchor-offset" name="module-3-keyboard-control-of-the-trucks"></a>Module 3 - Keyboard Control of the Trucks</h1>
<p dir="auto"> </p>
<p dir="auto">Now that the game world is set up, it’s time to let players control their trucks using the keyboard. But remember, this is a two-player online game. That means when a player presses a key to move their truck, the same movement must happen <strong>in real-time on both computers</strong>.</p>
<p dir="auto">We’ll accomplish this using cloud variables to send and sync movement commands.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-1-broadcast-the-start-game-message"></a>Step 1 - Broadcast the “start game” Message</h2>
<p dir="auto"> </p>
<p dir="auto">Once all objects are added and the game is ready, we need to let both players begin controlling their trucks. In the <strong>Setup</strong> sprite, broadcast the <code>"start game"</code> message <strong>on both computers</strong>:</p>
<p dir="auto">For <strong>Player 1</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5911d4d8-9f48-4a28-874a-c5278f886288.png" alt="startgame-player1.png" class=" img-responsive img-markdown" width="433" height="374" /></p>
<p dir="auto">For <strong>Player 2</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8780a953-0db8-482f-83f4-902cb9c73a78.png" alt="startgame-player2.png" class=" img-responsive img-markdown" width="450" height="316" /></p>
<p dir="auto">In the <strong>Controller</strong> sprite, we’ll listen for this message and begin a <code>forever</code> loop to handle keyboard inputs:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/186d8ded-b7ae-4982-8e06-e3df73714e23.png" alt="controller-loop.png" class=" img-responsive img-markdown" width="326" height="223" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-2-detect-key-press-and-set-command"></a>Step 2 - Detect Key Press and Set Command</h2>
<p dir="auto"> </p>
<p dir="auto">Within the loop, check which arrow key the player is pressing:</p>
<ul>
<li><code>1</code>: Up</li>
<li><code>2</code>: Down</li>
<li><code>3</code>: Left</li>
<li><code>4</code>: Right</li>
<li><code>0</code>: No key is pressed (stay still)</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9db0b7d8-eaac-4315-b963-9af8515e38b0.png" alt="key-detection.png" class=" img-responsive img-markdown" width="451" height="919" /></p>
<p dir="auto">This value is stored in the <code>new command</code> variable.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-3-send-the-command-only-if-it-changes"></a>Step 3 - Send the Command Only If It Changes</h2>
<p dir="auto"> </p>
<p dir="auto">Since this loop runs continuously (many times per second), it could end up sending the same command over and over. That’s unnecessary and wasteful. So we’ll compare the current command to the last command sent, and only send a new command when it actually changes.</p>
<p dir="auto">First, store the command 0 in a <code>current command</code> variable initially:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8677fa96-a2ef-47e4-8cc4-284b25981c89.png" alt="set-current-command.png" class=" img-responsive img-markdown" width="407" height="251" /><br />
 <br />
Then, compare it with the <code>new command</code>. If different, save this new command:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9d716250-75db-4915-b172-c67bb598f047.png" alt="compare-command.png" class=" img-responsive img-markdown" width="616" height="632" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-4-send-the-command-to-the-correct-truck"></a>Step 4 - Send the Command to the Correct Truck</h2>
<p dir="auto"> </p>
<p dir="auto">Each player controls <strong>one</strong> of the two trucks. Player 1 controls truck 1, and player 2 controls truck 2. On both computers, <strong>both trucks exist</strong>, so we need to ensure that <strong>only the correct truck clone</strong> receives the command from the player.</p>
<p dir="auto">Here’s how we’ll do it:</p>
<ul>
<li>Use the <code>my team</code> variable (1 or 2) to send a message with that same number</li>
<li>Attach the <code>new command</code> as the parameter (we have made sure this is different from before)</li>
</ul>
<p dir="auto">So Player 1 will send message <code>"1"</code> with the command, and Player 2 will send <code>"2"</code>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c3c218d3-b804-4faa-b210-e3534779d52f.png" alt="c49bb7cd-1aaf-4c14-9a8c-a82769961baa-image.png" class=" img-responsive img-markdown" width="608" height="337" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-5-let-the-correct-truck-handle-the-command"></a>Step 5 - Let the Correct Truck Handle the Command</h2>
<p dir="auto"> </p>
<p dir="auto">Switch to the <strong>Truck</strong> sprite.</p>
<p dir="auto">All truck clones (ID 1 and ID 2) will receive the message <code>"1"</code> or <code>"2"</code>, but only the clone with the matching ID should actually respond. Use an <code>if</code> block to check the <code>clone ID</code>.</p>
<p dir="auto">Then define a custom block called <code>handle command</code> that will process the movement:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2876f259-e549-463a-a8e8-ea87b387c4a7.png" alt="handlecommand-block.png" class=" img-responsive img-markdown" width="444" height="437" /></p>
<p dir="auto">So if Player 1 presses a key:</p>
<ul>
<li>On Player 1’s computer, <strong>Truck 1</strong> responds and updates its state.</li>
<li>On Player 2’s computer, <strong>Truck 1’s mirror</strong> will also update its state.</li>
</ul>
<p dir="auto">This keeps both trucks behaving identically on both screens.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-6-set-code-my-state-code-cloud-variable"></a>Step 6 - Set <code>☁ my state</code> Cloud Variable</h2>
<p dir="auto"> </p>
<p dir="auto">Inside the <code>handle command</code> block, set the cloud variable <code>☁ my state</code> to the command value:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c8ba841b-014f-45c6-b81b-0bc0d0970025.png" alt="set-my-state.png" class=" img-responsive img-markdown" width="348" height="160" /></p>
<p dir="auto">Each truck clone owns its own cloud variable. This variable is <strong>not visible</strong> to other clones or sprites locally—but because it’s a <strong>cloud variable</strong>, the value is automatically shared with the other player’s computer.</p>
<p dir="auto">So the state of a truck is immediately synced across both computers. That’s how both players see the <strong>exact same movement</strong> for the same truck.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-7-calculate-travel-duration-and-distance"></a>Step 7 - Calculate Travel Duration and Distance</h2>
<p dir="auto"> </p>
<p dir="auto">Now we’ll build the movement loop. To ensure that truck speed is consistent across fast and slow computers, calculate how much time has passed between iterations of the loop using the timer.</p>
<p dir="auto">Use that to determine how far the truck should travel:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a6465485-18c2-49ca-95e0-4ad00f5da728.png" alt="calculate-timer-distance.png" class=" img-responsive img-markdown" width="633" height="658" /></p>
<p dir="auto">For example:</p>
<ul>
<li>On a fast computer, the loop might run 40 times/sec, so <code>travel duration ≈ 0.025</code></li>
<li>On a slower one, maybe 20 times/sec, so <code>travel duration ≈ 0.05</code></li>
</ul>
<p dir="auto">Multiply speed by time to get consistent movement regardless of computer speed.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-8-move-the-truck-up-state-1"></a>Step 8 - Move the Truck Up (State = 1)</h2>
<p dir="auto"> </p>
<p dir="auto">Now use that travel distance to move the truck in the right direction. Start by handling state <code>1</code> (move <strong>up</strong>)</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/54141de1-74ab-478e-9e98-fa50633fe9c5.png" alt="391fc6b3-60db-4cce-b52f-980d9651ad12-image.png" class=" img-responsive img-markdown" width="620" height="489" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-9-move-in-the-other-directions-states-2-4"></a>Step 9 - Move in the Other Directions (States 2–4)</h2>
<p dir="auto"> </p>
<p dir="auto">Repeat this logic for states <code>2</code> (down), <code>3</code> (left), and <code>4</code> (right):</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/443ccc57-80dc-4f93-9bff-e6e07d20ff06.png" alt="move-234.png" class=" img-responsive img-markdown" width="423" height="873" /></p>
<p dir="auto">No need to handle state <code>0</code> directly—if no key is pressed, the truck simply doesn’t move.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-10-undo-movement-if-hitting-a-tree-or-edge"></a>Step 10 - Undo Movement if Hitting a Tree or Edge</h2>
<p dir="auto"> </p>
<p dir="auto">If the truck hits a tree or touches the edge, immediately reverse the last movement:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/54773884-9005-45f7-b4d8-a7dc43fd4a9d.png" alt="reverse-hit.png" class=" img-responsive img-markdown" width="604" height="747" /><br />
 <br />
Put this logic <strong>at the top level inside the loop</strong>, so it always runs on every frame, regardless of what state the truck is in.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-11-sync-position-when-truck-is-idle-command-0"></a>Step 11 - Sync Position When Truck is Idle (Command = 0)</h2>
<p dir="auto"> </p>
<p dir="auto">Even with identical movement commands, trucks can drift out of sync over time due to slight timing delays. Here’s why:</p>
<p dir="auto">Imagine Player 1 moves the yellow truck up, then presses right. The command is instantly executed on Player 1’s computer. But there’s a small delay (maybe 0.1 seconds) before that command reaches Player 2. So on Player 2’s screen, the yellow truck moves <strong>slightly farther up</strong> before turning.</p>
<p dir="auto">To fix this, whenever a truck receives command <code>0</code> (no movement), we broadcast its <strong>exact position</strong> to the other computer using a cloud variable <code>☁ my x y</code> (formatted like <code>"x_y"</code><img src="https://forum.creaticode.com/plugins/nodebb-plugin-emoji/emoji/android/1f61e.png?v=h4bqj8tg448" class="not-responsive emoji emoji-android emoji--disappointed" title="):" alt="😞" /></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/68927a65-6971-4f89-b2aa-41a8e4e465d9.png" alt="sync-on-idle.png" class=" img-responsive img-markdown" width="767" height="311" /><br />
 <br />
Then, when the other computer receives this update, the matching truck clone updates its position <strong>smoothly</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/32cd73a5-0c4c-4b57-ad63-26ca1db43c22.png" alt="receive-sync.png" class=" img-responsive img-markdown" width="823" height="151" /></p>
<p dir="auto">This ensures both versions of the truck stay visually aligned.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-12-sync-position-when-truck-hits-obstacle"></a>Step 12 - Sync Position When Truck Hits Obstacle</h2>
<p dir="auto"> </p>
<p dir="auto">When a truck hits a tree or an edge, it should stop moving—and sync its corrected position to the other computer. You can reuse the <code>handle command</code> block by calling it with command <code>0</code>.</p>
<p dir="auto">But only the truck being controlled <strong>locally by the player</strong> should do this, because only that version has the accurate position. Add this logic <strong>inside the collision detection block</strong>, not at the top level:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/be6bed34-fcd4-438a-91b8-7de338032e0b.png" alt="sync-on-stuck.png" class=" img-responsive img-markdown" width="537" height="778" /></p>
<p dir="auto">Now, both players will always see <strong>exactly the same movement</strong>, even when a truck hits trees or stop moving.</p>
<p dir="auto">Here’s a demo of it in action:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/db5450d8-7491-4125-84ca-3702a3841c90.gif" alt="insync.gif" class=" img-responsive img-markdown" width="1110" height="411" /></p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<h1><a class="anchor-offset" name="module-4-collecting-items"></a>Module 4 - Collecting Items</h1>
<p dir="auto"> </p>
<p dir="auto">In this module, we’ll enable the trucks to <strong>collect Sugar and Gas items</strong>, which can <strong>increase the truck’s life</strong> or <strong>boost its speed</strong>. When a truck picks up one of these items, we need to make sure the effect happens only <strong>once</strong>, and that it syncs across both computers.</p>
<p dir="auto">This means the item should <strong>disappear at the same time</strong> on both screens, and the truck’s speed or life must update only on the <strong>player’s own truck</strong> (not the mirrored one).</p>
<p dir="auto">Let’s go step by step.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-1-detect-touching-the-sugar"></a>Step 1 - Detect Touching the Sugar</h2>
<p dir="auto"> </p>
<p dir="auto">Start in the <strong>Truck</strong> sprite.</p>
<p dir="auto">We’ll use the <strong>“when touching [object]”</strong> block to detect when the truck touches a Sugar clone. But there’s an important detail: each computer has two trucks (clone ID 1 and 2), and we only want <strong>the truck controlled by the local player</strong> to handle the collection.</p>
<p dir="auto">So first, check that <code>my team</code> matches the truck’s <code>clone ID</code>. That ensures only the player’s truck reacts:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c75923ef-8d4c-494b-a5d7-9e1ea6ed50ca.png" alt="truck-touch-sugar.png" class=" img-responsive img-markdown" width="359" height="188" /></p>
<p dir="auto">If you don’t do this check, <strong>both the original truck and its remote mirror</strong> might react, and the item could be collected twice incorrectly.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-2-notify-which-sugar-was-collected"></a>Step 2 - Notify Which Sugar Was Collected</h2>
<p dir="auto"> </p>
<p dir="auto">When a Sugar item is collected, we want <strong>both computers</strong> to delete that Sugar clone. Since each Sugar clone has a unique <strong>ID</strong>, we can use a <strong>cloud variable</strong> to broadcast that ID.</p>
<p dir="auto">Set <code>☁ sugar being collected</code> to the ID of the clone that was touched:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/98985722-583f-474e-9966-d97cd9cd840b.png" alt="set-sugar-being-collected.png" class=" img-responsive img-markdown" width="599" height="219" /></p>
<p dir="auto">Once this variable changes, every copy of the Sugar sprite (on both computers) will be notified.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-3-increase-truck-s-life-max-3"></a>Step 3 - Increase Truck’s Life (Max 3)</h2>
<p dir="auto"> </p>
<p dir="auto">After collecting a Sugar item, the player’s truck should gain one life—but only if its current life is <strong>less than 3</strong>.</p>
<p dir="auto">Here’s the code to do that:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0e227b2c-c8f7-43e4-a173-791a35520af5.png" alt="increase-life.png" class=" img-responsive img-markdown" width="603" height="346" /><br />
 <br />
This check prevents the truck from going above the max life limit.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-4-delete-the-correct-sugar-clone"></a>Step 4 - Delete the Correct Sugar Clone</h2>
<p dir="auto"> </p>
<p dir="auto">Now switch to the <strong>Sugar</strong> sprite.</p>
<p dir="auto">We want only the <strong>clone with the matching ID</strong> to delete itself when <code>☁ sugar being collected</code> changes. Use the <code>when [cloud variable] changes</code> hat block, and compare the clone’s ID with the new value:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/35bb3fa4-97b1-4f6e-a7f3-a01fd45c12d2.png" alt="delete-sugar-clone.png" class=" img-responsive img-markdown" width="478" height="224" /><br />
 <br />
This event runs on both computers, but <strong>only the matching clone</strong> actually deletes itself.</p>
<p dir="auto">To make this detection work reliably, set <code>☁ sugar being collected</code> to an <strong>invalid ID</strong> at the beginning of the game, like <code>-1</code> or <code>"none"</code>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/faf415aa-79ca-47f2-b8f5-721d810386d7.png" alt="init-sugar-variable.png" class=" img-responsive img-markdown" width="485" height="254" /><br />
 <br />
If you skip this step, the first clone might mistakenly delete itself before anything has been collected.</p>
<p dir="auto">Now test it out: try adjusting the Sugar spawn delay to appear more frequently so you can quickly see the effect during testing:<br />
 <br />
<img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d67f1724-0c2c-484f-a49b-d56abeb080e7.gif" alt="sugarcollect.gif" class=" img-responsive img-markdown" width="1110" height="411" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-5-handle-touching-gas-objects"></a>Step 5 - Handle Touching Gas Objects</h2>
<p dir="auto"> </p>
<p dir="auto">Back in the <strong>Truck</strong> sprite, we’ll use similar logic to handle collecting <strong>Gas</strong> objects. These do not increase life—they <strong>increase the truck’s speed</strong> instead.</p>
<p dir="auto">Duplicate the Sugar collection code and change it to work with Gas. When touched, increase the speed variable of the truck:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7d5dbae3-368a-493b-a3ed-3d7ed9b862e7.png" alt="touch-gas.png" class=" img-responsive img-markdown" width="687" height="416" /><br />
 <br />
Again, remember to <strong>only allow the local player’s truck</strong> to trigger the collection.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-6-delete-the-gas-clone"></a>Step 6 - Delete the Gas Clone</h2>
<p dir="auto"> </p>
<p dir="auto">In the <strong>Gas</strong> sprite, add logic similar to Sugar. When the cloud variable <code>☁ gas being collected</code> changes, compare its value to each clone’s ID. If it matches, delete the clone:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8eff53cf-de5f-406f-acf6-b8be09b94f57.png" alt="delete-gas.png" class=" img-responsive img-markdown" width="560" height="257" /><br />
 <br />
And just like with Sugar, initialize this variable to an invalid value at the start:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/55fb1060-7058-437c-9ae6-83110d6f7aaa.png" alt="init-gas-variable.png" class=" img-responsive img-markdown" width="485" height="235" /><br />
 <br />
This avoids accidental deletions or unexpected behavior.</p>
<p dir="auto">Once you’ve added this code, try testing the collection of both Sugar and Gas. Make sure:</p>
<ul>
<li>Only one truck can collect the item (not the mirror)</li>
<li>The effect applies only to that truck</li>
<li>The item disappears on both screens</li>
</ul>
<p dir="auto">This logic is essential for keeping both players’ games perfectly in sync.</p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<h1><a class="anchor-offset" name="module-5-shooting-donuts"></a>Module 5 - Shooting Donuts</h1>
<p dir="auto"> </p>
<p dir="auto">In this module, we’ll let players shoot <strong>donuts</strong> from their trucks. Donuts travel in a straight line and disappear when they hit a tree, the edge of the stage, or the opponent truck. If a donut hits the opponent’s truck, it will <strong>lose a life</strong>. As always, everything must stay synchronized between both players’ computers.</p>
<p dir="auto">Let’s walk through each part of the shooting mechanic.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-1-detect-space-key-to-fire"></a>Step 1 - Detect SPACE Key to Fire</h2>
<p dir="auto"> </p>
<p dir="auto">In the <strong>Controller</strong> sprite, add a condition to detect when the player presses the <strong>SPACE</strong> key. When that happens, broadcast a <code>"shoot donut"</code> message.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ad5e6b9f-ead3-4177-91e9-e8bf25aeb5a8.png" alt="ca2b8092-5090-4b15-909b-b736e2739c68-image.png" class=" img-responsive img-markdown" width="463" height="465" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-2-only-original-donut-handles-the-message"></a>Step 2 - Only Original Donut Handles the Message</h2>
<p dir="auto"> </p>
<p dir="auto">Switch to the <strong>Donut</strong> sprite. When the <code>"shoot donut"</code> message is sent out, it will be received by both the original Donut and all its clones. So, add a check to make sure <strong>only the original</strong> sprite handles it:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0f9c97dd-4707-42ad-8217-8e86e72deb82.png" alt="a863f526-6d52-4546-98c2-96f02939cc71-image.png" class=" img-responsive img-markdown" width="466" height="237" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-3-limit-shooting-frequency"></a>Step 3 - Limit Shooting Frequency</h2>
<p dir="auto"> </p>
<p dir="auto">To prevent players from spamming the SPACE key, introduce a cooldown. Create two variables:</p>
<ul>
<li><code>donut_time</code>: last time a donut was fired</li>
<li><code>donut_interval</code>: minimum wait time between shots (e.g., 1 second)</li>
</ul>
<p dir="auto">Initialize them like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b5c1916e-d382-465f-95ab-116c8577c7bc.png" alt="donut-time-init.png" class=" img-responsive img-markdown" width="353" height="201" /><br />
 <br />
And check the current timer value before allowing a new shot:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3ab19574-da7e-4084-a296-cbe3b7113269.png" alt="7db53c54-f190-4c4a-bfc2-b9f77239f0e7-image.png" class=" img-responsive img-markdown" width="660" height="368" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-4-get-truck-position-and-direction"></a>Step 4 - Get truck position and direction</h2>
<p dir="auto"> </p>
<p dir="auto">A donut should appear <strong>at the front of the truck</strong>, facing the same direction. Use the sensing blocks to get the x/y position and direction of the truck <strong>the player is controlling</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7c9f4d75-a997-4449-83f0-7b9c6702374a.png" alt="get-truck-position.png" class=" img-responsive img-markdown" width="877" height="573" /><br />
 <br />
Use <code>my team</code> to access the truck clone ID that matches the player.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-5-track-donut-id"></a>Step 5 - Track Donut ID</h2>
<p dir="auto"> </p>
<p dir="auto">Each donut clone should have a unique ID. Use a variable <code>next donut ID</code>, initialized to 0:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f0d1c1d9-6935-4900-bcc3-104050514e88.png" alt="init-donut-id.png" class=" img-responsive img-markdown" width="355" height="286" /></p>
<p dir="auto">Increase it by 1 with each new shot:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8c28e1b7-9676-488a-9c27-e4765ac0e21e.png" alt="7925599a-d71d-4c97-a97e-9d27534d9a8a-image.png" class=" img-responsive img-markdown" width="475" height="622" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-6-create-code-new-donut-command-code"></a>Step 6 - Create <code>☁ new donut command</code></h2>
<p dir="auto"> </p>
<p dir="auto">Use a cloud variable <code>☁ new donut command</code> to let both computers know a new donut was fired.</p>
<p dir="auto">Its format will be:</p>
<p dir="auto"><code>playerID_direction_x_y_donutID</code></p>
<p dir="auto">Start with the direction <code>90</code> (right). Offset the x-position by 35 so the donut doesn’t touch the truck that fired it:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7e0e122c-011a-40be-9147-9fc77196c2d2.png" alt="8b2df66f-6d40-4f73-a2da-5d0189496cbe-image.png" class=" img-responsive img-markdown" width="1108" height="267" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-7-new-donut-commands-for-the-other-three-directions"></a>Step 7 - New donut commands for the other three directions</h2>
<p dir="auto"> </p>
<p dir="auto">Repeat the same logic for directions:</p>
<ul>
<li><code>0</code> (up): adjust <strong>y</strong></li>
<li><code>180</code> (down): adjust <strong>y</strong></li>
<li><code>270</code> (left): adjust <strong>x</strong></li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/44cc9747-475e-4b37-bdec-3653939c0f20.png" alt="b11bf926-5d97-4fc4-8539-e5f35880a2dc-image.png" class=" img-responsive img-markdown" width="1108" height="673" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-8-handle-updates-in-code-new-donut-command-code"></a>Step 8 - Handle updates in <code>☁ new donut command</code></h2>
<p dir="auto"> </p>
<p dir="auto">When the cloud variable <code>☁ new donut command</code> changes, both computers will create a new donut <strong>clone</strong>.</p>
<p dir="auto">Make sure <strong>only the original Donut sprite</strong> handles this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/659e36a8-7646-4806-bc3f-f858406add99.png" alt="c414302c-3fe0-4f77-8ea1-fc07a72bb8d3-image.png" class=" img-responsive img-markdown" width="532" height="254" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-9-extract-donut-info"></a>Step 9 - Extract Donut Info</h2>
<p dir="auto"> </p>
<p dir="auto">Split the command string into 2 parts and store them:</p>
<ul>
<li><code>owner team</code>: the shooting player</li>
<li><code>my direction</code>: the direction the donut should move in</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b24f5a43-23bb-4f18-97bd-2765196ab79a.png" alt="c7d9a2d3-551a-4934-a574-abdca16d07d8-image.png" class=" img-responsive img-markdown" width="783" height="332" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-10-move-donut-to-position-and-set-angle"></a>Step 10 - Move Donut to Position and Set Angle</h2>
<p dir="auto"> </p>
<p dir="auto">Move the original Donut sprite to the target position and direction. The clone will inherit this when it’s created:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/23c7cd63-1342-44f5-b984-ce5426d3ab32.png" alt="a0e3f559-856b-45f0-808f-962cc39111f4-image.png" class=" img-responsive img-markdown" width="1132" height="459" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-11-create-the-donut-clone"></a>Step 11 - Create the Donut Clone</h2>
<p dir="auto"> </p>
<p dir="auto">Now create the donut clone. Its ID will be a combo of the player’s ID and the sequence number:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3260cd5d-3314-4d9a-8adf-ab0780aed67e.png" alt="c80015ee-0406-4755-a88d-ec1761887c06-image.png" class=" img-responsive img-markdown" width="1179" height="433" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-12-set-costume-by-team"></a>Step 12 - Set Costume by Team</h2>
<p dir="auto"> </p>
<p dir="auto">Set the donut costume based on the player who fired it:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4d848590-2e2d-4ca5-ad96-d12b5e349323.png" alt="ff0b0c44-15f0-4727-8309-b05718c2078d-image.png" class=" img-responsive img-markdown" width="287" height="148" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-13-hide-the-original-donut-sprite"></a>Step 13 - Hide the Original Donut Sprite</h2>
<p dir="auto"> </p>
<p dir="auto">Only clones should be visible. The original Donut stays hidden:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/21eef390-9c9b-4fc5-9458-cbd8d608da77.png" alt="fcefe505-18da-4679-93ee-73c26a4ff6cf-image.png" class=" img-responsive img-markdown" width="322" height="282" /></p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9550ed58-d8ec-4b99-a225-94e3cc3e67e0.png" alt="16aad3ca-23c6-46ef-9a7d-ba7776735fc5-image.png" class=" img-responsive img-markdown" width="311" height="193" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-14-calculate-travel-distance-based-on-timer"></a>Step 14 - Calculate Travel Distance Based on Timer</h2>
<p dir="auto"> </p>
<p dir="auto">Just like trucks, donuts should move consistently across fast and slow computers. Use the timer and a constant speed (e.g. 150) to calculate how far each donut moves every frame:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/92678085-1c43-4250-998d-75214fe65273.png" alt="donut-travel-distance.png" class=" img-responsive img-markdown" width="536" height="419" /></p>
<p dir="auto">Each clone tracks its own <code>prev time</code> and <code>travel distance</code>.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-15-move-the-donut"></a>Step 15 - Move the Donut</h2>
<p dir="auto"> </p>
<p dir="auto">Move the donut in its set direction using the calculated distance:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4869d568-38d7-4a81-acf7-6921104a7b8f.png" alt="e00ba132-02f3-4bb0-8e93-dc61fea43c64-image.png" class=" img-responsive img-markdown" width="543" height="810" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-16-delete-on-tree-or-edge-only-on-owner-s-side"></a>Step 16 - Delete on Tree or Edge (Only on Owner’s Side)</h2>
<p dir="auto"> </p>
<p dir="auto">Only the player who <strong>owns</strong> the donut should check for collisions (to avoid false positives due to lag). If a donut touches a tree or the edge:</p>
<ul>
<li>Set <code>☁ donut to delete</code> to its ID</li>
<li>Both computers delete the clone when this variable updates</li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a69ed308-aef3-49e3-89cf-969e02b274bf.png" alt="0ab2eef4-235d-496b-8513-303c10da800a-image.png" class=" img-responsive img-markdown" width="635" height="544" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-17-detect-hitting-the-opponent"></a>Step 17 - Detect Hitting the Opponent</h2>
<p dir="auto"> </p>
<p dir="auto">If the donut hits the <strong>opponent truck</strong>, do the same thing: set a cloud variable <code>☁ donut hitting target</code> to the donut’s ID:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e612d3df-579e-448c-96f6-2a76395d6269.png" alt="6835d348-d1aa-4586-baf9-d1383f1ebd90-image.png" class=" img-responsive img-markdown" width="607" height="433" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-18-delete-the-donut"></a>Step 18 - Delete the Donut</h2>
<p dir="auto"> </p>
<p dir="auto">In the <strong>Donut</strong> sprite, when <code>☁ donut to delete</code> updates, each clone compares its ID and deletes itself if it matches:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/93affc76-4cdc-4090-a92a-694d4d89e177.png" alt="aaa08493-cff5-4532-a8d4-b1230d40729b-image.png" class=" img-responsive img-markdown" width="427" height="236" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-19-handle-opponent-hit"></a>Step 19 - Handle Opponent Hit</h2>
<p dir="auto"> </p>
<p dir="auto">Similarly, when <code>☁ donut hitting target</code> changes, the matching clone:</p>
<ol>
<li>Sends a <code>truck hit</code> message with the opponent truck’s ID. For example, if this clone’s team is 1, then the opponent team’s ID is 2 (3 - 1).</li>
<li>Deletes itself</li>
</ol>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/833f32fb-c80c-40b7-8780-b63c89886ff9.png" alt="99bfd34d-57ce-4346-855a-9ac03c501ac3-image.png" class=" img-responsive img-markdown" width="552" height="276" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-20-truck-handles-truck-hit"></a>Step 20 - Truck Handles “truck hit”</h2>
<p dir="auto"> </p>
<p dir="auto">In the <strong>Truck</strong> sprite, handle this message carefully:</p>
<p dir="auto">Only <strong>the truck that got hit</strong>, and <strong>only on its own player’s computer</strong>, should reduce its life by 1.</p>
<p dir="auto">Use two checks:</p>
<ul>
<li><code>clone ID = truck being hit</code></li>
<li><code>my team = owner of this truck</code></li>
</ul>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/51d3a8d0-3f24-43ff-84fc-0f6bfa4a1aad.png" alt="truck-hit-checks.png" class=" img-responsive img-markdown" width="728" height="235" /><br />
 <br />
Then update <code>☁ my lives</code>, which is mirrored on both computers.</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-21-react-to-changes-in-code-my-lives-code"></a>Step 21 - React to Changes in <code>☁ my lives</code></h2>
<p dir="auto"> </p>
<p dir="auto">Each truck that shares that cloud variable will react when <code>☁ my lives</code> is updated.</p>
<p dir="auto">Make sure the game has started (game state is 1):</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b2105d74-daae-4f01-b8f2-48cd081d2f21.png" alt="52292a20-0798-4591-9b81-108863d8ea51-image.png" class=" img-responsive img-markdown" width="562" height="224" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-22-compare-life-count"></a>Step 22 - Compare Life Count</h2>
<p dir="auto"> </p>
<p dir="auto">Use a separate variable <code>prev my lives</code> (initially 3) to compare the new life count to the previous one:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a4f18e35-d559-4e53-8976-d90b9f26fae8.png" alt="5cc07434-3c0a-4e68-8523-ae922d7efbb6-image.png" class=" img-responsive img-markdown" width="359" height="181" /></p>
<p dir="auto"> </p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1faedc75-f3d6-44ff-8565-5c1448a25a60.png" alt="dfde5a30-496f-4f1f-b4bd-98a0ba752c48-image.png" class=" img-responsive img-markdown" width="662" height="509" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-23-play-sound-effect"></a>Step 23 - Play Sound Effect</h2>
<p dir="auto"> </p>
<p dir="auto">Play different sounds for gaining vs. losing lives:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/20a58da0-811f-42d2-b5c4-0b645417589b.png" alt="93767c74-c89b-47e9-82e8-4fffe51029f2-image.png" class=" img-responsive img-markdown" width="669" height="472" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-24-end-the-game-if-lives-0"></a>Step 24 - End the Game if Lives = 0</h2>
<p dir="auto"> </p>
<p dir="auto">If the truck’s life count drops to zero, end the game. This check happens on both computers:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8cbf55f2-cf83-497e-934e-4e88366a9376.png" alt="8018a2f8-a3c6-4462-9999-ca5be2402b16-image.png" class=" img-responsive img-markdown" width="518" height="498" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-25-show-the-winner"></a>Step 25 - Show the Winner</h2>
<p dir="auto"> </p>
<p dir="auto">Before stopping the game, show a message announcing who won:</p>
<p dir="auto">If truck ID = 1, then <strong>player 2 wins</strong>. If it’s 2, then <strong>player 1 wins</strong>:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e298978e-1d66-4bed-b6ff-da6002fba0a9.png" alt="show-winner.png" class=" img-responsive img-markdown" width="1233" height="672" /></p>
<p dir="auto"> <br />
Here is a full demo of the game:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8594a3cd-60d8-45e8-b9d5-bb9f102568ce.gif" alt="finaldemo.gif" class=" img-responsive img-markdown" width="1110" height="411" /></p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<h1><a class="anchor-offset" name="additional-challenges"></a>Additional Challenges</h1>
<p dir="auto"> </p>
<p dir="auto">The game you’ve built so far is a fully functional 2-player battle, but there’s plenty of room to expand and experiment. The main goal of this project was to demonstrate how <strong>cloud variables</strong> and <strong>clones</strong> can be used to build a multiplayer game where each player runs the same project on their own computer, and everything stays in sync.</p>
<p dir="auto">Once you’ve got the basic mechanics working, here are some ways you can extend and improve the game. These ideas range from simple tweaks to major redesigns—try one or try them all.</p>
<hr />
<h2><a class="anchor-offset" name="img-src-https-forum-creaticode-com-plugins-nodebb-plugin-emoji-emoji-android-1f539-png-v-h4bqj8tg448-class-not-responsive-emoji-emoji-android-emoji-small_blue_diamond-title-small_blue_diamond-alt-firepower-reward"></a><img src="https://forum.creaticode.com/plugins/nodebb-plugin-emoji/emoji/android/1f539.png?v=h4bqj8tg448" class="not-responsive emoji emoji-android emoji--small_blue_diamond" title=":small_blue_diamond:" alt="🔹" /> Firepower Reward</h2>
<p dir="auto">You’ve already added Sugar and Gas as collectible items. Now try adding a <strong>third type of reward</strong> that affects the player’s shooting ability.</p>
<p dir="auto"><strong>What to do:</strong></p>
<ul>
<li>Create a new sprite for a Firepower item</li>
<li>When collected, reduce the cooldown time between donuts (e.g., from 1 second to 0.5 seconds)</li>
<li>Use a new cloud variable to sync this change across both computers</li>
<li>Maybe even show a temporary fire effect or shooting animation</li>
</ul>
<p dir="auto">This reward adds more strategy and makes collecting items more impactful.</p>
<hr />
<h2><a class="anchor-offset" name="img-src-https-forum-creaticode-com-plugins-nodebb-plugin-emoji-emoji-android-1f539-png-v-h4bqj8tg448-class-not-responsive-emoji-emoji-android-emoji-small_blue_diamond-title-small_blue_diamond-alt-shrinking-arena"></a><img src="https://forum.creaticode.com/plugins/nodebb-plugin-emoji/emoji/android/1f539.png?v=h4bqj8tg448" class="not-responsive emoji emoji-android emoji--small_blue_diamond" title=":small_blue_diamond:" alt="🔹" /> Shrinking Arena</h2>
<p dir="auto">To prevent players from hiding or running away forever, you can create a <strong>shrinking play area</strong>—similar to battle royale games.</p>
<p dir="auto"><strong>How it works:</strong></p>
<ul>
<li>Add invisible wall sprites that slowly move inward from the edges of the stage</li>
<li>These walls can appear every 10 seconds or after a certain time has passed</li>
<li>If a truck touches a wall, it loses a life or takes damage</li>
</ul>
<p dir="auto">This forces players toward the center of the stage over time, making the game more intense and guaranteed to end eventually.</p>
<hr />
<h2><a class="anchor-offset" name="img-src-https-forum-creaticode-com-plugins-nodebb-plugin-emoji-emoji-android-1f539-png-v-h4bqj8tg448-class-not-responsive-emoji-emoji-android-emoji-small_blue_diamond-title-small_blue_diamond-alt-2-vs-2-team-game"></a><img src="https://forum.creaticode.com/plugins/nodebb-plugin-emoji/emoji/android/1f539.png?v=h4bqj8tg448" class="not-responsive emoji emoji-android emoji--small_blue_diamond" title=":small_blue_diamond:" alt="🔹" /> 2 vs. 2 Team Game</h2>
<p dir="auto">This is a major upgrade: turn your 2-player battle into a <strong>4-player team battle</strong>, with two players on each team.</p>
<p dir="auto"><strong>What changes:</strong></p>
<ul>
<li>Four players will join the same cloud session, each with their own truck (clone IDs 1 to 4)</li>
<li>Players 1 and 2 are on <strong>Team Yellow</strong>, and Players 3 and 4 are on <strong>Team Red</strong></li>
<li>Each player only controls their own truck, but it has 3 mirror trucks on the other 3 computers</li>
<li>Donuts can damage players from the opposite team, but not teammates</li>
<li>The game ends when <strong>both trucks from one team</strong> have lost all their lives</li>
</ul>
<p dir="auto"><strong>What you’ll need to update:</strong></p>
<ul>
<li>Session setup screen to support 4 unique player IDs</li>
<li>Truck spawning logic to handle 4 clones and assign them to teams</li>
<li>Donut logic to check for friendly fire and block it</li>
<li>Life tracking and win condition logic to check if both teammates are eliminated</li>
<li>Display a win message like “Red Team Wins!” when the condition is met</li>
</ul>
]]></description><link>https://forum.creaticode.com/topic/2017/multi-player-2d-game-candy-truck-battle-difficulty-6</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 05:00:40 GMT</lastBuildDate><atom:link href="https://forum.creaticode.com/topic/2017.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 22 Jun 2025 19:54:59 GMT</pubDate><ttl>60</ttl></channel></rss>