In this short post I will show you how to make Agario on Scratch.
In this game, players control the main cell, and the goal is to gain as much mass as possible by eating smaller cells. But you have to be careful to avoid the bigger ones. It is praised by its simplicity in mechanics and the game objective. It is also very competitive. The way your control your cell is depending on where your mount pointer is, that is the direction you will go. The further away you are with it, the faster you will go. The closer you are with the mouse pointer, the slower it will go. Lets build Agario on Scratch.
Start by opening a new Scratch project.. Start by deleting the main sprite and upload a new sprite, which I have provided in this article below. (Right click on it and save it on your computer).
It is the grid of the game, that will act as a background. It is not the best looking one, but it will do for this game. The way you make the background really large, there is a special little trick I will show. First, go to the costumes for that sprite and make a new one, that is blank. Put that one before the graph costume. Then go to the Scripts tab, and add one ”when green flag clicked” block. Then add one ”switch costume to” Looks block under it, and choose ”costume 1” in the drop-down menu. Then add one ”go to x y” Motion block, and set it to 0 for x coordinates and 0 for y coordinates. That will put it right in the middle. Now, you want to change the size of it, but adding one ”set size to” Looks block, and set it to 300%. Then you have to switch the costume to the grid costume. To do so, add one ”switch costume to” block and in the drop-down menu choose the grid. This script makes the background really big.
Adding the grid.
Now I am going to paint the cell sprite for the game. Click on ”paint new sprite” icon, and draw a ball, by using the ellipse tool. Hold shift while drawing to make a circle. Draw in the center of the canvas, or use the ”set costumes center” tool, to mark the center. Name it ”cell”. This is not necessary, but it will make me easier to refer to the right sprite throughout this article. Now, to make the ball appear to be moving, I am going to make the graph move. This means, that the cell is going to remain in center at all times. Add another ”when green flag clicked” block, and add one ”forever” loop under it. And what the ”forever” loop is going to do is say: change the x position by, wherever the mouse pointer is. The way I am going to do that is add one ”change x by” Motion block inside it. If you run the script now, the grid will slide really fast, and that is now what you want. What I want is ”change x by” by position of the mouse pointer. From the Sensing category, add one ‘mouse x’ block inside the ”change x by” block. This block reports the x position of mouse pointer. The value of the ”mouse x” is way too big, when you move it to far left or far right, it will make the graph move way too fast. To slow it down, I am going to multiply it by a smaller number. Go to the Operators category, pick the ”multiplication” block and drag it to the script area. Place the ”mouse x” block inside it and multiply it by -0.01. Now it is moving really slow, and for now, you can only go side to side. To enable up and down movement, I have to recreate the previous block, but for y axis only. Add ”change y by” and ”mouse y” blocks. Run the program, and it should look more like the Agario game. The further away the mouse, the faster it moves now. We are progressing very fast in this how to make Agario on Scratch.
Food spawning code.
Next I am going to make those smaller cell, which the main cell is going to eat in order to grow bigger. Paint a new, small circle in different color. Make sure that it is centered. Name this one ”food”. Now I am going to give it some code, and I am going to say when the game starts, forever make a clone of its self. That is going to make too many clones, therefore I will have to slow down the clone production a little bit. Add one ”when green flag clicked” block. Under it add one ”forever” block and one ”create clone of food’ block. Then add one ”wait 3 seconds” block, to slow it down. Then add the ”when I start as a clone”, remember that clones to listen to this block only. I am going to make the clone food spawn in random positions on the stage. Add one ”go to x y” Motion block, and I am going to use the random number generated ”pick random” block, to put them in random positions. Set the random number spawn range from -240 to 240 for x coordinates, and duplicate this Operators block and place it for y coordinates as well. Run the program and see if it works. Every 2.5 seconds you should see a new food particle to appear. Notice how they are not moving. We need them to make them move exactly as the background is. I am going to copy the ”forever change x and y” script to the second sprite. Place it under the ”when I start as a clone” script. Change the multipliers to -0.015 for better gaming experience. The clones are now moving with the background. You will notice that the food is following the cell, because it does not execute the script that is made for clones. To make it act like the rest of the food particles, simply add the ”hide” block from the looks category, and place it under the ”when green flag clicked” block. And make sure that you add the ”show” block under the ”when I start as a clone” Operators block.
Next I am going to make code that will make the main cell eat the smaller ones. Actually, what will happen is that the smaller cells will be deleted, when I touch them. Add one ”delete this clone” block for the food sprite. Place it inside an ”if then” statement block. The statement will be that, if It touches the main cell sprite, it will disappear. Go to Sensing category, and add one ”touching” bock inside the statement window, and in the drop-down menu choose ”Cell”. Put it inside the ”forever” loop. Now the food will disappear when I touch them, appearing to be eaten. To make the food appear in different colors, add the ”change color effect” Looks block inside the main ”forever” block for the food sprite. Now I will make the cell grow bigger when it consumes food. Click on the character, and add one ”when green flag clicked” Event block. Under it, add one ”set size to 50%” block. This will make the cell start at its normal size. Now, add one ”forever” block, and inside it add the ”if then” statement. Inside the conditional window add the ‘touching food” block. And when the cell touches the food, we want it to grow bigger. To do so, add one ”change size by 10” Looks block. As I run the program, I noticed a big problem. As soon as I touch the food sprite, the program deletes it, and it does not make the cell grow bigger. To fix this, click on the food sprite, and add one ”wait 0.01 sec” block above the ”delete this clone’ block
I hope you enjoyed the how to make agar.io on Scratch.
I recommend you taking this course to learn programming basics. It will help you understand such concepts as variables, coordinates, scripting, objects, loops, conditional statements, Boolean, lists and much more! It will get you started to program your dream projects! I offer you a 25% discount price in this link: https://www.udemy.com/scratch-programming/?couponCode=SPECIAL