2012年3月18日 星期日

Homework 6




I have learnt how to use arrays to store one picture into many locations. Thereafter, I used mouseX and mouseY to move the racket.   I also use an array to kill the mosquitos. If I click a mosquito the score increases until I finish them all. At the end a win message should be displayed.

PImage b;
PImage r;
PImage k;
int t=10;
int n=50;
int score=0;
String j;
boolean showimage = true;
int[] x = new int[n];
int[] y = new int[n];
int[] kill = new int [n];
void setup() {
    size(1000,800);
  b = loadImage("mosquito.jpg");
  r = loadImage("wilson-federer-27-110-tennis-racket.jpg");
 
  PFont font;
  font = loadFont("AgencyFB-Reg-48.vlw");
  textFont(font);
  textSize(20);
  String j = "Score is =";
  text(j,450, 20);
 
  b.resize(b.width/5, b.height/5);
  r.resize(r.width/2, r.height/2);
 
  for(int i=0;i<n;i++) {
    x[i]= int(random(0,width));
    y[i]= int(random(0,height));
    kill[i]=1;
  }
     for(int i=0;i<n;i++)
     {
    if( kill[i]==1)
    image(b, x[i], y[i],55,55);
  }
    
 
}
void draw() {
 background(255);
 for(int i=0;i<n;i++) {
      if (mousePressed == true){
            if (mouseButton == LEFT){
              if (x[i]<=mouseX && x[i]>=mouseX-50 && y[i]<=mouseY && y[i]>=mouseY-50 && kill[i]==1 && score <50){
              kill[i]=0;
              score++;
            }
      }
   }
 }

    for(int i=0;i<n;i++)
         if( kill[i]==1) image(b,x[i], y[i],55,55);
        
         if(score==50) {
           background(255);
           fill(0);
           text ("You Win", 350,350);
         }
fill(0);
text ("Score:", 630,50);
text (score, 730,50);
image (b, mouseX-40, mouseY-20,85,85);
}

沒有留言:

張貼留言