We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › need help debugging my game
Page Index Toggle Pages: 1
need help debugging my game (Read 612 times)
need help debugging my game
Feb 4th, 2010, 9:05am
 
so here's the deal i'm building a block breaker game and the block collision detection isn't working properly. it seems that the checkcollision function isn't getting the proper x,y loc for the ball because blocks seems to explode when ever the ball passes under them before the ball is even launched. if anyone can take a look and see if they see anything that i dont that would be amazing
Code:
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////         Variables        ///////////////////////////////
ArrayList blocks; //arraylist for th blocks on the screen
Paddle paddle = new Paddle(); //paddle
PFont font;
String level = ""; //user input string for level selection
int levelnum = 0;
boolean runonce = true; // bool to run level selection
boolean runonce2 = true; //bool to add blocks once per map start
boolean rungame = false; //bool to run game
boolean complete = false; // bool to check map completion
ArrayList particles; //arraylist for explosion animation
Ball ball = new Ball(mouseX+48, mouseY, 10); // ball creation
boolean mouseclick = false; //bool to check for mouse clicks... used to launch the ball
PVector vel = new PVector(6, -4); // pvector to move the ball
int check; // used to check which collision occured
int blocknum; //value returned from checkcollision to tell which block was hit

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////           setup          ///////////////////////////////

void setup(){
 size(960,600);
 smooth();
 font = loadFont("Helvetica-48.vlw");
 particles = new ArrayList();
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////            draw          ///////////////////////////////

void draw(){
 background(0);

 ////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////
 ///////////////////////         game intialization        //////////////////////////

 if(runonce == true){
   levelselect();
 }
 if(rungame == true){
   levelexecute(levelnum);
 }


 ////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////
 ////////////////////        paddle/ball intialization        ///////////////////////

 paddle.display(mouseX,constrain(mouseY, 500, height-paddle.ph/2-25));

 fill(255);
 ellipse(ball.bx, ball.by, ball.br*2, ball.br*2);
 if(mousePressed){
   mouseclick = true;
 }
 if(mouseclick == false){
   ball.bx = paddle.px;
   ball.by = paddle.py-17.5;

   if(mouseX < width/2){
     vel.x *= -1;
   }
   vel.y = -4;
 }
 else{
   ball.bx += vel.x;
   ball.by += vel.y;
 }

 ////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////          checks          ///////////////////////////////
 
 //block collision
 if(rungame == true){
   //walls
     if((ball.bx-ball.br) <= 0  || ball.bx+ball.br >= width){
       vel.x *= -1;
     }
     //ceiling
     else if(ball.by-ball.br <= 0){
       vel.y *= -1;
     }
     //paddle
     else if(ball.bx > paddle.px-48 && ball.bx < paddle.px+48 && ball.by+ball.br >= paddle.py-10){
       vel.x *= -1;
     }
   for(int i = 0;i < blocks.size(); i++){
     if(checkcollision(ball.bx, ball.by,i) == true){
       switch(check){
       case 0:
         break;
       case 1: //walls
         vel.x *= -1;
         break;
       case 2:  //ceiling
         vel.y *= -1;
         break;
       case 3:  //left
         vel.x *= -1;
         explode();
         break;
       case 4:  //right
         vel.x *= -1;
         explode();
         break;
       case 5:  //top
         vel.y *= -1;
         explode();
         break;
       case 6:  //bottom
         vel.y *= -1;
         explode();
         break;
       case 7:  //paddle
         vel.y *= -1;
         break;

       }
     }

   }

   //map complete
   if(checkcomplete() == true){

     delay(1000);
     runonce2 = true;
     if(levelnum < 10){
       levelnum ++;
     }
     else{
       complete = true;
     }
   }

   if(complete == true){
     levelnum = -2;
     blocks.clear();
     stroke(0,80,250);
     fill(0,80,250);
     textFont(font);
     textSize(40);
     textMode(SCREEN);
     text("Congratulations! You Win!", 250, height/2);
     text("(Hit 'Tab' to restart)", 350, height/2+50);
   }
 }

 ////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////         animation        ///////////////////////////////

 for (int i = 0; i < particles.size(); i++ )
 {
   Particle p = (Particle) particles.get(i);
   p.display();
   p.burst();
   p.gravity();


   if (p.finished())
   {
     particles.remove(i);
   }
 }


} //end draw
 
Re: need help debugging my game
Reply #1 - Feb 4th, 2010, 9:07am
 
Code:
void keyPressed(){
 if((runonce == true) && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' ||
   key == '7' || key == '8' || key == '9' || key == '0')){
   level = level+key;
   levelnum = int(level);
 }
 if(key == ENTER){
   runonce = false;
   rungame = true;

 }
 if(key == TAB){
   runonce = true;
   runonce2 = true;
   rungame = false;
   complete = false;
   level = "";
   ball.bx = mouseX;
   ball.by = mouseY-17.5;
   mouseclick = false;
 }
 if(rungame ==true && key == BACKSPACE){
   blocknum = 0;
   explode();
 }
}
class Ball{
 float bx;
 float by;
 float br;

 Ball(float x, float y, float r) {
   this.bx = x;
   this.by = y;
   this.br = r;
 }
}
class Block{
 float xpos;
 float ypos;

 Block(float x, float y){
   xpos = x;
   ypos = y;
 }

 void display(){
   strokeWeight(1);
   stroke(0,80,250);
   rectMode(CENTER);
   fill(0,80,250,75);

   rect(xpos,ypos, 60,25);
 }
}
boolean checkcollision(float px, float py, int i){
 boolean result = false;
 PVector coord = new PVector();
 if(rungame == true){
     Block tempblock = (Block) blocks.get(i);
     coord.x = tempblock.xpos;
     coord.y = tempblock.ypos;
     float top = coord.y - 12.5;
     float bottom = coord.y + 12.5;
     float left = coord.x - 30;
     float right = coord.x + 30;
     //walls
     if((ball.bx-ball.br) <= 0  || ball.bx+ball.br >= width){
       result = true;
       check = 1;
     }
     //ceiling
     else if(ball.by-ball.br <= 0){
       result = true;
       check = 2;
     }
     //paddle
     else if(ball.bx > paddle.px-48 && ball.bx < paddle.px+48 && ball.by+ball.br >= paddle.py-10){
       result = true;
       check = 7;
     }    
     //left collision
     if(py > top && py <  bottom && px+ball.br >= left){
       result = true;
       check = 3;
       blocknum = i;
     }
     //right collision
     else if(py > top && py <  bottom && px-ball.br <= right){
       result = true;
       check = 4;
       blocknum = i;
     }
     //top collision
     else if(px > left && px <  right && py+ball.br >= top){
       result = true;
       check = 5;
       blocknum = i;
     }
     //bottom collision
     else if(px > left && px < right && py-ball.br <= bottom){
       result = true;
       check = 6;
       blocknum = i;
     }
     
   

 }
 return result;
}
boolean checkcomplete(){
 if(runonce == true){
   return false;
 }
 else if(blocks.size() <= 0){
   return true;
 }
 else{
   return false;
 }
}

void explode(){
 for(int i = 0; i < 25; i++){
   Block tempblock = (Block) blocks.get(blocknum);
   particles.add(new Particle(tempblock.xpos,tempblock.ypos));
   //particles.add(new Particle(mouseX,mouseY));
 }
 blocks.remove(blocknum);

}

void levelexecute(int levelnum){
 if(runonce2 == true){
   blocks = new ArrayList();
   for(int x = 1;x < 15;x++){
     for(int t = 1; t <= levelnum+2; t++){
       blocks.add(new Block(x*60+30, t*25+30));
     }
   }
   runonce2 = false;
 }
 for(int i = 0; i < blocks.size(); i++){
   Block tempblock = (Block) blocks.get(i);
   tempblock.display();
 }
 
}
void levelselect(){
 stroke(0,80,250);
 fill(0,80,250);
 textFont(font);
 textSize(40);
 textMode(SCREEN);
 text("Welcome to Ulitmate Block Breaker!!!", 125, height/2 -50);
 text("Type which level you want then hit enter(1-10): " + level, 25, height/2);
}
class Paddle{
 float px;
 float py;
 int ph;
 int pw;
 
 Paddle(){
   ph = 15;
   pw = 96;
 }
 void display(int xpos_,int ypos_){
   px = xpos_;
   py = ypos_;
   px = constrain(mouseX, pw/2, width-pw/2);
   rectMode(CENTER);
   fill(255);
   rect(px, py, pw, ph);
 }
}
class Particle{
 float px, py, xspeed, yspeed, life;
 
 Particle(float x_, float y_){
   px = x_;
   py = y_;
   xspeed = random(-2,2);
   yspeed = random(-4,0);
   life = 500;
 }
 
 void burst(){
   px += xspeed;
   py += yspeed;
 }
 
 void gravity(){
   yspeed += .05;
 }
 
 boolean finished(){
   life -= 2.0;
   if (life < 0) return true;
   else return false;
 }

 void stop() {
   xspeed = 0;
   yspeed = 0;
 }
 
 void display(){
   strokeWeight(1);
   stroke(0,80,250);
   ellipseMode(CENTER);
   fill(0,80,250,75);
   ellipse(constrain(px,5,width-5),py,10,10);
 }

}
Re: need help debugging my game
Reply #2 - Feb 6th, 2010, 8:25am
 
can anyone help me?
Re: need help debugging my game
Reply #3 - Feb 6th, 2010, 8:49am
 
The error appears to be in the checkCollision method for example in the following code
Code:
      //top collision
     else if(px > left && px <  right && py+ball.br >= top){
       result = true;
       check = 5;
       blocknum = i;
     }
     //bottom collision
     else if(px > left && px < right && py-ball.br <= bottom){
       result = true;
       check = 6;
       blocknum = i;
     }


In each case the px tests define a column of blocks but the py test will return true for all blocks above (check = 6) or all all blocks below (check = 5)

if you look at the code I think you will find a similar thing happening to the left and right of the ball

Code:
      //left collision
     if(py > top && py <  bottom && px+ball.br >= left){
       result = true;
       check = 3;
       blocknum = i;
     }
     //right collision
     else if(py > top && py <  bottom && px-ball.br <= right){
       result = true;
       check = 4;
       blocknum = i;
     }



Page Index Toggle Pages: 1