xxxxxxxxxx
69
let ufo = {x: 50, y: 100, beamOn: false}
let ball = {
x: 200,
y: 350,
size: 15,
moving: false,
captured: false
}
function setup() {
createCanvas(600, 400);
noStroke()
}
function draw() {
background("cyan");
drawGrass()
drawSun()
drawUfo()
drawBall()
}
function drawGrass() {
fill("forestgreen")
rect(0, 300, width, height-300)
}
function drawSun() {
fill("gold")
circle(width, 0, 250)
}
function drawUfo() {
if (ufo.beamOn) {
fill("white")
triangle(ufo.x, ufo.y, ufo.x-20, 350, ufo.x+20, 350)
}
fill("silver")
ellipse(ufo.x, ufo.y, 80, 30)
circle(ufo.x, ufo.y - 13, 30)
ufo.x += 1
if (ufo.x > width) {
ufo.x = 0
}
if (ufo.beamOn && ufo.x === ball.x && ball.y > ufo.y) {
ball.moving = true
}
if (ufo.y > ball.y) {
ball.captured = true
}
}
function drawBall() {
fill("red")
circle(ball.x, ball.y, ball.size)
if (ball.captured) {
ball.x = ufo.x
} else if (ball.moving) {
ball.x = ufo.x
ball.y -= 1
}
}
function mousePressed() {
ufo.beamOn = !ufo.beamOn
}