2020年3月27日 星期五

week04

第四周上課內容:

(1)加入背景,可用滑鼠點出五顆子彈



PImage imgBG;   //陣列
int[]x={0,0,0,0,0};
int[]y={0,0,0,0,0};
void setup(){
size(261,435);     //記得要與背景圖相同
imgBG=loadImage("background.jpg");
}
void draw(){
  background(imgBG);
  circle( x[0], y[0],40);
}
void mousePressed(){
  x[0] = mouseX;
  y[0] = mouseY;
}

(2.)設定子彈,不要超過五顆




PImage imgBG;
int[]x={0,0,0,0,0};
int[]y={0,0,0,0,0};
int n=0;  //子彈數目,現在為0顆子彈
void setup(){
 size(261,435);
 imgBG=loadImage("background.jpg");
}
void draw()
{
  background(imgBG);
  for(int i=0;i<5; i++)
{
  circle(x[i], y[i], 40);  //把原來[ ]中的0改成i
 }
}
void mousePressed()
{
  if(n>=5)return;   //不超過五顆
  x[n] = mouseX;  //[ ]的0改為n ,因為印列不夠大,按第六顆就爆,所以n:5 ---->6
  y[n] = mouseY;
  n++;
}
(3)子彈循環移動



PImage imgBG;
int[]x={0,0,0,0,0};
int[]y={0,0,0,0,0};
int n=0;     //子彈數目,用了0顆
void setup(){
 size(261,435);  //尺寸須和背景圖相同
 imgBG=loadImage("background.jpg");
}
void draw()
{
  background(imgBG);
  for(int i=0;i<5; i++)
{
  circle(x[i], y[i], 40);
  y[i]--;  ***
 }
}
void mousePressed()
{
  x[n] = mouseX;
  y[n] = mouseY;
  n = (n+1)%5;  //可收回子彈,下次用最舊的那顆
}


(4)迴圈練習


int []a={1,1,1,0};
size(400,100);
for(int i=0; i<4; i++)  //for迴圈配陣列
{
 if(a[i]==1) fill(255,0,0);  //灰色
 else fill(128); 
 rect(i*100,0, 100,100);
}

//算對應座標

(5)方形迴圈練習



int [][]a={
  {0,1,1,0},
  {0,0,0,1},
  {0,1,1,1},
  {1,1,0,0}};
size(400,400);
for(int i=0; i<4; i++)
{
  for(int j=0;j<4;j++)
  {
 if(a[i][j]==1) fill(255,0,0);
 else fill(128);
 rect(i*100, j*100, 100,100);
}
} //j對應x座標,i對應y座標

沒有留言:

張貼留言