2016年11月14日 星期一

Week 08 曹家豪

期中作品程式碼:

int score = 0,count=990*2,lv=1;

class ShotObject
{
  PVector position;
  PVector velocity;

  ShotObject()
  {
    this.position = new PVector(random(0, width), -10);
    this.velocity = new PVector(random(0, 1), random(1, 2));
  }

  void updatePosition()
  {
    position.add(velocity);
  }

  void displayObject()
  {
   if(score<10) ellipse(position.x, position.y,10,10);
 
   else if(score<20)
   {
      ellipse(position.x,position.y,8,8);
      lv=2;
   }
 
   else if(score<30)
   {
      ellipse(position.x,position.y,5,5);
      lv=3;
   }
 
   else if(score<40)
   {
     ellipse(position.x,position.y,3,3);
     lv=4;
   }
 
   else if(score<50)
   {
     ellipse(position.x,position.y,1,1);
     lv=5;
   }
 }
}

class Bullet
{
  PVector position;
  PVector velocity;

  Bullet()
    {
      this.position = new PVector(width/2, height);
      this.velocity = new PVector((width/2-mouseX)/sqrt((width/2-mouseX)*(width/2-mouseX)+(height-mouseY)*(height-mouseY))*5, (height-mouseY)/sqrt((width/2-mouseX)*(width/2-mouseX)+(height-mouseY)*(height-mouseY))*5);
    }

  void updatePosition()
  {
    position.sub(velocity);
  }

  void displayObject()
  {
    ellipse(position.x, position.y, 5, 5);
  }
}

ArrayList shotObjects = new ArrayList();
ArrayList bullets = new ArrayList();

int shotObjectDelay = 0;

void setup()
{
  size(500,500);
  smooth();
  noStroke();
  ellipseMode(RADIUS);
}

void draw()
{

  if(count<60) background(255);

  else
  {
    background(0);
    count--;
  }
  fill(0,255,255);
  textSize(50);
  text("Time:"+int(count/60),0,50);

  textSize(20);
  text("Score:"+int(score),400,40);
  text("LV:"+int(lv),400,60);

  fill(255);
  if(shotObjectDelay == 0)
  {
    shotObjects.add(new ShotObject());
    shotObjectDelay = 50;
  }
  else
  shotObjectDelay--;

  for(int i = 0; i<shotObjects.size(); i++)
  {
    ((ShotObject)shotObjects.get(i)).updatePosition();
    ((ShotObject)shotObjects.get(i)).displayObject();

    for(int j = 0; j<bullets.size(); j++)
    {
      if(PVector.dist(((ShotObject)shotObjects.get(i)).position, ((Bullet)bullets.get(j)).position)<10)
      {
        shotObjects.remove(i);
        bullets.remove(j);
        score++;
      }
    }
  }

  for(int i = 0; i<bullets.size(); i++)
  {
    ((Bullet)bullets.get(i)).updatePosition();
    ((Bullet)bullets.get(i)).displayObject();
  }
}

void mousePressed()
{
bullets.add(new Bullet());
}

沒有留言:

張貼留言