Math

Tuesday, February 12, 2013

Problem-Filled Processing Code

      This post is contains code in Processing that I have been working on at home. I am trying to make a Block Breaker game, (move the paddle, bounce the ball off it, break the blocks) and am working on the mechanism for the block disappearing. If you know how to fix it, you can comment in the comment box. I am halfway through this code, so there might be snippets of it that don't do anything. I don't have the code commented, but I'll try to comment on it soon. I can make them disappear, but I need the ball to not bounce into the blocks once their gone, which is hard. Here's the code:


float paddleX;
float paddleY;
float ballX, ballY;
int blocksBroken;
int alive?

Block block1;
Paddle plat;
Ball ball;

void setup(){
  size(600,600);
  block1 = new Block(0,0);
  plat = new Paddle();
  ball = new Ball();
  background(255);
}

void draw(){
  background(255);
  block1.display();
  block1.die();
  ball.ballReset();
  plat.display();
  ball.display();
  ball.move();
}

void keyPressed(){
    plat.move();
    //ball.paddleRadar();
}


class Ball {
  float xSpeed, ySpeed, ballsRandomX, ballsRandomY;


  Ball(){
    ballX = width/2;
    ballY = paddleY - 50;
    //ballsRandomX = random(0,8);
    //ballsRandomY = random(-8,0);
    xSpeed = -5;//ballsRandomX;
    ySpeed = -5;//ballsRandomY;
  }

  void display(){
    ellipse(ballX,ballY,30,30);
    println(blocksBroken);
  }

  void move(){
    ballX = ballX + xSpeed;
    ballY = ballY + ySpeed;
    if ((ballX > width) || (ballX < 0)){
      xSpeed = xSpeed * -1;
    }
    if ((ballY > height) || (ballY < 0)){
      ySpeed = ySpeed * -1;
    }
    if (((dist(ballX,ballY,paddleX,paddleY) == paddleX - ballX) && (dist(ballX,ballY,paddleX,paddleY) < 50)) || ((dist(ballX,ballY,paddleX,paddleY) == ballX - paddleX) && (dist(ballX,ballY,paddleX,paddleY) < 50))){
      ySpeed = ySpeed * -1;
    }
  }

  void ballReset(){
    if(ballY > height){
      fill(0,255,0);
      ballX = paddleX;
      ballY = paddleY - 50;
      xSpeed = 5;//ballsRandomX;
      ySpeed = height/100 * -1;//ballsRandomY;
    }
  }
}


class Block{
  int blockX;
  int blockY;


  Block(int tempBlockX, int tempBlockY, string temp){
    blockX = tempBlockX;
    blockY = tempBlockY;
  }

  void display() {
    rectMode(CORNER);
    rect(blockX,blockY,width/25,width/25);
    rectMode(CENTER);
  }

  void die() {
    if(dist(blockX,blockY,ballX,ballY) < width/50 + 15) {
      int
    }
  }
}


class Paddle {

  Paddle(){
    paddleX = width/2;
    paddleY = height - 25;
  }

  void display() {
    rectMode(CENTER);
    rect(paddleX, paddleY, 100, 8);
    fill(170, 0, 250);
  }

  void move() {
 
    if (key == 'a') {
      paddleX = paddleX - width/12;
    }
 
    if (key == 'd') {
      paddleX = paddleX + width/12;
    }
  }
}



3 comments:

  1. wow! this is quite arcane! Looks like a good language!

    ReplyDelete
  2. Max -

    Give it a try here:
    http://www.dotline.com/processing/paddle01/web-export/index.html

    It seems to work better in Firefox than in MS IE.
    We can talk through the code if you want & we should figure out how to...
    See each other's code...
    Work on a shared project (version control, etc.)
    Deploy to web in 'test' mode
    & in 'public' mode.
    My next coding items would be:
    Make spirograph.
    Optimize development cycle.

    Best,

    Uncle-Ted

    ReplyDelete
  3. Max - Try this one too:
    http://www.dotline.com/processing/spiro/web-export/index.html
    (notice that it is 'published' to the web. You should find a place where you can publish yours)

    (? how do I turn this portion of my content into a hyperlink?)

    ReplyDelete