Using P5js to design a sustem that would simulate an existing natural events.
This project is a snake game. The body of the snake is randomly colored. The foods will appear randomly and disappear within every 5 seconds.
Use the keypress up down left right to control the direction of the snake movement.
this.eat = function(pos){
var d = dist(this.x, this.y, pos.x, pos.y);
if (d<1){
this.total++;
print("FOOD EATEN");
return true;
}else{
return false;
}
}
After snake eat the food, there will be a prompt of the amount of food that has been eaten.
I first determine the size of the snake's head and then calculate the position of the eyes based on the x and y of the snake's head. This ensures that the eyes follow when the head moves.
created with
Website Builder Software .