xxxxxxxxxx
74
const groundHeight = 100
const ball = {x: 100, y: 100, falling: false,
speed: 0}
let hole
let score = 0
function setup() {
createCanvas(550, 350);
noStroke()
cursor('GRAB')
hole = {x: width/2, y: height-groundHeight/2,
width: 60, height: 30}
}
function draw() {
background("cyan");
drawGround()
drawBall()
drawScore()
}
function drawGround() {
fill('lightgreen')
rect(0, height-groundHeight, width, groundHeight)
fill('black')
if (frameCount % 200 === 0) {
hole.x = random(0, width)
}
ellipse(hole.x, hole.y, hole.width, hole.height)
}
function drawBall() {
fill('darkgreen')
if (ball.falling) {
adjustIfAtGround()
ball.y += ball.speed
ball.speed += 0.3
}
else {
ball.x = mouseX
ball.y = mouseY
}
circle(ball.x, ball.y, 25)
}
function mousePressed() {
if (!ball.falling) {
ball.falling = true
}
}
function inHole() {
return ball.x > hole.x - hole.width/2 &&
ball.x < hole.x + hole.width/2
}
function adjustIfAtGround() {
if (ball.y > hole.y) {
if (inHole()) {
score += 1
}
ball.falling = false
ball.speed = 0
}
}
function drawScore() {
fill('black')
textSize(28)
text(`Score: ${score}`, width-180, 50)
}