xxxxxxxxxx
91
let playerWeight = 10;
let playerPositionX = 300;
let playerPositionY = 300;
let enemyPositionX = 0;
let enemyPositionY = 0;
let direction = null;
let velocity = 1;
let gameOver = false;
function setup() {
// setup the random initial location of the enemy
enemyPositionX = random(500);
enemyPositionY = random(500);
createCanvas(500, 500);
}
function draw() {
// Game over?
if (gameOver) {
fill(200,50,40);
textSize(20);
text("GAME OVER", 200,250);
return; // just skip everything else
}
// Hit a wall?
if (playerPositionX < 0 || playerPositionX > 500 || playerPositionY < 0 || playerPositionY > 500) {
gameOver = true;
}
// get distance of the two circles using Pythagoras
let distance = sqrt(pow(playerPositionX -enemyPositionX ,2) + pow(playerPositionY - enemyPositionY, 2));
// get the sum of the two radii (note we pass the diameter to the circle function, so we need to half it!)
let radii = 10 + playerWeight / 2;
// check if the distance is smaller than the sum
let collisionDetected = distance <= radii;
if (collisionDetected) {
playerWeight += 2;
velocity += 1;
enemyPositionX = random(500);
enemyPositionY = random(500);
}
// Update the world
if (direction == 'up') {
playerPositionY = playerPositionY - velocity;
}
if (direction == 'left') {
playerPositionX = playerPositionX - velocity;
}
if (direction == 'right') {
playerPositionX = playerPositionX + velocity;
}
if (direction == 'down') {
playerPositionY = playerPositionY + velocity;
}
// draw a black background
background(0);
// define the fill color for the player
fill(255, 120, 90);
// draw a circle (our player)
circle(playerPositionX, playerPositionY, playerWeight);
// define the fill color of the enemy
fill(0, 255, 0);
// draw the enemy as a circle
circle(enemyPositionX, enemyPositionY, 20);
// current score
fill(60,200,120);
textSize(20);
text("your current weight " + playerWeight + " kg",20,20);
}
function keyPressed() {
if (key == 'w') {
direction = 'up';
}
if (key == 'a') {
direction = 'left';
}
if (key == 's') {
direction = 'down';
}
if (key == 'd') {
direction = 'right';
}
}