首页 > Sktech > 习作AA_带场景地图的弹球
2017
08-30

习作AA_带场景地图的弹球

习作AA_带场景地图的弹球 - 第1张  | Processing编程艺术


– 问题:镜头速度过快会产生抖动


EcMap pbTheMap = new EcMap();
EcBall pbTheBall = new EcBall(pbTheMap);;

void setup(){
  size(320,240);
  frameRate(29);
  textAlign(LEFT, CENTER);
  ellipseMode(CENTER);
  //-- 
}

void draw(){
  background(0);
  pbTheMap.ccUpdate();
  pbTheBall.ccUpdate();
  fill(0x77,0xEE,0x77);
  fnTextString("Press SpaceBar to activate \n  or stop bouncing...",4);
  fnTextFloat("cameraX:",pbTheMap.cmX,9);
  fnTextFloat("cameraY:",pbTheMap.cmY,10);
  fnTextFloat("ballX:",pbTheBall.cmRelativeX,11);
  fnTextFloat("ballY:",pbTheBall.cmRelativeY,12);
}

void keyPressed(){
  switch(key){
    //--
    case 'w':pbTheBall.ccShiftPos(0,-3);break;
    case 's':pbTheBall.ccShiftPos(0, 3);break;
    case 'a':pbTheBall.ccShiftPos(-3,0);break;
    case 'd':pbTheBall.ccShiftPos( 3,0);break;
    //--
    case ' ':pbTheBall.cmIsStop=!pbTheBall.cmIsStop;break;
    //--
    case 'q':fsPover();break;
    default:break;
  }
}

void fsPover(){
  exit();
}

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 EcElement{
  float cmX=0;
  float cmY=0;
  float cmW=100;
  float cmH=100;
  EcElement(){;}
}


class EcMap extends EcElement{
  EcMap(){
    super();
    cmH=600;
    cmW=cmH;
  }
  void ccUpdate(){
    fill(0x77);
    rect(cmX,cmY,cmW,cmH);
    fill(0x33);
    
    float lpDiv=cmW/8;
    for(int i=0;i<64;i++){
      text(nf(i,3),i%8*lpDiv+cmX,i/8*lpDiv+cmY);
    }
  }
  void ccMoveCamera(float pxOffsetX, float pxOffsetY){
    cmX+=pxOffsetX;
    cmY+=pxOffsetY;
  }
}


class EcBall extends EcElement{
  float cmRelativeX;
  float cmRelativeY;
  
  float cmSpeedX=2;
  float cmSpeedY=2;
  
  float cmCameraSpeed=2;
  int cmCameraEffect=3;
  
  boolean cmIsStop=false;
  
  EcElement cmFollowed;
  EcBall(EcElement pxFollowed){
    super();
    cmRelativeX=32;
    cmRelativeY=32;
    cmW=8;
    cmH=8;
    cmFollowed=pxFollowed;
  }
  void ccUpdate(){
    ccCheckCamera();
    cmX=cmFollowed==null?0:cmFollowed.cmX+cmRelativeX;
    cmY=cmFollowed==null?0:cmFollowed.cmY+cmRelativeY;
    if(!cmIsStop){ccShiftPos(cmSpeedX,cmSpeedY);}
    if(ccIsOutOfRange()){
      cmSpeedX=random(-8,8);
      cmSpeedY=random(-8,8);
    }
    fill(0xEE);
    ellipse(cmX,cmY,cmW,cmH);
  }
  void ccShiftPos(float pxOffsetX, float pxOffsetY){
    cmRelativeX+=pxOffsetX;cmRelativeX=constrain(cmRelativeX,0,cmFollowed==null?cmW:cmFollowed.cmW);
    cmRelativeY+=pxOffsetY;cmRelativeY=constrain(cmRelativeY,0,cmFollowed==null?cmH:cmFollowed.cmH);
  }
  
  boolean ccIsOutOfRange(){
    return (cmRelativeX<1) || (cmRelativeX>cmFollowed.cmW-1) || (cmRelativeY<1)||(cmRelativeY>cmFollowed.cmH-1);
  }
  void ccCheckCamera(){
    if(cmX<width *1/cmCameraEffect){cmFollowed.cmX+=cmCameraSpeed;}
    if(cmX>width *(cmCameraEffect-1)/cmCameraEffect){cmFollowed.cmX-=cmCameraSpeed;}
    if(cmY<height*1/cmCameraEffect){cmFollowed.cmY+=cmCameraSpeed;}
    if(cmY>height*(cmCameraEffect-1)/cmCameraEffect){cmFollowed.cmY-=cmCameraSpeed;}
    
  }
}

 



最后编辑:
作者:constrain
constrain
nullpointerexception

习作AA_带场景地图的弹球》有 4 条评论

  1. 卡萨布兰卡 卡萨布兰卡 说:

    要是能做个gif就更好了

留下一个回复

你的email不会被公开。