xxxxxxxxxx
67
// A simple mouse-based bouncing ball game
//
// Versions:
// - This version: https://editor.p5js.org/jonfroehlich/sketches/04sI6mCwQ
// - Version 2 (changes dir/speed after scoring): https://editor.p5js.org/jonfroehlich/sketches/TPN7xA0WD
// - Version 3 (has stages, gets faster): https://editor.p5js.org/jonfroehlich/sketches/Cs1lWc1-_
// - Version 4 (has timing): https://editor.p5js.org/jonfroehlich/sketches/ZtbrCLdhw
//
// By Jon Froehlich
// https://makeabilitylab.cs.washington.edu/
//
let x;
let y;
let xSpeed;
let ySpeed;
let diameter = 40;
let score = 0;
function setup() {
createCanvas(400, 400);
x = width / 2;
y = height / 2;
xSpeed = random(1, 3); // random x speed
ySpeed = random(1, 2); // random y speed
}
function draw() {
background(220);
x += xSpeed;
y += ySpeed;
fill(200, 0, 0);
noStroke();
ellipse(x, y, diameter);
// check to make sure it doesn't go offscreen
let radius = diameter / 2;
if(x - radius < 0 || x + radius > width){
xSpeed = xSpeed * -1; // reverse x direction
}
if(y - radius < 0 || y + radius > height){
ySpeed = ySpeed * -1; // reverse y direction
}
textSize(20);
fill("black");
text("Score: " + score, 10, 25);
}
function mousePressed(event){
let distMouseAndCircleCenter = dist(mouseX, mouseY, x, y);
if(distMouseAndCircleCenter <= diameter / 2){
score++;
}
}
function keyPressed() {
if(key == ' '){
print("Reseting the game!")
score = 0;
}
}