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 & HelpSyntax Questions › Code not working Properly
Pages: 1 2 
Code not working Properly (Read 1950 times)
Code not working Properly
Jun 28th, 2009, 5:10pm
 
I had a topic on here earlier, but I solved the problem on that. I'm a beginner at Processing and read most of a book on it and decided to make my own simple program. I decided to make a game where aliens fall down and whenever you press the mouse, "ammo " comes out of the box and hits the alien, which sends it off the screen, having the effect that you defeated it. The problem is that the ammo is not coming out when the mouse is pressed. Heres my code.

TTab 1
Code:

Ammo ammo;
Timer timer;
Alien[] alien;

int totalAlien = 0;
int score=0;


void setup() {
 size(750,750);
 
 alien = new Alien[250];
 ammo = new Ammo(20);
   timer = new Timer(1000);
 timer.start();

}

void draw(){
 background (137,112,112);
 if(timer.isFinished()) {
   alien[totalAlien]= new Alien();
   totalAlien++;
   if(totalAlien >= alien.length) {
totalAlien = 0;
   }
   timer.start();
 
 }
 for(int i=0; i< totalAlien;i++) {
   alien[i].move();
   alien[i].display();
   if(ammo.intersect(alien[i])) {
alien[i].caught();
   }
 }
 stroke(0);
 fill(72,2,3);
 rectMode(CENTER);
 rect(mouseX,width-20,40,40);
 

 if(ammo.fired) {
   ammo.drawAmmo();
   ammo.move();  
 
 }

  text("score:"+score,100,20);  
 




}


void mousePressed() {

//  if (!ammo.fired) {
 ammo.x = mouseX;
 
 // ellipse(mouseX,width-30,40,40);
 ammo.y = height-30;
 ammo.fired = true;
//}

}


Tab 2 Alien

Code:

class Alien  {
 float x,y;
 float speed;
 color c;
 float r;


 Alien() {
   r = 8;
   x = random(width);
   y= -r*4;
   speed = (2);
   c = color(50,100,150);
   

 }


 void move(){
   y += speed;
 }
 boolean reachedBottom() {
 
   if (y > height + r*4) {
return true;
   } else {
return false;
   }
 }


 void display() {
   
  ellipseMode(CENTER);
  rectMode(CENTER);
  fill(255);
  for(int i=2; i<r; i++)
  ellipse(x,y+i*4,40,40);
  fill(0);
  for(int i=2; i<r; i++){
    ellipse(x-19,y+i*4,10,26);
    ellipse(x+19,y+i*4,10,26);
   }
 }


 void caught() {

 speed = 0;
   y = - 10000;

if(ammo.fired){
score++;
}
   
   }
 
}


Tab 3 Ammo

Code:

class Ammo {
 float r;
 color col;
 float x,y;
 float speed;
 
 boolean fired = false;
 
 
 Ammo (float tempR) {
   r = tempR;
   col = color(50,10,10,150);
   x = 0;
   y = 0;
   speed = 23;
 }

void drawAmmo() {
 ellipse(x,y,r,r);
}
 
 
 void move() {
   y-=speed;
 }
 
 boolean intersect(Alien d) {
 float distance = dist(x,y,d.x,d.y);
   
   if (distance < r + d.r) {
return true;
   } else {
return false;
   }
 }
}  


Tab 4 Timer

Code:

class Timer {

 int savedTime;
 int totalTime;
 
 Timer(int tempTotalTime) {
   totalTime = tempTotalTime;
 }
 

 void start() {
 
   savedTime = millis();
 }
 
 
 boolean isFinished() {

   int passedTime = millis()- savedTime;
   if (passedTime > totalTime) {
return true;
   } else {
return false;
   }
 }
}
Re: Code not working Properly
Reply #1 - Jun 28th, 2009, 5:39pm
 
I think you posted the tab 1 code twice. No Alien class.
Re: Code not working Properly
Reply #2 - Jun 28th, 2009, 5:50pm
 
NoahBuddy wrote on Jun 28th, 2009, 5:39pm:
I think you posted the tab 1 code twice. No Alien class.


Thanks for pointing that out! The code has been edited with the alien class!
Re: Code not working Properly
Reply #3 - Jun 28th, 2009, 6:58pm
 
I don't see where you are testing for mouse input. The mousePressed() is only declared inside the Ammo class. This is fine, but it isn't called by anything else.

If mousePressed() is in the main code block (like draw() and setup() are) then it will pick up your mouse events.
Re: Code not working Properly
Reply #4 - Jun 28th, 2009, 7:22pm
 
After I add this to the main code, how do I make it move forward?
Re: Code not working Properly
Reply #5 - Jun 28th, 2009, 11:29pm
 
Not quite code:

1. Mouse click brings ammo to life if not already. Reset ammo position.
2. In draw(), update ammo; if ammo is live, ammo.move().
3. Check if ammo has hit something or left the screen. If it has, ammo is no longer live.

If you have trouble converting that to actual code, I (and I'm certain others) can help. It's just getting late here.
Re: Code not working Properly
Reply #6 - Jun 28th, 2009, 11:45pm
 
Here's some updates that make it work - see comments for explanations Wink

You may then want to remove the ammo from the screen when it hits an alien and perhaps consider allowing the player more than one shot (i.e. create an ammo array), though that may present an interesting challenge in terms of limiting the rate of fire...

Code:
class Ammo {
 float r;
 color col;
 float x,y;
 float speed;
 // Add a variable to store the ammo's fired state
 boolean fired = false;
 
 
 Ammo (float tempR) {
   r = tempR;
   col = color(50,10,10,150);
   x = 0;
   y = 0;
   speed = 8;
 }

// replaced the mousePressed method with one to draw the ammo
// note also that the ellipse properties are now the ammo's properties rather than hard-coded
void drawAmmo() {
 ellipse(x,y,r,r);
}
 
 // Note that to move something up the screen you *subtract* from its y value
 void move() {
   y-=speed;
 }
 
 boolean intersect(Alien d) {
 float distance = dist(x,y,d.x,d.y);
   
   if (distance < r + d.r) {
return true;
   } else {
return false;
   }
 }
}



Code:
void draw(){
 background (137,112,112);
 if(timer.isFinished()) {
   alien[totalAlien]= new Alien();
   totalAlien++;
   if(totalAlien >= alien.length) {
totalAlien = 0;
   }
   timer.start();
 }
 for(int i=0; i< totalAlien;i++) {
   alien[i].move();
   alien[i].display();
   if(ammo.intersect(alien[i])) {
alien[i].caught();
   }
 }
 stroke(0);
 fill(72,2,3);
 rectMode(CENTER);
 rect(mouseX,width-20,50,50);
 
 // only draw and move the ammo if it has been fired
 if(ammo.fired) {
   ammo.drawAmmo();
   ammo.move();  
 }
 
}

// Set the ammo position to that of the ship and register that it has been fired
void mousePressed() {
//  You might want to include this condition, but would need to set fired to false when ammo is removed from screen ;)
//  if (!ammo.fired) {
 ammo.x = mouseX;
 // careful - what would happen if your sketch weren't square?
 // ellipse(mouseX,width-30,40,40);
 ammo.y = height-30;
 ammo.fired = true;
//}
}
Re: Code not working Properly
Reply #7 - Jun 29th, 2009, 4:49pm
 
Wow! Thanks! I tried the code, and it worked perfectly! I then tried to Export and when I opened the html file, the applet wouldn't load. It was just a blank screen that said built with processing and listed the source files.  I have updated the code.

I think I've found the problem.
frame.setTitle("Destroy the Alien!");
When I remove this line of code from my setup, everything works fine in the html file, but when I have it, the applet doesn't work. Why is that?
Re: Code not working Properly
Reply #8 - Jun 29th, 2009, 8:08pm
 
Oh, and with the current code I have, how would I make a scoring system that adds points if you shot an alien, and displays the score in a box? I just need some code to get started... Thanks

Ok, I got the score system up and am updating the old code!
Re: Code not working Properly
Reply #9 - Jun 30th, 2009, 10:24am
 
Is there any way to make the player have lives, and they lose one life everytime an alien goes beyond the bottom of the window? I`m fiddling with some code for this right now.
Ok, I got this in the alien tab.
void move(){
   y += speed;
   if (y<height){
     life-=1;
   }
 }

THis in the main tab.
int life = 10;
and
text("life:"+life,200,50);

My question is why does the counter keep decreasing if just one alien goes beyond the bottom? I have the counter at ten, and after one alien goes down, the counter keeps going down until I quit the program. Why is that?
Re: Code not working Properly
Reply #10 - Jul 1st, 2009, 12:49am
 
Add a check to see if that alien is dead (beyond the screen or shot).
Something like:

if (*alien is at bottom of screen*) {
 if (!alien[i].dead) {
   life-=1;
   alien[i].dead = true;
 }
}

Then when you create the aliens at the top, be sure to reset 'dead'. Since you are using 'new Alien()', a default 'boolean dead = false;' inside the Alien class would work.

As for 'frame.setTitle()', a Frame object is a container to hold the PApplet. Browsers essentially run the applet by itself. I am guessing (without testing) that frame is null when run in the browser, and probably shows some error message in the console.

PS. There is a built in flag called 'online' to know if it is running in a browser.
Re: Code not working Properly
Reply #11 - Jul 4th, 2009, 4:33pm
 
Ok, I have this as my main tab so far.
Code:

Ammo ammo;
Timer timer;
Alien[] alien;

int totalAlien = 0;
int score = 0;
boolean gameplaying = false;
int life = 15;
boolean newgame = true;

void mousePressed() {
 if(!gameplaying) {
   gameplaying = true;
   restart();
 }
 newgame = false;
}

void restart() {
 score = 0;
 life = 15;

 alien = new Alien[250];
 ammo = new Ammo(20);
 timer = new Timer(1000);
 timer.start();
}

void setup() {
 size(750,750);
 textFont(loadFont("AngsanaNew-25.vlw"),14);
restart();
}

void draw(){
 background (137,112,112);
 if(timer.isFinished()) {
   alien[totalAlien]= new Alien();
   totalAlien++;
   if(totalAlien >= alien.length) {
totalAlien = 0;
   }
   timer.start();
 
 }
 for(int i=0; i< totalAlien;i++) {
   alien[i].move();
   alien[i].display();
   if(ammo.intersect(alien[i])) {
alien[i].caught();
   }
 }
 stroke(0);
 fill(72,2,3);
 rectMode(CENTER);
 rect(mouseX,width-20,40,40);
 
PFont metaBold;

metaBold = loadFont("AngsanaNew-25.vlw");
String lines = " To shoot press the space!";
textFont(metaBold);
textMode(SCREEN);
text(lines, 3, 689);

 if(ammo.fired) {
   ammo.drawAmmo();
   ammo.move();  
 
 }

  text("score:"+score,100,20);  
 

 
 

if(gameplaying) {
   for(int i=0; i < height ;i++) {
if (alien[i].reachedBottom()) {
 life--;
}

   }

   if(life<=0) {
gameplaying = false;
   }
 }

 else {
   if(newgame) {
showtitle();
   }
   else{
showgameover();
   }
 }
}

 void keyPressed() {
 if (key == 32) {

   ammo.x = mouseX;

   ammo.y = height-30;
   ammo.fired = true;

 }
}


void showtitle() {
 fill (140);
 text("Destroy Those Aliens!!!",200,80);
 text("Move your cannon left and right using the mouse!",200,120);
 text("Fire by pressing space!",200,160);
 text("Use your ammo to kill those aliens!",200,180);
 text("Click to start",200,260);
}
void showgameover() {
 fill(255);
 text("Destroy Those Aliens",200,80);
 text("Game over",200,120);
 text("Your score is:"+score,200,160);
 text("Click to restart!",200,370);

}



When I run it, the title screen does not show up, and the program says "Cannot find a class or type named "Ammo".
When I remove this from the code
Code:

if(gameplaying) {
 if (alien.y>height)
  life-=1;

if(life<=0) {
 gameplaying = false;
}
}
else {
 if(newgame) {
   showtitle();
 } else{
   showgameover();
 }
}

The code works again, but does not display the title screen, and the score does not deduct properly. Can anyone help?

@NoahBuddy
I tried your code and added this to the alien tab.
Code:

boolean dead() {
   if(y>height){
     life--;
     return true;
 }

}

The program told me that "This method must return a result of type boolean". What can I add to make it work?
Thanks for your time!
Re: Code not working Properly
Reply #12 - Jul 4th, 2009, 5:05pm
 
Since there is an if statement, if the condition is not true, there is no returned value (either true or false).

After the if block, add 'return false;' that way it will always return something.

I'll have to look closer on the title screen...

EDIT: OK, something to get you going on troubleshooting.
The 'if(gameplaying)' block is floating outside of the draw method.

In that same code block, alien.y is accessed, but it should be looking at an array: alien[i].y

Hope that helps.
Re: Code not working Properly
Reply #13 - Jul 4th, 2009, 6:33pm
 
What does it mean when it says "Cannot find a class or type named "Ammo"? I keep getting this error...


Oh, and on this line: if (alien[i].reachedBottom), where am I supposed to declare the variable i and to what number?




Thanks

Re: Code not working Properly
Reply #14 - Jul 5th, 2009, 12:01am
 
jeff1233219 wrote on Jul 4th, 2009, 6:33pm:
What does it mean when it says "Cannot find a class or type named "Ammo" I keep getting this error...


Oh, and on this line: if (alien[i].reachedBottom), where am I supposed to declare the variable i and to what number


The code I just copied down works ok (surprisingly - see below).  Check that you still have your Ammo tab and the Ammo class is declared properly: that error would seem to suggest that you've removed it or changed its name.

As for the 'i' variable.  It's already declared in the code in the enclosing for loop:

Code:
for(int i=0; i < height ;i++) {
if (alien[i].reachedBottom()) {
 life--;
}

   }


Since 'i' acts as an index reference in the array of aliens (think of it as an address in the array) in theory that should go through every alien and check that it has reached the bottom (since each time through the loop 'i' has increased by one).  However the limit you have set in the loop is 'height' which presumably has nothing to do with the number of aliens.  This might either cause an array outOfBounds error or mean that not all aliens are checked...

As it happens you already have a for loop dealing with the aliens earlier in your code.  It would be more efficient to incorporate the alien[i].reachedBottom() call into that somehow Wink
Pages: 1 2