Park it Documentation

General overview

In the code editor you must implement everything inside one method (by default called "carBrain") that will receive one parameter, which is the player's car (so, by default, called "car"). This parameter represents the car that should have it's attributes changed to actually change the car in the game. So, for example, just by adding the line car.speed = 5; inside the function, which would result in:

function carBrain(car) {
car.speed = 5;
}

The car would move forward with a speed of 5. You can also use other properties, like reading informations from the sensors. Let's say you want to speed up and stop if something comes up in front of the car, with a proximity of less than 50 (in this case, the distance is presented in pixels). You could use the sensor 1 (there are 20). Your code would look like:

function carBrain(car) {
car.speed = 5;

if (car.sensors[1].reading > 50) {
car.speed = 0;
}
}

Available properties

The parameter "car" that is passed to the function has the following available properties:

Small example

Let's use what we learned above to create a simple example code. Imagine you want the car to accelerate and get close to the other cars that are parked. You could use this code:

function carBrain(car) {
car.speed = 10;
car.angle = 5;
}

With actually only this code, the car will crash, since nothing is there to make it go straight again. Let's add the instruction to make it go straight after getting close enough:

if (car.sensors[3].reading >= 30) {
car.angle = 0;
}
}

Almost works, but there's still a problem. When the sensor reaches 30, it tries to make the car go straight again. This moves the front of the car away from the objects, causing also the sensor 3 to move away from the object, which makes the reading go above 30 again. Then the car starts going to angle 5 again, until sensor 3 reaches 30 and keep repeating this over and over.

So you actually need to keep stored the information that you are already close to the other cars. Let's use the car's memory for that:

function carBrain(car) {
car.speed = 10;

if (!car.memory.closeToOtherCars) {
car.angle = 5;
}

if (car.sensors[3].reading >= 30) {
car.angle = 0;
car.memory.closeToOtherCars = true;
}
}

Remember that you need to keep informations stored in the memory, not in variables declared inside the function itself. Variables declared inside the 'carBrain' will actually lose their value at the end of each tick of the animation (there are more than 20 ticks each second).

With this code you should be able to achieve the desired result! That's it. You are now ready to get there and park that car! Good luck =)