# I'm trying to make an Agar.io copy

**URL:** https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327
**Category:** Coding Questions
**Created:** [January 8, 2019, 5:55pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327 "2019-01-08T17:55:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Krokido](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Krokido](https://discourse.processing.org/u/Krokido)
#### Post date: [January 8, 2019, 5:55pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/1 "2019-01-08T17:55:35Z")

</div>

I am new in programming and I wanted to try to make [Agar.io](http://Agar.io) copy, but I do not know how to create food.

[My code](https://pastebin.com/raw/W1N7d2h3)

---

<div class="post-metadata">

### Author: ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)
#### Post date: [January 8, 2019, 6:11pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/2 "2019-01-08T18:11:39Z")

</div>

Problem n 1: You dont n++, and this makes the game draw infinite food(but dont do this, do what I say next)  
Problem n 2, you should store the coordinates of the generated food, or the game will draw random food every cicle. i suggest to initialize an array food with random positions and when one food is eaten, you find the eaten food in the array and replace it with another coordinates, color…

---

<div class="post-metadata">

### Author: ![Krokido](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Krokido](https://discourse.processing.org/u/Krokido)
#### Post date: [January 8, 2019, 6:14pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/3 "2019-01-08T18:14:55Z")

</div>

Thanks, but I do not know how to do it, I tried it and I did not succeed, and when I add n ++ food does not create

---

<div class="post-metadata">

### Author: ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)
#### Post date: [January 8, 2019, 6:15pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/4 "2019-01-08T18:15:57Z")

</div>

when the while() finishes, you set n = 0, but as I said, you dont store any food coordinates, and this makes the food position will change everytime

---

<div class="post-metadata">

### Author: ![Krokido](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Krokido](https://discourse.processing.org/u/Krokido)
#### Post date: [January 8, 2019, 6:17pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/5 "2019-01-08T18:17:49Z")

</div>

Can you help me, I’m new to programming. Please

---

<div class="post-metadata">

### Author: ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)
#### Post date: [January 8, 2019, 6:31pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/6 "2019-01-08T18:31:21Z")

</div>

New to coding 😕  
I cant help much bc I dont use java,  
but take a look to [array](https://www.geeksforgeeks.org/arrays-in-java/)  
and do a while() for the array where you fill it with [PVectors()](https://processing.org/reference/PVector.html)  
in draw you do another while where you draw food and check wth [dist()](https://processing.org/reference/dist_.html) if its eaten. If its eaten, you replace the coordinates with new random ones, if not, you draw the food.  
If you are very new to programming in Java, you should first learn the basis in another IDE, because PDE is not the best way to start learning a new progr language. What you need for a generating food is:  
Working with arrays(Loop through array, accesing with the index,)  
PVector  
when you got confortable with arrays, you can start using a class for food  
I cant give you more help than this  
Good luck

---

<div class="post-metadata">

### Author: ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)
#### Post date: [January 8, 2019, 7:50pm UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/7 "2019-01-08T19:50:35Z")

</div>

```auto
float l1;
float l2;
float n;
float x; 
float y;
float easing = 0.05;
float cr;
float cf;
PVector[] food= new PVector [50];

void setup() {
size(720,720);
noStroke();
n = 0;
cr = 50;
for(int i = 0;i < 50;i++){
  food[i] = new PVector(random(0,width),random(0,height));
  
}

}

void draw() {
  background(255);
  cf = random(16,24);

//------------------------------------------------------
l1 = 0;
l2 = 0;
stroke(169,169,169);
while (l1 < width) {
line(l1,0,l1,height);
l1 = l1 + 30;
}

while (l2 < height) {
line(0,l2,width,l2);
l2 = l2 + 30;
}
//------------------------circle-----------
fill(0,255,0);
float targetX = mouseX;
  float dx = targetX - x;
  x += dx * easing;
  float targetY = mouseY;
  float dy = targetY - y;
  y += dy * easing;
  ellipse(x, y, cr, cr);

//-------------------------------------food-----------
for(int i = 0;i < 50;i++){
  ellipse(food[i].x,food[i].y,10,10);
}

}

```

I just [delay(60000)](https://processing.org/reference/delay_.html) from studying and looked at java arrays and made you this simple example.  
You can add an `int [] r = new [50]` for the radi of every food and do a variable like `array_dim = 50` and replace every fifty with this dim so when you can change this value to add more space in the array  
(You cant modify arrays space while the program is running, except for a special kind of arrays that idk how are they called)  
I suggest you to read about [pointers](https://www.geeksforgeeks.org/is-there-any-concept-of-pointers-in-java/),

---

<div class="post-metadata">

### Author: ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)
#### Post date: [January 9, 2019, 9:37am UTC](https://discourse.processing.org/t/im-trying-to-make-an-agar-io-copy/7327/8 "2019-01-09T09:37:08Z")

</div>

This tutorial might help:

[![](https://img.youtube.com/vi/JXuxYMGe4KI/hqdefault.jpg "Coding Challenge #32.1: Agar.io - Part 1 (Basic Game Mechanics)") ](https://www.youtube.com/watch?v=JXuxYMGe4KI)
