Cloud Game Tutorial | Part 5 | Smooth Movement
Skills:
AI Productivity Tools70%
Key Takeaways
This video tutorial demonstrates how to create smooth movement in a multiplayer cloud game using Scratch 3, by utilizing a buffered data stream to send player data between web browsers, achieving 30fps movement for all players.
Full Transcript
hello fellow scratchers i'm griff patch and welcome to the fifth video in a series creating a cloud game in scratch you fed up with a jittery movement in the cloud games and want to finally get things playing smoothly well don't go anywhere because today is your lucky day we probably all know by now that scratch runs at 30 frames per second which means if we move our cat around the level he will update his position 30 times every second this also means that we must match this rate in our cloud data transmission if we want smooth gameplay for all our players the problem is scratch forces a maximum update rate of 10 frames a second for cloud data that would be one cloud update for every three screen updates and that is why in our previous episodes the other player's movement was still so dissatisfyingly jittery there are a number of ways we can attempt to tackle this problem to simply smooth out the two out of the three missing frames but to be honest one simple approach has consistently worked far better than all others for me and that is to implement a simple buffered transmission stream this luckily sounds far more complex than it really is at present we only send the position of the player every tenth of a second losing the other two in between frame positions all together but no need instead we can record the positions as they occur into a list this is called buffering and then when we send the cloud data we send the whole buffer all three recorded frames in one update this way no data is lost and the receiving scripts will be able to decode the buffer and play back the movements exactly as they occurred without any guesswork to make the movement look any less realistic so we begin in the player sprite let's create a new custom block called player tick markers run without screen refresh remove the go to mouse pointer block into the new custom block and make sure it's called from the same place it used to be let's also tidy up the send cloud data custom block rather than having the if check before calling into the send cloud data let's move it inside the custom block and reverse the logic to stop the script if we haven't yet connected to the cloud we know this when our player hash player number is less than one stopping this script inside a custom block with this stop this script only stops this custom block from running and will continue running any scripts that call the custom block so in other coding languages this might be thought of as a return statement now all this talk of scratch only allowing updates of cloud variables every three frames but griff patch this project is seemingly updating them every frame is it not well yes it is but scratch is just ignoring two out of every three updates so let's take control of situation add a new variable make it for this sprite only and name it cloud timer drag in change cloud timer by 1 and place it in the send cloud data custom block and then if cloud timer is less than 3 then we again stop this script right after the if we can set the cloud timer back to zero this has the effect of now only running the code below the if on the third of every three frames perfect next we need to create the position buffer add a new list ensuring we mark it for this sprite only that is very important and name the list buffer add the exposition and y position of the player to the new buffer list and insert the blocks before the cloud timer check we need to make sure we are recording these every frame it's very good practice to add a delete all block for any list that you intend to start empty and add it to the setup phase of your project okay let's take a moment to give this project a quick run click the green flag and move your mouse around we should be seeing the buffer list quickly filling up with values as the positions of the mouse get recorded frame by frame into the buffer great so the next step is to better send this data to the cloud if you look at the scripts that currently construct the cloud data to be sent you can see that we are sending the following structured data username time x position and y position the four values that we're writing out now we're going to modify this to be sending all three buffered positions now username time x position 1 and y position 1 x position 2 and y position 2 and x position 3 and y position 3. to do this we add a repeat loop in place of the right x and y position blocks this loop will repeat for the length of the buffer list as we want it to send all the items that are in the list inside this loop we first write item one of the buffer list and then immediately afterwards delete the first item so this is the effect of writing out the first second third line after line of this list until there's none left in it because our send cloud data custom block now contains a repeat loop it is worth checking that it is marked as run without screen refresh otherwise we might have the whole project lagging out on this new repeat loop okay let's run the project we can see that the buffer list now begins to fill up but then on the third frame it empties as expected awesome but we are not done yet as we haven't scripted the opponent's sprite to handle this new updated cloud variable data structure click on the opponent's sprite now and we'll fix that right away to be able to decode and play back the position of each opponent player from the new cloud data structure we will again make use of a buffering list from within the opponent's sprite we create a new list now ensuring it's for this sprite only and name it again buffer this way each opponent clone will have its own personal list we again delete all the content of the list in the setup custom block right at the start now we need to modify our tick custom block separate off the begin decode stack from the bottom of the custom block and again split it just before we set the x position of the opponent's sprite now the top part of these scripts is used to decode the player name and prepare the first exposition of the opponent because we only actually need to decode the player's name when the cloud variable has changed we can move the script up into the else of the if block above remember this if checks to see if the cloud variable has changed since we last looked at it next add a repeat until value equals blank within the repeat add value to buffer and a value equals read from encoded custom block so what does this do look at the structure we are reading from again the first read gets the player's name the second gets the time although we simply don't make use of this right now the third read gets the first x position if there is one and otherwise the value will be set to nothing all the remaining values in the cloud variable are x and y position value pairs so we've created a repeat loop that goes around until there is nothing left to read each time around we add the current value from the buffer list and then read another value and around we go again filling up the buffer until all items are read so the buffer list is now filled now we need to play back the positions in the buffer to move the opponent's sprite around at the bottom of the tick custom block we check if the length of buffer is greater than zero if so then there is data buffered ready for playback so set the exposition to item 1 of the buffer this will be the first x position then immediately delete item 1 as we have no more use for it next set the y position again to item 1 of buffer remember that we just deleted the previous item one so item one is now what would have been item two a moment ago which is the y position and again delete item one and that's that each time we run through this tick receiver in the opponent sprite we are checking for new cloud updates if there are any we're adding them to the sprites buffer and we play back a single tick of the buffered positions always the top one in the list because that is the first one put into the buffer list and therefore the oldest we have to all want to play them back in the same order they were recorded okay hold on to your mice because it's time to give this a try i'll do what i usually do and open two web browsers side by side the right one is in incognito mode but you can also use a different browser profile or any way that you want to do this and i log into scratch with a different scratch account also note that they both have to be of scratcher status new scratcher statuses just won't work with cloud data yet oh um so something has gone wrong here this is not working as expected but don't panic let's quickly analyze what's going wrong see how slowly all the movement is in my mind i've already have a suspicion as to the cause if we go back to the opponent's sprite and the tick custom block remember i specifically mentioned to check the custom block in the player sprite that it was now running without screen refresh well we have the same problem here i didn't remember to tell you to do the same in the opponent sprite now we have fallen to the same problem this tick block must run and finish within one scratch run cycle but the repeat loop inside the custom block doesn't allow for that not without the run without screen refresh so let's try this again opening the two windows and making sure to reload the right-hand browser so that the project will be updated with our latest changes and oh wow do you see what i am seeing we did it finally after all these episodes we have got smooth player movement over scratches cloud variables compare this to the movement we had just four episodes ago and well we're simply worlds apart now i want to invite some scratch friends online to test this new update with me but to be honest four players is not going to be enough let's quickly up the maximum player count to eight to do this click on the player sprite and set the max players to eight at the start here then under the set cloud custom block we're going to extend the if clause to include players five to eight as follows now create four new variables ensuring you mark them all as cloud variables and name them p5 p6 p7 and p8 all as cloud variables update the cloud block to use each variable against its corresponding player number now switch to the opponent sprite and we'll extend the value equals cloud custom block to also declare player 5 through 8. ensure your scripts look just like mine this amount of nested ifs could start to get a little tricky to put together so do be careful excellent now it's time to play i've just sent out a request to some of my friends so i'll wait and see if they join now aha and here we go some people are joining this is great they're just moving around and watching see how smooth they are and what they're up to yeah look at this they're all moving around really nicely some people aren't moving their mouse obviously but that's not the fault of the project here we go boinging around really good yep this is working great and it works fine with with lots more players and we play as we got here is it six seven i'm not sure right there you have it a smooth cloud experience with auto joining auto disconnecting and finally smooth player movement via this new buffered transmission stream where we go from here is up to you i do expect to do at least one more in the series where i shall show you how to incorporate these scripts into say a platformer just in case anyone's having trouble making the leap between the tutorials i provide so thanks for watching please like the video remember to subscribe to my channel to catch future videos and leave a comment say hi and share your ideas and any projects you've been able to create following these tutorials until next time then scratch on
Original Description
This is part 5 of how to create a multiplayer cloud game in Scratch 3. In this episode we use a buffered data stream to send player data between web browsers to smooth the movement, achieving full 30fps movement for all players! We also up the maximum player limit to 8 just for fun :)
If it helps, you can remix the completed part 4 project here:
https://scratch.mit.edu/projects/443168164/
🚀 *Boost Your Creativity with Griffpatch*
The Griffpatch Academy will take you from "Gamer to Game Creator"
Learn more at 👉 https://griffpatch.academy 👈
😺 Scratch was developed by the Lifelong Kindergarten Group at the MIT Media Lab. See http://scratch.mit.edu
Part 1 - The Basics - https://www.youtube.com/watch?v=-KoHaz7ZPvo
Part 2 - Encoding and Decoding - https://www.youtube.com/watch?v=nNv9iehpGRQ
Part 3 - Multiplayer - https://youtu.be/4acaa5y_jSU
Part 4 - Auto Game Joining - https://youtu.be/kzp-xr2f3Kw
Part 5 - Smooth Movement - https://youtu.be/2oNTn9ijWIw
Some multiplayer cloud games I have recreated in scratch are slither.io, agar.io, diep.io, Terraria, bomber man, and a few off my own including multiplayer platformer fun, and fireflies, and tanks.
Hi, I'm griffpatch from #Scratch (https://scratch.mit.edu/users/griffpatch/). I create and code games in the online Scratch coding website / language often used in schools to teach the basics of learning to code. In this #tutorial series I will be teaching the basics of how to create #multiplayer #games in Scratch using #cloud variables. In this episode we will create a small project that communicates the mouse position from one web browser to another. We also see the immediate limitations of how cloud variables which we will then learn how to overcome in subsequent videos.
Playlist
Uploads from griffpatch · griffpatch · 32 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
▶
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Burrow Blitz - A game by Griffpatch
griffpatch
Burrow Blitz Level 1 Speed Run!
griffpatch
Scratch Messaging Extension (for chrome & firefox)
griffpatch
Scrolling Platformer Tutorial | Part 1 | Get Scrolling
griffpatch
Scrolling Platformer Tutorial | Part 2 | Gravity
griffpatch
Scrolling Platformer Tutorial | Part 3 | Platform Detection
griffpatch
Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
griffpatch
Scrolling Platformer Tutorial | Part 5 | Collectables
griffpatch
Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
griffpatch
Scrolling Platformer Tutorial | Part 7 | Exit Portal
griffpatch
Scratch 3 | Box2d | Physics Extension
griffpatch
Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
griffpatch
Zombie Cube | Speedrun challenge
griffpatch
Scratch 3 | Physics Extension | Version 2
griffpatch
Top Down Scroller | E1 - Scrolling | Scratch Tutorial
griffpatch
Top Down Scroller | Part 2 | Enemy AI
griffpatch
Top Down Scroller | Part 3 | Enemy Clones & List
griffpatch
Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
griffpatch
Top Down Scroller | Part 5 | You've Won!
griffpatch
Cloud Game Tutorial | Part 1 | The Basics
griffpatch
Cloud Game Tutorial | Part 2 | Encoding and Decoding
griffpatch
Scratch 3 Dev Tools Browser Extension by griffpatch
griffpatch
Cloud Game Tutorial | Part 3 | Multiplayer
griffpatch
Scrolling Platformer Tutorial | Part 9 | Momentum
griffpatch
Scrolling Platformer Tutorial | Part 10 | Wall Jumping
griffpatch
Cloud Game Tutorial | Part 4 | Auto Game Joining
griffpatch
Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
griffpatch
Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
griffpatch
Griffpatch's Live Stream
griffpatch
MMO Platformer Speedrun Challenge
griffpatch
Appel Platformer | Scratch Game Blog
griffpatch
Cloud Game Tutorial | Part 5 | Smooth Movement
griffpatch
How to Design a Level for Appel
griffpatch
Fastest Appel Speedruns | Jan 2021
griffpatch
Tile Scrolling Platformer (Mario) | 1. Setup
griffpatch
Tile Scrolling Platformer | 2. Grid List
griffpatch
Tile Scrolling Platformer | 3. Tile Collisions
griffpatch
Tile Scrolling Platformer | 4. Platforming Scripts
griffpatch
Tile Scrolling Platformer | 5. The Level Editor
griffpatch
Tile Scrolling Platformer | 6. Level Codes
griffpatch
Tile Scrolling Platformer | 7. Drop Through Platforms
griffpatch
How to make Physics in Scratch | Full Tutorial
griffpatch
Tile Scrolling Platformer | 8. Enemy Clones
griffpatch
Simple Pathfinding Tutorial
griffpatch
Tile Scrolling Platformer | 9. Enemies in Level Editor
griffpatch
Simple Maze Generation | Scratch Tutorial
griffpatch
Tile Scrolling Platformer | 10. Mystery Blocks
griffpatch
Simple Car Steering Simulation | Scratch Tutorial
griffpatch
Simple Background Scrolling Tutorial
griffpatch
Tile Scrolling Platformer | 11. Death, Background & Sound
griffpatch
Mid Week Update & Channel Membership
griffpatch
Simple Grid List Tutorial with Image Scanning
griffpatch
Tile Scrolling Platformer | 12. Auto Arranging Tiles
griffpatch
Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial
griffpatch
Space Shooter 2 - Enemy Collisions
griffpatch
Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
griffpatch
Space Shooter 3 - Camera Shake & FX
griffpatch
Tile Scrolling Platformer | 14. Sloping Tiles
griffpatch
Pathfinding a Maze using Lists
griffpatch
Tile Scrolling Platformer | 15. Crouch & Slide
griffpatch
More on: AI Productivity Tools
View skill →Related Reads
📰
📰
📰
📰
Ten files is not a budget
Dev.to AI
Inteligencia artificial para empresas: cómo está cambiando la forma de trabajar
Medium · SEO
SmartKirana: Building an AI-Powered Quick-Pickup and Inventory System for Neighborhood Stores
Medium · Machine Learning
Top 5 Custom AI Development Agencies in the US (2026)
Medium · SEO
🎓
Tutor Explanation
DeepCamp AI