首页 > Sktech > 习作AB_带场景地图的喷气飞船
2017
08-31

习作AB_带场景地图的喷气飞船

习作AB_带场景地图的喷气飞船 - 第1张  | Processing编程艺术


飞船本身的代码修改自nature of code第三章的习题范例.
追加了两边侧面喷和倒喷动作.


– 问题:在地图边缘和镜头判定边缘处镜头依然抖动.
– 问题:飞船侧喷移位只是随便设了个定数很难控制,不知道重心怎么算.


int pbLeRoller=0;
PVector pbTheOringin;
//--
EcMap pbTheMap= new EcMap();
Spaceship ship;


void setup() {
  size(320, 240);
  frameRate(32);
  ship = new Spaceship(pbTheMap);
  pbTheOringin=new PVector(width/2,height/2);
}


void draw() {background(0);pbLeRoller++;pbLeRoller&=0x1F;
  //--
  fsCheckCamera();
  pbTheMap.ccUpdate();
  ship.ccRefresh();
  //--
  fill(0x77,0xEE,0x77);if(pbLeRoller<16){
  fnTextString("[J] to accel [K] to break...",2);
  fnTextString("[A][D] to turn the ship...",3);}
  fnTextFloat("cameraX:",pbTheMap.cmPos.x, 9);
  fnTextFloat("cameraY:",pbTheMap.cmPos.y,10);
  fnTextFloat("shipX:",ship.location.x,11);
  fnTextFloat("shipY:",ship.location.y,12);
  fnTextFloat("speed:",ship.velocity.mag(),13);
  //--
  if (keyPressed) {switch(key){
    case 'a':ship.turn(-0.15);ship.cmTurnPlus    = true;break;
    case 'd':ship.turn( 0.15);ship.cmTurnMinus   = true;break;
    case 'j':case 'w':ship.thrust( 1);ship.thrusting     = true;break;
    case 'k':case 's':ship.thrust(-1);ship.backThrusting = true;break;
   default:break;
  }}
  //--
}//+++


void keyPressed(){switch(key){
  case 'q':fsPover();break;
  default:break;
}}//+++


void fsPover(){
  //--
  exit();
  //--
}//+++


void fsCheckCamera(){
  PVector lpDist=PVector.sub(ship.cmAbsolutePos,pbTheOringin);
  if(lpDist.mag()>32){
    lpDist.normalize();
    lpDist.mult(ship.velocity.mag());
    pbTheMap.cmPos.sub(lpDist);
  }
}//+++


void fnTextFloat(String pxTitle, float pxVal, int pxLine){
  text(pxTitle+nfc(pxVal,2),16,16*pxLine);
}

void fnTextInt(String pxTitle, int pxVal, int pxLine){
  text(pxTitle+nf(pxVal,4),16,16*pxLine);
}

void fnTextString(String pxTitle, int pxLine){
  text(pxTitle,16,16*pxLine);
}

class EcMap{
  PVector cmPos;
  PVector cmDia;
  int cmColorSet;
  EcMap(){
    cmPos=new PVector(22,22);
    cmDia=new PVector(1024,768);
    cmColorSet=0x77;
  }
  //--
  void ccUpdate(){
    fill(cmColorSet,cmColorSet);rect(cmPos.x,cmPos.y,cmDia.x,cmDia.y);
    float lpDiv=cmDia.x/8;
    fill(255-cmColorSet);
    for(int i=0;i<64;i++){
      text(nf(i,3),i%8*lpDiv+cmPos.x,i/8*lpDiv+cmPos.y);
    }
  }
  //--
}//+++


class Spaceship {
  //--
  EcMap cmMap;
  //--
  PVector cmAbsolutePos;
  //--
  PVector location,velocity,acceleration;
  float damping,topspeed,heading,r;
  boolean thrusting,backThrusting;
  //--
  boolean cmTurnMinus,cmTurnPlus;
  //--
  Spaceship(EcMap pxMap) {
    //--
    cmAbsolutePos = new PVector();
    location = new PVector(2,2);
    velocity = new PVector();
    acceleration = new PVector();
    cmMap=pxMap;
    damping=0.995;
    topspeed=8;
    heading=2;
    r=8;
    thrusting=false;
    backThrusting=false;
    cmTurnMinus=false;
    cmTurnPlus=false;
    //--
  } 
  //--
  void ccRefresh(){update();display();}
  void update() { 
    velocity.add(acceleration);velocity.mult(damping);velocity.limit(topspeed);
    location.add(velocity);
    location.x=constrain(location.x,0,cmMap.cmDia.x);
    location.y=constrain(location.y,0,cmMap.cmDia.y);
    acceleration.mult(0);
  }
  //--
  void applyForce(PVector force) {PVector f = force.get();acceleration.add(f);}
  void turn(float a) {heading += a;thrust(a*0.3);}
  //--
  void thrust(float pxDirect) {
    float angle = heading - pxDirect*PI/2;
    PVector force = new PVector(cos(angle),sin(angle));
    force.mult(0.3);
    applyForce(force);
  }
  //--
  void display() { 
    //--
    cmAbsolutePos=PVector.add(location,cmMap.cmPos);
    //--
    pushMatrix();translate(cmAbsolutePos.x,cmAbsolutePos.y);rotate(heading);rectMode(CENTER);
    //--
    fill(thrusting?color(0x77,0x77,0xFF):color(0x33,0x11));rect(-r/2,r,r/2,2*r);rect(r/2,r,r/2,2*r);
    fill(backThrusting?color(0x77,0x77,0xFF):color(0x33,0x11));rect(-r*3/4,-r/2,r/4,r);rect(r*3/4,-r/2,r/4,r);
    fill(cmTurnMinus?color(0x77,0x77,0xFF):color(0x33,0x11));rect(-r,-r,10,r/4);rect( r,0,10,r/4);
    fill(cmTurnPlus?color(0x77,0x77,0xFF):color(0x33,0x11));rect( r,-r,10,4/4);rect(-r,0,10,r/4);
    //--
    fill(175);stroke(0);beginShape();
    vertex(-r,r);vertex(0,-2*r);vertex(r,r);
    endShape(CLOSE);rectMode(CENTER);rectMode(CORNER);noStroke();
    popMatrix();
    //--
    thrusting=false;backThrusting=false;
    cmTurnMinus=false;cmTurnPlus=false;
  }
}//+++

 

 



最后编辑:
作者:constrain
constrain
nullpointerexception

留下一个回复

你的email不会被公开。