Skip to content

Instantly share code, notes, and snippets.

@mrzachxu
Created December 11, 2018 21:51
Show Gist options
  • Save mrzachxu/106ebb9332bed07709a3be3c9825e026 to your computer and use it in GitHub Desktop.
Save mrzachxu/106ebb9332bed07709a3be3c9825e026 to your computer and use it in GitHub Desktop.
let me;
let you;
let gravity;
let rectarray = [];
let caught = false;
function setup() {
frameRate(50);
createCanvas(1500,800)
rectMode(CENTER)
me = new Rectangles
you = new Ball(height/2,20,3)
}
function draw() {
background(0,0,0)
border()
you.moveBall();
you.getCaught();
you.drawBall();
//print(caught);
if (frameCount % 30 == 0) {
let r = new Rectangles(random(0,width), height-30, 500, -4);
rectarray.push(r);
//print(rectarray);
}
for (let i = 0; i < rectarray.length; i++) {
rectarray[i].drawRect();
rectarray[i].moveRect();
}
}
class Rectangles{
constructor(x,y, width, speed){
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
}
drawRect(){
stroke(0);
fill("white");
rect(this.x,this.y,500,10);
rect(this.x+600, this.y, 500,10)
rect(this.x-600, this.y, 500,10)
rect(this.x-1200, this.y, 500,10)
}
moveRect(){
this.x;
this.y = this.y-4;
}
}
class Ball{
constructor(x,y, speed){
this.x = x;
this.y = y;
this.speed = speed;
}
drawBall(){
stroke(0);
fill("yellow");
ellipse(this.x,this.y,30,30);
}
moveBall(){
for (let i = 0; i < rectarray.length; i++) {
if (keyIsDown(65)){
this.x -= 3;
}
if (keyIsDown(68)){
this.x += 3;
}
if (this.x>0 && caught == false){
this.y= this.y+.5;
}
}
}
getCaught(){
for (let i = 0; i < rectarray.length; i++) {
if (this.x >= rectarray[i].x-250 && this.x <= rectarray[i].x+ 250 && this.y >= rectarray[i].y-10 && this.y <=rectarray[i].y){
caught = true;
this.y = rectarray[i].y-10;
print("hit rectangle "+[i]+" at y value "+rectarray[i].y);
}
else {
caught = false;
}
}
}
}
function border(){
fill(255,255,255)
rect(0,0,3000,10)
rect(0,800,3000,10)
rect(0,0,10,3000)
rect(1500,0,10,2000)
}
@lizzybrooks
Copy link

lizzybrooks commented Dec 12, 2018

let me;
let you;
let gravity;

let rectarray = [];

let caught = false;
let currentRectangle;


function setup() {
  frameRate(50);
  createCanvas(700,800)
  rectMode(CENTER)

//  me = new Rectangles
  you = new Ball(height/2,20,3)
}

function draw() {
  background(0,0,0);
  border();



//print(caught);

  if (frameCount % 30 == 0) {
      let  r = new Rectangles(random(0,width), height-30, 500, -4);
      rectarray.push(r);
      //print(rectarray);
    }

  for (let i = 0; i < rectarray.length; i++) {
  	 	rectarray[i].drawRect();
      rectarray[i].moveRect();
   }

   you.moveBall();
   you.getCaught();
   you.drawBall();

}

class Rectangles{

  constructor(x,y, width, speed){
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.width = width;
      }

	drawRect(){
    		stroke(0);
    		fill("white");
		    rect(this.x,this.y,500,10);
        // rect(this.x+600, this.y, 500,10)
        // rect(this.x-600, this.y, 500,10)
        // rect(this.x-1200, this.y, 500,10)
      }

  moveRect(){
    this.x;
    this.y = this.y-4;
  }
}


class Ball{

  constructor(x,y, speed){
      this.x = x;
      this.y = y;
      this.speed = speed;
    }

  drawBall(){
      stroke(0);
      fill("yellow");
      ellipse(this.x,this.y,30,30);
    }

  moveBall(){
    print(caught);
     for (let i = 0; i < rectarray.length; i++) {

        if (keyIsDown(65)){
          this.x -= 3;
        }

        if (keyIsDown(68)){
          this.x += 3;
        }

        if (caught == false){
          this.y= this.y+.5;
        }



      }
    }

    getCaught(){
        for (let i = 0; i < rectarray.length; i++) {
          if (this.x >= rectarray[i].x-250 && this.x <= rectarray[i].x+	250 && this.y >= rectarray[i].y-10 && this.y <=rectarray[i].y+20 && caught ==false){
            caught = true;
            currentRectangle = i;
            this.y = rectarray[currentRectangle].y-5;
            print("hit rectangle "+[i]+" at y value "+rectarray[i].y+" at "+frameCount);


          }
          else {
            caught = false;
          }

        }
    }
}

function border(){
  fill(255,255,255)
  rect(0,0,3000,10)
  rect(0,800,3000,10)
  rect(0,0,10,3000)
  rect(1500,0,10,2000)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment