xxxxxxxxxx
73
let score = 0;
let falling = false
let ball
let hole
let gravity = 0.3
function setup() {
createCanvas(500, 400);
cursor("GRAB")
noStroke()
hole = {x: width/2, y: height-50, width: 60}
}
function draw() {
background("cyan");
drawScore();
drawGround()
drawBall()
if (frameCount % 200 === 0)
moveHole()
}
function mousePressed() {
if (!falling) {
falling = true
ball = {x: mouseX, y: mouseY, speed: 0}
}
}
function drawScore() {
textSize(30)
text(`Score: ${score}`, 320, 50)
}
function drawGround() {
push()
fill('lightgreen')
rect(0, height-100, width, 100)
fill('black')
ellipse(hole.x, hole.y, hole.width, 25)
pop()
}
function moveHole() {
hole.x = random(hole.width, width-hole.width)
}
function ballInHole() {
return ball.y > hole.y &&
abs(ball.x - hole.x) <= hole.width / 2;
}
function drawBall() {
push()
fill('blue')
if (!falling) {
ball = {x: mouseX, y: mouseY}
} else {
ball.y += ball.speed
ball.speed += gravity
if (ballInHole()) {
score += 1
falling = false
ball.y = hole.y
} else if (ball.y > height) {
falling = false
}
}
circle(ball.x, ball.y, 20)
pop()
}