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 › Lunar Landing Game
Page Index Toggle Pages: 1
Lunar Landing Game (Read 970 times)
Lunar Landing Game
Nov 26th, 2009, 12:05pm
 
Guys,

Here's my code. I'm an apprentice on processing Smiley


int posx =200; //position of the left vertex of our ship
int posy =275; // position of the bottom vertex of our ship

void setup() {
 
size(400, 400);
background(0, 0, 0);//uzay boslugu
stroke(183,141,26);//yeryüzü kahverengi
noFill();

}
void draw() {
noStroke();
fill(183,141,26);
rect(0,350,400,50);

for(int k =360 ; k<390; k=k+10) {
    for (int i=10 ; i<390 ; i= i+40 ){
      if (k==370 ) {
        k=k+5;
      }
        noFill(); //taşlar çiziliyor
        rect (i, k, 10, 5) ;
     
    }
}


 
//drawing the spaceship
stroke (2);
fill(139,160,11);
rect(posx,posy,25,60);
fill(255,0,0,1);
triangle(posx + 12,posy - 23,posx + 32,posy,posx-7,posy);

for (int i=187 ; i<212; i=i+3) {
 stroke(255,0,0,1);
 line(posx,posy+60,i,posy+75);
}

for (int i=215 ; i<240; i=i+3) {
stroke(255,0,0,1);
line(225,335,i,350);
}
}

I want to build a game similar to this:

http:// phet.colorado.edu/sims/lunar-lander/lunar-lander_en.html

Could anybody help me? (want to use some gravity and some velocity)

Thanks
Re: Lunar Landing Game
Reply #1 - Nov 26th, 2009, 12:19pm
 
nice one

(i just had to)
Re: Lunar Landing Game
Reply #2 - Nov 27th, 2009, 2:24pm
 
Here is some code I used in a previous project. Feel free to study it and re-use it.

Quote:
final float GRAVITY = 0.016;
final float AIR = 0.99;
final float TURN_VALUE = PI/40;
final float PROP = 0.05;

Ship ship;

boolean[] keys = new boolean[4];

void setup() {
  ship = new Ship(new PVector(0, 0), 5);
  size(400, 300);
}

void draw() {
  model();
  view();
}

// physics stuff
void model() {
  handleKeys();
  ship.move();
}

// drawing stuff
void view() {
  background(255);
  translate(width/2, height/2);
  ship.draw();
}

void keyPressed() {
  if (key == CODED) {
    switch (keyCode) {
      case UP:
        keys[0] = true; break;
      case LEFT:
        keys[1] = true;  break;
      case RIGHT:
        keys[2] = true; break;
    }
  }
}

void keyReleased() {
  if (key == CODED) {
    switch (keyCode) {
      case UP:
        keys[0] = false; break;
      case LEFT:
        keys[1] = false; break;
      case RIGHT:
        keys[2] = false; break;
    }
  }
}

void handleKeys() {
  if (keys[0]) ship.thrust();            // THRUST
  if (keys[1]) ship.turns(-TURN_VALUE);  // LEFT
  if (keys[2]) ship.turns(TURN_VALUE);   // RIGHT
}

class Ship {

  PVector pos;   // position
  float rot;     // rotation angle
  PVector dis;   // displacement
  float speed;   // speed
  
  int siz;       // size
  PVector[] pt;  // shape
  
  Ship(PVector pos, int siz) {
    this.pos = pos;
    this.siz = siz;
    dis = new PVector(0, 0);
    // the ship has its nose upwards
    rot = -PI/2;
    pt = new PVector[3];
    updatePoints();
  }
  
  /**
   * calculate the coordinates of the shape
   */
  void updatePoints() {
    // nose
    pt[0] = new PVector(pos.x+siz*cos(rot), pos.y+siz*sin(rot));
    // bottom left
    pt[1] = new PVector(pos.x+1.7*siz*cos(rot+(PI+0.7)), pos.y+1.7*siz*sin(rot+(PI+0.7)));
    // bottom right
    pt[2] = new PVector(pos.x+1.7*siz*cos(rot+(PI-0.7)), pos.y+1.7*siz*sin(rot+(PI-0.7)));
  }
  
  /**
   * display the ship on screen
   */
  void draw() {
    strokeWeight(1); stroke(0); fill(255);
    triangle(pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x, pt[2].y);
  }
  
  void move() {
    // friction
    dis.mult(AIR);
    // gravity
    dis.y += GRAVITY;
    // propulsion
    if (keys[0]) dis.add(new PVector(PROP*cos(rot), PROP*sin(rot)));
    // deplacement
    pos.add(dis);
    updatePoints();
  }
  
  void turns(float angle) {
    rot += angle;
  }
  
  void thrust() {
    if (speed < 1) speed += 0.1;
  }

}
Page Index Toggle Pages: 1