FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Responsive Form, Games
(Moderator: REAS)
   Asteroids
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Asteroids  (Read 2527 times)
REAS


WWW
Asteroids
« on: Aug 8th, 2002, 3:48am »

Here is the very beginning of mouse-based version of Asteroids/Space Wars. It shows a good use for atan2(). Anyone care to continue?  
 
// Proto-Asteroids
// by REAS
 
float a;      // The angle of the ship
float xpos, ypos;  // The position of the ship
 
void setup() {
  size(200, 200);  // Set the size to 200x200 pixels
  background(0);   // Set the background to black
  noFill();   // Turn off fill
  stroke(255);     // Set the stroke color to white
}
 
void loop() {
  // Set the position of the ship
  xpos += 0.4;
  ypos += 0.2;
 
  // If the ship moves off the screen, put it back on
  if (xpos > width) { xpos = 0; }
  if (xpos < 0) { xpos = width; }
  if (ypos > height) { ypos = 0; }
  if (ypos < 0) { ypos = height; }  
 
  // Position the ship  
  translate(xpos, ypos);
 
  // Calculate and set the angle of the ship
  a = atan2(mouseY-ypos, mouseX-xpos);
  rotate(a);
 
  // Draw the ship
  triangle(-15, 10, 15, 0, -15, -10);
}
 
 
nashdj


Re: Asteroids
« Reply #1 on: Aug 8th, 2002, 1:04pm »

added some basic acceleration
made wrap relative to speed
 
// Proto-Asteroids 0.1 ?
// Original by REAS  
// acceleration by nashdj
 
float a; // The angle of the ship  
float xpos, ypos;  // The position of the ship  
 
long time;
float vx, vy; // velocity of the ship
float acceleration = 200; // speed of acceleration
 
void setup()  
{  
  size(200, 200);  // Set the size to 200x200 pixels  
  background(0);   // Set the background to black  
  noFill();   // Turn off fill  
  stroke(255);     // Set the stroke color to white  
}  
 
void loop()  
{  
  // find out how much time has passed
  float dt = (millis() - time)/1000.0;
  time = millis();
 
  // accelerate the ship
  if (mousePressed)
  {
    vx += cos(a) * acceleration * dt;
    vy += sin(a) * acceleration * dt;
  }
 
  // Set the position of the ship  
  xpos += vx * dt;  
  ypos += vy * dt;
 
  // If the ship moves off the screen, put it back on
  if (xpos > width) xpos = xpos % width;
  if (xpos < 0) xpos += width;
  if (ypos > height)ypos = ypos % height;
  if (ypos < 0) ypos += height;
 
  // Position the ship  
  translate(xpos, ypos);  
 
  // Calculate and set the angle of the ship  
  a = atan2(mouseY-ypos, mouseX-xpos);  
  rotate(a);  
 
  // Draw the ship  
  triangle(-15, 10, 15, 0, -15, -10);  
}  
 
REAS


WWW
Re: Asteroids
« Reply #2 on: Aug 13th, 2002, 1:17am »

// Proto-Asteroids 0.2
// Started by REAS  
// Acceleration by nashdj
// Firing, deceleration by REAS
 
// Mouse-based Asteroids, navigate by pressing the mouse
// Fire with the 'space bar'.
 
long time;
float dt;
 
float a; // The angle of the ship  
float xpos, ypos;  // The position of the ship  
float vx, vy; // Velocity of the ship  
float vmax = 300; // The maximum velocity
float acceleration = 200; // Speed of acceleration  
float deceleration = 40.0; // Speed of deceleration
 
int tfire = 10; // Total number of fires
float[] firex = new float[tfire]; // X locations of fire
float[] firey = new float[tfire]; // Y locations of fire
float[] firea = new float[tfire]; // The angle of each fire's direction
float vfire; // Velocity of the fire
int nfire = 0; // Numbers of fires fired
int slength = 15; // Length of ship
int sheight = 10; // Height of ship
int scrook = 7;  // Crook of ship
   
void setup()  
{  
  size(200, 200);  // Set the size to 200x200 pixels  
  background(0);   // Set the background to black  
  noFill();   // Turn off fill  
  stroke(255);     // Set the stroke color to white  
  xpos = width/2;  // Place in center of the screen
  ypos = height/2;
}  
   
void loop()  
{  
  // Find out how much time has passed  
  dt = (millis() - time)/1000.0;  
  time = millis();  
   
  push();
  updateShip();
  drawShip();
  pop();
 
  updateAndDrawFire();
}  
 
void updateShip()  
{
 
  // Accelerate the ship if mouse is pressed
  // and it has not yet reached terminal velocity
  // Decelerate the ship if the mouse is not pressed
  if (mousePressed) {
    if(abs(vx)+abs(vy) < vmax) {  
 vx += cos(a) * acceleration * dt;  
 vy += sin(a) * acceleration * dt;
    }
  } else {
    if(abs(vx) > .01) { vx = vx - vx/deceleration; }
    if(abs(vy) > .01) { vy = vy - vy/deceleration; }
  }
 
  // Set the position of the ship  
  xpos += vx * dt;  
  ypos += vy * dt;  
 
  // If the ship moves off the screen, put i back on  
  if (xpos > width+slength) xpos = -slength;    
  if (xpos < 0-slength) xpos += width + slength;  
  if (ypos > height+slength)ypos = -slength;  
  if (ypos < 0-slength) ypos += height + slength;  
  // Position the ship    
  translate(xpos, ypos);  
 
  // Calculte and set the angle of the ship  
  a = atan2(mouseY-ypos, mouseX-xpos);  
  rotate(a);  
}
 
void updateAndDrawFire()  
{
  for(int i=0; i<tfire; i++) {
    // Update based on angle and current position
    firex[i] += cos(firea[i]) * 2;
    firey[i] += sin(firea[i]) * 2;
    // Draw  
    stroke(255);
    point(firex[i], firey[i]);
  }
}
 
 
// keyPressed() is called automatically by Proce55ing
// whenever a key is pressed
 
void keyPressed()  
{
  // If 'space bar' is pressed update the fire (laser)
  if (key == ' ') {
    firea[nfire] = a;
    firex[nfire] = xpos + cos(a)*slength;
    firey[nfire] = ypos + sin(a)*slength;
    nfire += 1;
    if (nfire >= tfire) { nfire = 0; }
    println(nfire);
  }
}
 
 
// Draw the ship  
 
void drawShip()  
{
  // Gray body
  stroke(102);
  beginShape(LINE_LOOP);
  vertex(-slength, sheight);
  vertex(slength, 0);
  vertex(-slength,-sheight);
  vertex(-scrook, 0);
  endShape();
   
  // White points simulating vector display
  stroke(255);
  beginShape(POINTS);
  vertex(-slength, sheight);
  vertex(slength, 0);
  vertex(-slength,-sheight);
  vertex(-scrook, 0);
  endShape();
}
 
anurag

Email
Re: Asteroids
« Reply #3 on: Dec 16th, 2002, 12:16am »

hi guys just tried to add a sample of the first asteroid which can be broken into two.. i am just a beginner in proce55ing so hope you dont mind..! sorry had to delete all the messages as it wouldnt post coz of length.  
// Proto-Asteroids 0.2  
// Started by REAS    
// Acceleration by nashdj  
// Firing, deceleration by REAS  
// anurag tried to add a sample of one asteroid which can be broken into two
 
long time; int col;int gepx;int gepxx;
int gepxy;float dt;int astno=1;
int asa1=int(random(1,89));  
int asa2=int(random(1,89));  
int asa3=int(random(1,89));  
float asx1=100;float asx2;float asx3;float asy1=100;
float asy2;float asy3;float asl1=40;float asb1=40;  
float a;float xpos, ypos;float vx, vy;
float vmax = 300; int tfire = 10;  
float acceleration = 200;float deceleration = 40.0;  
 
float[] firex = new float[tfire]; // X locations of fire  
float[] firey = new float[tfire]; // Y locations of fire  
float[] firea = new float[tfire]; // The angle of each fire's direction  
float vfire;  
int nfire = 0; int slength = 15; // Length of ship  
int sheight = 10;int scrook = 7;  
void setup()    
{    
  size(500, 500);background(0);noFill();    
  stroke(255);xpos = width/2;ypos = height/2;  
}    
   
void loop()    
{    
  dt = (millis() - time)/1000.0;  
  time = millis();push();updateShip();drawShip();  
  pop();asteroid();updateAndDrawFire();  
}    
 
void updateShip()  
{  
  if (mousePressed) {  
    if(abs(vx)+abs(vy) < vmax) {  
 vx += cos(a) * acceleration * dt;  
 vy += sin(a) * acceleration * dt;  
    }  
  } else {  
    if(abs(vx) > .01) { vx = vx - vx/deceleration; }  
    if(abs(vy) > .01) { vy = vy - vy/deceleration; }  
  }  
  xpos += vx * dt;ypos += vy * dt;  
  if (xpos > width+slength) xpos = -slength;    
  if (xpos < 0-slength) xpos += width + slength;  
  if (ypos > height+slength)ypos = -slength;  
  if (ypos < 0-slength) ypos += height + slength;  
  translate(xpos, ypos);    
  a = atan2(mouseY-ypos, mouseX-xpos);    
  rotate(a);  
}  
 
void updateAndDrawFire()  
{  
 
  int col=color(0,100,100);
  for(int i=0; i<tfire; i++) {  
  firex[i] += cos(firea[i]) * 10;
  firey[i] += sin(firea[i]) * 10;  
    stroke(255);point(firex[i], firey[i]);  
    int gepxx=int(firex[i]);//changes floats of shots into int for getpixel command
    int gepxy=int(firey[i]);
 if (gepxy>0 && gepxy<height && gepxx>0 && gepxx<width)
    {
    int gepx=getPixel(gepxx+1,gepxy+1); //gets the colour of the pixel next to the shot.
    if (gepx==col)// checks if the shots fired passes over a blue surface
    {
    astno=2;//breaks the asteroid into two
    println(astno);
   }
    }
  }  
}  
   
void keyPressed()  
{  
   if (key == ' ') {  
    firea[nfire] = a;
    firex[nfire] = xpos + cos(a)*slength;  
    firey[nfire] = ypos + sin(a)*slength;  
    nfire += 1;  
    if (nfire >= tfire) { nfire = 0; }  
    println(nfire);  
  }  
}  
   
 
void drawShip()  
{  
  stroke(102); beginShape(LINE_LOOP);  
  vertex(-slength, sheight);vertex(slength, 0);  
  vertex(-slength,-sheight);vertex(-scrook, 0);  
 endShape();stroke(255);  
  beginShape(POINTS);vertex(-slength, sheight);  
  vertex(slength, 0);vertex(-slength,-sheight);  
  vertex(-scrook, 0); endShape();  
}  
void asteroid()//astroids
 {
  int col=color(0,100,100);fill(col);
 if (astno==1)//for a single asteroid
  {
  ellipse(asx1,asy1,asl1,asb1);
   asx1 -= cos(asa1)*2;asy1 -= sin(asa1)*2;
   asx2 = asx1;asy2 = asy1;asx3 = asx1;asy3 = asy1;
   if (asx1>height+asl1){asx1=0-asl1;}
  if (asy1>width+asb1){asy1=0-asb1;}
  if (asx1<0-asl1){asx1=height+asl1;}
   if (asy1<0-asb1){asy1=width+asb1;}
  }
  if (astno==2)//for two asteroids
  {
   ellipse(asx2,asy2,asl1/2,asb1/2);
   asx2 -= cos(asa1+40)*3;  
   asy2 -= sin(asa1+40)*3;
  ellipse(asx3,asy3,asl1/2,asb1/2);
  asx3 -= cos(asa1-40)*3;  
   asy3 -= sin(asa1-40)*2;
   if (asx2>height+asl1/2){asx2=0-asl1/2;}
   if (asy2>width+asb1/2) {asy2=0-asb1/2;}
   if (asx2<0-asl1/2) {asx2=height+asl1/2;}
   if (asy2<0-asb1/2) {asy2=width+asb1/2;}
   if (asx3>height+asl1/2){asx3=0-asl1/2;}
   if (asy3>width+asb1/2) {asy3=0-asb1/2;}
   if (asx3<0-asl1/2) {asx3=height+asl1/2;}
   if (asy3<0-asb1/2) {asy3=width+asb1/2;}
  }
 }
 
anurag

Email
Re: Asteroids
« Reply #4 on: Dec 16th, 2002, 5:57pm »

hi guys just tried to add the ship destruction when you touch the asteroid. works fine but for only one error which i couldnt understand(arrayoutofbound)error when you move the ship out of the y axis. please tell the solution. run it and youl see
 
// Proto-Asteroids 0.2  
// Started by REAS    
// Acceleration by nashdj  
// Firing, deceleration by REAS  
// just tried to add a sample of one asteroid and the destruction of the ship, Anurag.
// Mouse-based Asteroids, navigate by pressing the mouse  
// Fire with the 'space bar'.  
// fire asteriods and avoid them to avoid destruction.
int life=1;long time;int cola;int cols;int gepx;int gepxx;
int gepxy;int xposint;int yposint;float dt;int astno=1;
int asa1=int(random(1,89));
int asa2=int(random(1,89));  
int asa3=int(random(1,89));float asx1=100;
float asx2;float asx3;float asy1=100;float asy2;float asy3;
float asl1=40;float asb1=40; float a; float xpos, ypos;  
float vx, vy;float vmax = 300;float acceleration = 200;  
float deceleration = 40.0;int tfire = 10;  
float[] firex = new float[tfire];  
float[] firey = new float[tfire];  
float[] firea = new float[tfire];  
float vfire;int nfire = 0; int slength = 15; int sheight = 10;  
int scrook = 7;  
void setup(){  
size(500, 500); background(0); noFill();
stroke(255); xpos = width/2; ypos = height/2;  
cola=color(0,100,100); cols=color(100,0,100);}    
void loop(){    
  dt = (millis() - time)/1000.0; time = millis();  
   if (life==1){
  push(); updateShip(); drawShip(); pop(); asteroid();
  updateAndDrawFire(); shipdest(); }
  if (life==2){asteroid();}}    
void updateShip(){  
  if (mousePressed) {  
    if(abs(vx)+abs(vy) < vmax) {  
 vx += cos(a) * acceleration * dt;  
 vy += sin(a) * acceleration * dt; }
  } else {  
    if(abs(vx) > .01) { vx = vx - vx/deceleration; }  
    if(abs(vy) > .01) { vy = vy - vy/deceleration; }  
  }  
  xpos += vx * dt;ypos += vy * dt; xposint=int(xpos);  
  yposint=int(ypos);  
  if (xpos > width+slength) xpos = -slength;    
  if (xpos < 0-slength) xpos += width + slength;  
  if (ypos > height+slength)ypos = -slength;  
  if (ypos < 0-slength) ypos += height + slength;  
  translate(xpos, ypos);    
  a = atan2(mouseY-ypos, mouseX-xpos);    
  rotate(a);  }
void updateAndDrawFire()  {
   for(int i=0; i<tfire; i++) {  
   firex[i] += cos(firea[i]) * 10;  
   firey[i] += sin(firea[i]) * 10;
   stroke(255);point(firex[i], firey[i]);
   int gepxx=int(firex[i]);
   int gepxy=int(firey[i]);
   if (gepxy>0 && gepxy<height && gepxx>0 && gepxx<width){
    int gepx=getPixel(gepxx+1,gepxy+1); //colour of pixel next to bullet
  if (gepx==cola)// checks if the shots fired passes over a blue surface
   { astno=2;//breaks the asteroid into two
   println(astno); }
    }}}  
void keyPressed(){  
  if (key == ' ') {  
    firea[nfire] = a; firex[nfire] = xpos + cos(a)*slength;  
    firey[nfire] = ypos + sin(a)*slength; nfire += 1;  
    if (nfire >= tfire) { nfire = 0; }  
    println(nfire);  
  }}  
void drawShip(){  
  fill(cols); stroke(244); beginShape(POLYGON);  
  vertex(-slength, sheight); vertex(slength, 0);  
  vertex(-slength,-sheight); vertex(-scrook, 0);  
 endShape(); beginShape(POINTS);  
  vertex(-slength, sheight); vertex(slength, 0);  
  vertex(-slength,-sheight); vertex(-scrook, 0);  
  endShape();}  
void asteroid(){
 fill(cola);stroke(255);
  if (astno==1){
  ellipse(asx1,asy1,asl1,asb1);
   asx1 -= cos(asa1)*2; asy1 -= sin(asa1)*2;
  asx2 = asx1; asy2 = asy1;
  asx3 = asx1; asy3 = asy1;
  if (asx1>height+asl1){asx1=0-asl1;}
  if (asy1>width+asb1){asy1=0-asb1;}
   if (asx1<0-asl1){asx1=height+asl1;}
   if (asy1<0-asb1){asy1=width+asb1;}
  }
  if (astno==2){
   ellipse(asx2,asy2,asl1/2,asb1/2);
   asx2 -= cos(asa1+40)*3; asy2 -= sin(asa1+40)*3;
   ellipse(asx3,asy3,asl1/2,asb1/2);
  asx3 -= cos(asa1-40)*3; asy3 -= sin(asa1-40)*2;
   if (asx2>height+asl1/2) {asx2=0-asl1/2;}
   if (asy2>width+asb1/2){asy2=0-asb1/2;}
   if (asx2<0-asl1/2){ asx2=height+asl1/2;}
   if (asy2<0-asb1/2){asy2=width+asb1/2;}
   if (asx3>height+asl1/2){ asx3=0-asl1/2;}
   if (asy3>width+asb1/2){asy3=0-asb1/2;}
  if (asx3<0-asl1/2){asx3=height+asl1/2;}
   if (asy3<0-asb1/2){asy3=width+asb1/2;}
  }}
 void shipdest(){
 for (int sl=yposint-15;sl<yposint+15;++sl) {
 for (int sh=xposint-15;sh<xposint+15;++sh) {
  if (getPixel(sh,sl)==cols)   {
  if (getPixel(sh+1,sl)==-1 || getPixel(sh-1,sl)==-1 ||getPixel(sh,sl+1)==-1 || getPixel(sh,sl-1)==-1){
   println("boom");life=2;
   for(int di=1; di<40; ++di){
   stroke(244);fill(255,0,0); beginShape(POLYGON);
  vertex(xposint-20, yposint); vertex(xposint-10, yposint);
  vertex(xposint-2, yposint-9); vertex(xposint+3, yposint+3);
  vertex(xposint+15, yposint-4); vertex(xposint+6, yposint+9);
  vertex(xposint+13, yposint+14); vertex(xposint+3, yposint+7);
  vertex(xposint-17, yposint+14); vertex(xposint-8, yposint+7);
  endShape();}
   }}}}}
 
Pages: 1 

« Previous topic | Next topic »