xxxxxxxxxx
78
// circle
let dropX, dropY;
let diam = 20;
let speedDrop = 5;
// line
let ang = 0;
let lineX = 0;
let lineY = 0;
// forces
const gravity = 1;
const jump = 20;
let pullY;
function setup() {
createCanvas(600, 250);
dropX = width / 2;
dropY = height - diam / 2;
pullY = 0;
}
function draw() {
background(0, 50);
dropY += pullY;
stroke(0);
strokeWeight(1);
circle(dropX, dropY, diam);
translate(dropX, height - diam / 2);
rotate(ang);
stroke("hotpink");
strokeWeight(5);
line(lineX, lineY, lineX + 5, lineY + 5);
line(lineX, lineY, lineX - 5, lineY - 5);
if (keyIsPressed) {
switchSides();
moveLeftRight();
}
// if jumping, pull down
if (dropY < height - diam / 2) {
pullY += gravity;
} else {
pullY = 0;
dropY = height - diam / 2;
}
}
function keyPressed() {
if (keyCode === 32) {
if (dropY >= height - diam / 2) {
pullY = -jump;
}
}
}
function switchSides() {
if (dropX < 0) {
dropX = width - diam;
}
if (dropX > width) {
dropX = diam;
}
}
function moveLeftRight(){
if (keyCode === LEFT_ARROW) {
dropX -= speedDrop;
ang -= 0.3;
}
if (keyCode === RIGHT_ARROW) {
dropX += speedDrop;
ang += 0.3;
}
}