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:
speed
Represents the speed that the car should be. This doesn't really mean it is the current speed of the car. That's because the car takes some time to actually change it's speed (it takes some time to accelerate and decelerate).
Example
function carBrain(car) {
car.speed = 5;
}angle
Represents the angle that the car should be. As the speed, this also doesn't mean it is the current angle of the car. It also may take some time for the car to reach the desired angle.
Example
function carBrain(car) {
car.angle = 10;
}sensors
Represents the sensors of the car, which detect objects close to the car. These values cannot be setted, only read. The value of the sensor will be according to the proximity of other objects. If no other objects are in range, the value will be kept at the sensor's maximum range. Different cars might have different sensors range. Value 0 means an object has touched the car (and you lost). Use the property 'reading' from the sensor to get it's current value. To draw the sensors in the animation (so that you can see where the sensor is in the car), you can highlight sensors by clicking their number in the interface (will be show only while the animation is playing).
Example
function carBrain(car) {
if (car.sensors[1].reading >= 10) {
car.speed = 0;
}
}parkingBreak
When the parking break is on, the car cannot move. This is meant to make clear that you are declaring the car as parked, so that the result can be processed. In other words, you need to activate the parking break to win the challenge (if you don't, it never ends).
Example
function carBrain(car) {
car.parkingBreak = true;
}memory
You can use this object to store values. Those values doesn't change the car behavior in any way. They are meant only for decision making in the developer's code. Declaring variables inside 'carBran' function will not really work, since they will be redeclared every tick of the animation. Instead, use this to store values.
Example
function carBrain(car) {
car.memory.rememberThis = 'yeah';
}
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 =)