2020年3月15日 星期日

week02


上週教的程式碼
可以像小畫家一樣隨意塗鴉
==============================================================

本週新教的 - 隕石掉落

程式碼:
int x=150,y=100;
void setup(){
    size(300,200);
  }
void draw(){
  background(255);
  circle(x,y,30);
  y+=3; //1hr=60min,1min=60sec,1sec=60frame
    if(mousePressed){
       x=mouseX;
       y=mouseY;
    }
}

             ==============================================================

多個隕石掉落

使用陣列的方式
int [ ]x={0,0,0,0,0,0,0,0,0,0};
int [ ]y={0,0,0,0,0,0,0,0,0,0};
↑代表有10個座標
程式碼:

int []x={0,0,0,0,0,0,0,0,0,0};
int []y={0,0,0,0,0,0,0,0,0,0};
int n=0;
void setup()
{
  size(300,200);
}
void draw(){
  background(255);
  for(int i=0;i<n;i++){///用迴圈
  circle(x[i],y[i], 30);
  y[i]+=3;
  if(y[i]>200-15) y[i]=200-15;
    }
  }
  void mousePressed(){
    x[n]=mouseX;
    y[n]=mouseY;
    n++;
  }
==============================================================
氣球

讓氣球往上飄
程式碼:
int x=200,y=200;
PImage img;
void setup(){
    size(500,500);
    img = loadImage("balloon.png");
 }
void draw(){
   background(255);
   image(img,x,y,100,150);
   y--;
   if(y<0) y=0;
}
如何插入圖片?


直接將圖片拉到視窗內即可加入圖片至程式中
==============================================================

多個氣球往上飄


int []x={0,0,0,0,0,0,0,0,0,0};
int []y={0,0,0,0,0,0,0,0,0,0};
int n=0;

PImage img;
void setup(){
    size(500,500);
    img = loadImage("balloon.png");
 }
void draw(){
   background(255);
   for(int i=0;i<n;i++){ ///使用迴圈的方式
   image(img,x[i],y[i],100,150);
   y[i]-=1;
   if(y[i]<0) y[i]=0;
   }
}
void mousePressed(){
       x[n]=mouseX;
       y[n]=mouseY;
       n++;
}

沒有留言:

張貼留言