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 some help for space invaders type game
Page Index Toggle Pages: 1
need some help for space invaders type game (Read 1130 times)
need some help for space invaders type game
Nov 18th, 2008, 1:10pm
 
Hi, Im making a space invaders type game, but I cant get the bullets to delay (for a second) between shots, if the key is held down, there is just a stream of bullets. How can I do this?

Plane plane;
Bullet[] bullet;
int maxBullets = 5000;
int time;
int lastSec;
int counter;


void setup(){

 plane = new Plane(8);
 bullet = new Bullet[maxBullets];
 for(int i = 0; i< maxBullets; i++)
 {
   bullet[i] = new Bullet();
   bullet[i].positionY = 2;
 }
}

void draw(){

 int s = second();
 time = s;


 background(200);



 plane.display();



 for(int i = 0; i < maxBullets; i++){
   if(bullet[i].alive){
     bullet[i].display();
     bullet[i].positionY++;
   }
 }


 for(int i = 1; i< maxBullets ;i++){
   if(!bullet[i].alive){
     if(keyPressed) {
       if(key == 'a'){

         int timer = s;
         bullet[i].alive = true;
         bullet[i].positionX = plane.positionX;
         i++;
         counter ++;
         println(counter);
         return;
       }
     }
   }
 }
}



class Plane{
 
 int planeHeight;
 int positionX;
 
Plane(int planeHeight){
 
 this.planeHeight = planeHeight;
}

void display(){
 rect(positionX,90,10,planeHeight);
 
   if(keyPressed) {
   if(key == 'z'){
     plane.positionX--;
   }
 }

   if(keyPressed) {
   if(key == 'x'){
     plane.positionX++;
   }
 }
}  
}



class Bullet{
 
 boolean alive;
 int positionX;
 int positionY;
 int velocity;
 
 Bullet(){
   alive = false;
 }

 void display(){
   ellipse(positionX,positionY,5,5);
 }  
}

Re: need some help for space invaders type game
Reply #1 - Nov 18th, 2008, 6:13pm
 
You could put a counter (number of frames?) to decide if it is ok to fire the next.


//beginning of code
...
int shottimer = 0;//<----------
...
void draw() {
...
if(keyPressed) {
  if((key == 'a') && (shottimer > 60)){//<----------

    int timer = s;
    bullet[i].alive = true;
    bullet[i].positionX = plane.positionX;
    i++;  
    counter ++;
    println(counter);
   
    shottimer = 0;//<----------
    return;
  }
}
...
//end of draw
shottimer++;//<----------
}
Re: need some help for space invaders type game
Reply #2 - Nov 25th, 2008, 4:30pm
 
for games i like to just use processings ability to set the framerate in the setup method. then when a shot is fired just record the framecount. then in your if statement check to see if the current framecount is >= the old framecount + x (where x is the delay) I find it works well to do 30 frames per second. you could impement different weapons that have different delays this way also to get different firing rates.

Page Index Toggle Pages: 1