2021年12月20日 星期一

week15

一.

貓狗大戰

程式碼:

float big=0,x,y,vx,vy;

boolean flying=false;

void setup(){

  size(500,300);

}

void draw(){

  background(255);

  fill(255,0,0);rect(0,0,big,50);

  line(200,200,mouseX,mouseY);

  if(mousePressed)big++;

  if(flying){

    x+=vx;y+=vy;vy+=0.98;

    ellipse(x,y,30,30);

  }

}

void mouseReleased(){

  flying=true;

  x=200;y=200;

  float angle=atan2(200-mouseY,200-mouseX);

  vx = cos(angle)*big/2;vy=sin(angle)*big/2;

  big=0;

}

進度;

float big=0,x1,y1,vx1,vy1,x2,y2,vx2,vy2;

boolean Catflying=false,Dogflying=false;

int round=0;

void setup(){

  size(500,300);

}

void draw(){

  background(255);

  fill(0,255,0);rect(0,0,big,50);

  fill(255,0,0);rect(240,150,20,150);

  if(mousePressed){

    big++;

  }

  if(Catflying){

     round=1;

     x1+=vx1;y1+=vy1;vy1+=0.98;

     if(Collision1()) fill(255);

     else ellipse(x1,y1,30,30);

  }

  

  if(Dogflying){

     round=0;

     x2+=vx2;y2+=vy2;vy2+=0.98;

     if(Collision2()) fill(255);

     else ellipse(x2,y2,30,30);

  }

 

}

boolean Collision1(){

  if(x1>240  && y1>150   )return true;

  else return false;

}

boolean Collision2(){

  if(x2<260 && y2>150   )return true;

  else return false;

}

void mouseReleased(){


  if(round==0){


     x1=50;y1=250;

     float angle1=atan2(x1-250,y1-50);

     vx1 = cos(angle1)*big/3;vy1=sin(angle1)*big/3;

     Catflying=true;

     Dogflying=false;

  }



  if(round==1){

     x2=450;y2=250;

     float angle2=atan2(x2-250,y2-450);

     vx2 = cos(angle2)*big/3;vy2=-sin(angle2)*big/3;

     Dogflying=true;

     Catflying=false;

  }

  big=0;

}

沒有留言:

張貼留言