Skip to content
Advertisement

How do I make it so that if a turtle is close to a turtle, it will increase a variable?

I am trying to make a more interactive game, basically, I want to make it so that if a turtle is close to another one, it will increase a variable(in this case, the variable is score).

The code is over here

JavaScript

Does anyone know how to make it so that if the turtle is distanced close to it, it will do what make the other one go to a random position and increase the score?

Advertisement

Answer

Your collision detection:

JavaScript

is correct, it’s the rest of your code that has issues. For example: score + 1 in isolation does nothing; fruit.goto(x,y) only makes sense if you’re changing x and y each time, not just once before the loop; your code is structured such that it locks out input events. Below is a simplified, working version of your code that you can build on:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement