## Writing Games with Processing: Winning

21. December, 2013

Now that the player can lose the game, he needs a way to win. One idea we had was that crocodiles are greedy: When they run into each other hunting Platty, they start to fight over the food.

To achieve this, we need to add more checks to checkCollisions():

```void checkCollisions() {
ArrayList<Enemy> enemiesToCheck = new ArrayList<Enemy>();

for(Enemy e: enemies) {
if(e.closeTo(px, py)) {
gameOver(e);
return;
}

for(Enemy e2: enemiesToCheck) {
if(e.closeTo(e2.x, e2.y)) {
e.fighting = true;
e2.fighting = true;
}
}

}

int notFighting = 0;
for(Enemy e: enemies) {
if(!e.fighting) {
notFighting ++;
}
}

if(notFighting == 0) {
youWon();
}
}
```

As you can see, I added an inner loop to check if the current enemy is close to any other enemy (except itself, that’s why there is a second list).

I also count how many enemies are currently not in a fight.

At the end, if all enemies are fighting with each other, Platty has won:

```void youWon() {
draw();
noLoop();

textAlign(CENTER, TOP);
textSize(40);
color outline = color(255,255,255);
color fill = color(255,0,0);
textWithOutline("YOU WON!", width/2, 200, outline, fill);

textSize(20);
textWithOutline("The cute platypus outsmarted "+enemies.size()+" crocodiles!", width/2, 240, outline, fill);
}
```

Now just add a boolean to Enemy:

```  boolean fighting;
```

Let’s see how that works:

(Click image to start the animation)

What’s left? Scoring!

Previous: Eating Platty
First post: Getting Started