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 › a slew of questions
Page Index Toggle Pages: 1
a slew of questions (Read 613 times)
a slew of questions
Jan 27th, 2010, 9:53pm
 
1. i had code to delete elements from an arraylist when the delete key is pressed, it was working and now it isn't, not sure what i did to change that.

2. my function checkcollision doesn't seems to work. i'm checking based on the distance of the mouse to the blocks on the screen, and below that i'm checking the distance of each point in each block and comparing it to the mouse's position.

thats all i can think of right now my code is below any and all help would be greatly appreciated.

Code:
ArrayList blocks;
Paddle paddle = new Paddle();
PFont font;
String level = "";
int levelnum = 0;
int runonce = 0;
int runonce2 = 0;
int rungame = 0;

void setup(){
 size(960,600);
 font = loadFont("Helvetica-48.vlw");


}

void draw(){
 background(0);
 if(runonce == 0){
   levelselect();

 }
 if(rungame != 0){
   levelexecute(levelnum);
 }

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

 if(checkcollision(mouseX, mouseY) == true){
   println("Success!");
 }


 if(checkcomplete() == true){
   delay(1000);
   levelnum ++;
 }

}

void keyPressed(){

 if((runonce == 0) && (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 == '\n'){
   runonce++;
   rungame++;

 }
 if(key == TAB){
   runonce = 0;
   runonce2 = 0;
   rungame = 0;
   level = "";
 }
 if(key == DELETE){
   int i = 0;
   blocks.remove(i);
   if(i < blocks.size()){
     i++;
   }
 }
}

Code:
class Ball{
 float ball_x;
 float ball_y;
 float ball_r;
 
 Ball(float x, float y, float r) {
   this.ball_x = x;
   this.ball_y = y;
   this.ball_r = r;
 }
}

Code:
class Block{
 float posx;
 float posy;

 Block(float x, float y){
   posx = x;
   posy = y;
 }

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

   rect(posx,posy, 60,25);
 }
}


Code:
boolean checkcollision(float px, float py){
 boolean check = false;
 ArrayList blockgrid = new ArrayList();
 if(runonce != 0){
   for(int i = 0; i < blocks.size(); i++){
     Block tempblock = (Block) blocks.get(i);
     PVector coord = new PVector();

     for(float x = tempblock.posy-12.5; x < tempblock.posy +12.5; x++){
       for(float y  = tempblock.posx - 30; y < tempblock.posx +30; y++){
         coord.x = x;
         coord.y = y;
         blockgrid.add(coord);
       }
     }
     for(int t = 0;t<blockgrid.size(); t++){
       PVector tempvec = (PVector) blockgrid.get(t);
       float temp = dist(px,py,tempvec.x,tempvec.y);
       if( temp< 10){
         check = true;
       }
       else{
         check = false;
       }
     }

   }
 }
 return check;
}

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

Code:
void levelexecute(int levelnum){
 if(runonce2 == 0){
   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));
     }
   }
 }
 for(int i = 0; i < blocks.size(); i++){
   Block tempblock = (Block) blocks.get(i);
   tempblock.display();
 }
 
}

Code:
void levelselect(){
 stroke(0,80,250);
 fill(0,80,250);
 textFont(font);
 textSize(40);
 textMode(SCREEN);
 text("Welcome to Ulitmate Block Breaker!!!", 100, height/2 -50);
 text("Which level do you want to start at?(1-10): " + level, 75, height/2);
}

Code:
class Paddle{
 float paddle_xpos;
 float paddle_ypos;
 int paddle_height;
 int paddle_width;
 
 Paddle(){
   paddle_height = 15;
   paddle_width = 96;
 }
 void display(int xpos_,int ypos_){
   int xpos = xpos_;
   int ypos = ypos_;
   xpos = constrain(mouseX, paddle_width/2, width-paddle_width/2);
   rectMode(CENTER);
   fill(255);
   rect(xpos, ypos,paddle_width, paddle_height);
 }
}
Re: a slew of questions
Reply #1 - Jan 28th, 2010, 1:35am
 
First a suggestion - don't separate your code into separate code blocks unless it's really necessary: we can then paste the whole lot into a single tab and it will work; so save us having to go through and delete the 'Code:' labels Wink

As for the delete key problem: Maybe you're trying it before you instantiate your ArrayList object?  If you hit delete before a level has been selected there's no blocks ArrayList to delete from.  It would be safer to call "blocks = new ArrayList();" in setup, not in levelexecute.  Also to empty an ArrayList you can simply use myArrayList.clear(); you don't have to iterate through it and remove() each item.

Finally I'm not convinced the code you posted is complete: there doesn't seem to be anything to create a ball.  I think it's also worth pointing out that your keyboard interface is far from intuitive: when you ask people to select a level they're going to press a number and expect something to happen.  It turns out you expect them to then hit the return key...  I suspect many people will assume it's broken at that point.
Page Index Toggle Pages: 1