2020年3月27日 星期五

程式設計Week04

發射子彈基本架構

程式碼:

PImage imgBG;
int [ ]x={0,0,0,0,0};
int [ ]y={0,0,0,0,0};
int n=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],20);
    }
}

void mousePressed( )
{
  x[n]=mouseX;
  y[n]=mouseY;
  n=(n+1)%5;  //可回收子彈,從第一顆重複利用
}



子彈擴張

程式碼:

PImage imgBG;
int [ ]x={0,0,0,0,0,0,0,0};
int [ ]y={0,0,0,0,0,0,0,0};
int [ ]r={5,5,5,5,5,5,5,5};   //子彈擴張
int n=0;

void setup( )
{
  size(348,580);
  imgBG=loadImage("background.jpg");
}

void draw( )
{
  background(imgBG);
  for(int i=0;i<8;i++)
  {
    circle(x[i],y[i],r[i]);
  }
  if(mousePressed)r[now]++;  //滑鼠按壓,子彈擴大
}
int now=0;

void mousePressed()

{

  x[n]=mouseX;

  y[n]=mouseY;

  r[n]=8;  //還原大小

  now=n;

  n=(n+1)%8;

}


泡泡式子彈

程式碼:
PImage imgBG;
int [ ]x={0,0,0,0,0,0,0,0};
int [ ]y={0,0,0,0,0,0,0,0};
int [ ]r={5,5,5,5,5,5,5,5};
int n=0;

void setup( )
{
  size(348,580);
  imgBG=loadImage("background.jpg");
}

void draw( )
{
  background(imgBG);
  for(int i=0;i<8;i++)
  {
    circle(x[i],y[i],r[i]);
    y[i]--;                                        //子彈往前射擊
  }
  if(mousePressed)r[now]+=2;  //加速子彈擴大速度
}
int now=0;

void mousePressed( )
{
  x[n]=mouseX;
  y[n]=mouseY;
  r[n]=8;
  now=n;
  n=(n+1)%8;
}


美化子彈


程式碼:
PImage imgBG;
int [ ]x=new int[1000];  //另一種矩陣宣告方法 new int[1000];
int [ ]y=new int[1000];
int [ ]r=new int[1000];
color [ ]c=new color[1000];  //設顏色矩陣color

int n=0;

void setup( )
{
  size(348,580);
  imgBG=loadImage("background.jpg");
}

void draw( )
{
  background(imgBG);
  for(int i=0;i<n;i++)
  {
    fill(c[i]);  //上顏色
    circle(x[i],y[i],r[i]);
    y[i]--;
  }
  if(mousePressed)r[now]+=2;
}
int now=0;

void mousePressed( )
{
  x[n]=mouseX;
  y[n]=mouseY;
  r[n]=8;
  c[n]=color(random(255),random(255),random(255));  //c[ ]矩陣挑選顏色
  now=n;
  n++;
}


陣列複習"畫格子"

程式碼:
int [ ][ ]a={{0,1,0,0,0},{0,0,1,1,1,},{1,0,1,0,0},{1,0,0,1,1},{0,1,0,0,1}};

//設一個5x5的陣列

void setup( )
{
  size(500,500);
}

void draw( )
{
   for(int i=0;i<5;i++)
  {
      for(int j=0;j<5;j++)
     {
        if(a[i][j]= =1) fill(255,0,0);
        else fill(128);
        rect(j*100,i*100,500,500);  //j對應橫向X軸, i對應直向Y軸
      }
   }
}


沒有留言:

張貼留言