红野猪动画

来源:互联网 发布:ubuntu xorg 安装 编辑:程序博客网 时间:2024/05/16 01:58
Robot robot1, robot2, robot3;
PImage img_Map;




//PImage img_Mon1;






void setup(){
size(800, 600);
img_Map = loadImage("back.jpg");


robot1 = new Robot(100,100,0.5);






//robot2 = new Robot(100,100,0.5);
//robot2.run(8);






//robot3 = new Robot(100,100,0.5);
//robot3.run(7);








//img_Mon1 = loadImage("红野猪/mon12_0.tga");
}




void draw(){
  image(img_Map, 0, 0);
  //image(img_Mon1, 0, 0);
  
  robot1.go();
  //robot2.go();
 // robot3.go();




  ellipse(100,100,10,10);//起点




  
  String t = "人物八方向移动演示:X,Y,XY同时到达移动";
  text(t, 120, 30);
}
//地图Tile比例48*32屏幕像素大小的矩形区域,48*32=3:2
void keyPressed() {
    switch(key){
      case '1': robot1.run(6); break;
      case '2': robot1.run(5); break;
      case '3': robot1.run(4); break;
      case '4': robot1.run(7); break;
      case '5': robot1.setMode(0); break;
      case '6': robot1.run(3); break;
      case '7': robot1.run(8); break;
      case '8': robot1.run(1); break;
      case '9': robot1.run(2); break;    


      //case 'z': robot1.setMode(1); break;
      //case 'x': robot1.setMode(0); break;
      case 'a': robot1.setMode(2); break;
      case 's': robot1.setMode(3); break;
      case 'd': robot1.setMode(4); break;


    }
    switch(keyCode){
      case DOWN: robot1.run(1); break;
      case LEFT: robot1.run(3); break;
      case UP: robot1.run(5); break;
      case RIGHT: robot1.run(7); break;
    }


}






class Robot {
String name="红野猪";
float x1, y1;//位置
float x2, y2;//目标
float speed;//速度
int fx;//方向:1:X轴,2:Y轴,3:XY轴
int mode;//站立、跑、攻击


int time1,time2,time3;
int j;//第几帧
float speed_temp;//临时速度变量


PImage [] imgS = new PImage[343];
//PImage [][] imgW = new PImage[8][10];
//PImage [][] imgA = new PImage[8][10];






public Robot(float _x, float _y, float _s) {
  x1 = _x;
  y1 = _y;
  x2 = _x;
  y2 = _y;
  
  speed = _s;


  j = 0;
  mode = 0;
  time1 = 0;
  fx = 1;
  
  for(int i1=0;i1<=342;i1++)
  {
      imgS[i1] = loadImage("红野猪/mon12_" + i1 + ".tga");
      //imgW[i1-1][i2-1] = loadImage("W/nz_" + i1 + "_00" + i2 + ".png");
      //imgA[i1-1][i2-1] = loadImage("A/nza_" + i1 + "_00" + i2 + ".png");
      println(i1);
  }
}


void setMode(int _n) {
  j = 0;
  mode = _n;
}
void setMuBiao(float _x, float _y) {
  x2 = _x;
  y2 = _y;
}
void setFangXiang(int _f) {
  j = 0;
  fx = _f;
}
void run(int _n) {


  println(_n);
  setFangXiang(_n);
  setMode(1);
   switch(_n){
      case 1: setMuBiao(x1,y1-32); break;
      case 2: setMuBiao(x1+48,y1-32); break;
      case 3: setMuBiao(x1+48,y1); break;
      case 4: setMuBiao(x1+48,y1+32); break;     
      case 5: setMuBiao(x1,y1+32); break;
      case 6: setMuBiao(x1-48,y1+32); break;
      case 7: setMuBiao(x1-48,y1); break;
      case 8: setMuBiao(x1-48,y1-32); break;
   }


}
void weizhiUpdate() {
  if (x2 == x1 && y2 == y1) return;




  float dx = x2 - x1;
  float dy = y2 - y1;
  float dd = sqrt(dx * dx + dy * dy);




  




if(fx == 3 || fx == 7 )
  speed_temp = speed * 3;
if(fx == 1 || fx == 5 )
  speed_temp = speed * 2;
if(fx == 2 || fx == 4 || fx == 6  || fx == 8 )
  speed_temp = speed * sqrt(2*2+3*3);
  
  //println(speed_temp);
  
  if (dd < speed_temp){
      x1 = x2;
      y1 = y2;
  }else{
    x1 += dx * (speed_temp / dd);
    y1 += dy * (speed_temp / dd);
  }
  




}
void dongzuoUpdate() {
  time2 = millis() ;
  time3 = time2 - time1;
  


  
  if(mode==0 && time3 >= 1050){
    
  if(j==4-1)
    j=0;
  else
    j++;
    
    time1 = time2;
    println(fx);
  }


  if(mode==1){
    if(time3 >= 150){
      if(j==6-1)
        j=0;
      else
        j++;
  
      time1 = time2;
    }
  }
  
    if(mode==2){
    if(time3 >= 150){
      if(j==6-1)
        j=0;
      else
        j++;
  
      time1 = time2;
    }
  }
  
  if(mode==3){
    if(time3 >= 150){
      if(j==2-1)
        j=0;
      else
        j++;
  
      time1 = time2;
    }
  }
    if(mode==4){
    if(time3 >= 150){
      if(j==10-1)
        j=0;
      else
        j++;
  
      time1 = time2;
    }
  }
switch(mode){
  case 0: image(imgS[(fx-1)*10 + j], x1-100/2, y1-168/2); break;
  case 1: image(imgS[80+(fx-1)*10 + j], x1-100/2, y1-168/2); break;
  case 2: image(imgS[160+(fx-1)*10 + j], x1-100/2, y1-168/2); break;
  case 3: image(imgS[240+(fx-1)*2 + j], x1-100/2, y1-168/2); break;
  case 4: image(imgS[260+(fx-1)*10 + j], x1-100/2, y1-168/2); break;




  //case 1: image(imgW[fx-1][j], x1-100/2, y1-168/2); break;
 // case 2: image(imgA[fx-1][j], x1-300/2, y1-300/2); break;
}
  //kkk =  80 +  fx *10+j;
//println(80 +  (fx-1) *10+j);


ellipse(x1,y1,10,10);//中心点


//image(imgS[j], x1, y1);


//text(x1,x1+30,y1+30);
//text(y1,x1+30,y1+30+10);
//text(x2,x1+30,y1+30+20);
//text(y2,x1+30,y1+30+30);


text(name,x1,y1-10);
}




void go() {
  weizhiUpdate();
  dongzuoUpdate();
}


}


0 0
原创粉丝点击