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.
Page Index Toggle Pages: 1
space invaders (Read 1564 times)
space invaders
Aug 26th, 2008, 7:12am
 
hey im having trouble creating a space invaders type program. where the bullet is shot from the mouse when it is pressd at amoving boxes down the bottom
this is my code so far to create the bullet but cant get it to work
any tips would be great
int y = 0;
int i = 0;
shoot [] history;
void setup() {
 size(500, 500);
 background(255);
 history = new shoot[100];
 if(mousePressed){
      i=i+1;
history[i] = new shoot(mouseX,mouseY);
 
   
}
}

void draw()
{
 for(int k = 0;k<100;k++){
   history[i].update();
   history[i].draw();

 }
}
 
   

  void mouseReleased(){
    history[i].update();
   
 }

 class shoot{
   int x = 0;
   int y = 0;
   shoot(int X,int Y){
   x = X;
   y= Y;
   
   }
 
 publi void update (){
    y = y+10;
    stroke(30,30,0);
  }
    void draw(){
      line(x,y,x,y+10);
    }

}
Re: space invaders
Reply #1 - Aug 26th, 2008, 8:32am
 
Quick fix:
Code:
 int i = 0;
int MAX = 100;
shoot [] history;
void setup() {
size(500, 500);
background(255);
history = new shoot[MAX];
}

void draw()
{
background(55, 120, 178);
for(int k = 0;k<i-1;k++){
history[k].update();
history[k].draw();

}
if(mousePressed && i < MAX){
history[i++] = new shoot(mouseX,mouseY);
}
}



class shoot{
int x = 0;
int y = 0;
shoot(int X,int Y){
x = X;
y= Y;

}

public void update (){
y = y+10;
}
void draw(){
stroke(30,30,0);
line(x,y,x,y+10);
}

}

No dynamic code in setup, it is called only once.
Removed the mouseReleased callback, I don't see its purpose.
Have put a limit on i. You should find another way since you will exhaust quickly the array. No time to code that now. Limited k to latest i, to avoid using a null object.
Moved the stroke() to draw(). Added a background() call to avoid the tracing on screen.
Re: space invaders
Reply #2 - Aug 26th, 2008, 2:34pm
 
thanks heaps man, tonnes of help
also i was wondering if anyone had any tips for making the bullet react and hit a box if it collids
cheers jamie
Page Index Toggle Pages: 1