Skip to content

Instantly share code, notes, and snippets.

@mrzachxu
Created October 29, 2018 22:17
Show Gist options
  • Save mrzachxu/7d883e1723b0ba96d207d37d546c64c5 to your computer and use it in GitHub Desktop.
Save mrzachxu/7d883e1723b0ba96d207d37d546c64c5 to your computer and use it in GitHub Desktop.
//create an empty array called balls
let balls = [];
function setup() {
createCanvas(800, 400);
}
function draw(){
background(220);
// draw all the balls in that array
for (let i = 0; i < balls.length; i++) {
balls[i].drawBall();
balls[i].moveBall();
}
}
function keyPressed(){ //every time you push a key, make a new ball from the ball class and add it to the balls array
let b = new Ball(0, 100);
balls.push(b);
console.log(balls);
}
//ball class from which to create new balls with similar properties.
class Ball {
constructor(x,y){ //every ball needs an x value and a y value
this.x = 700;
this.y = 30;
}
drawBall(){ // draw a ball on the screen at x,y
stroke(0);
fill("red");
ellipse(this.x,this.y,10,80);
}
moveBall(){ //update the location of the ball, so it moves across the screen
this.x = this.x-2;
this.y = this.y-.5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment