2018年10月4日 星期四

W04 陳泓丞的課堂筆記

期中作品:Ball Control 不知道大家有沒有玩過類似的遊戲,畫面中有幾顆有數字和顏色的球,還有許多只有顏色的球,按下有數字的球,其他跟數字一樣顏色的球會被吸過去,直到數字球的數字歸零,就會提升遊戲等級。我們在期中要發表的就是這遊戲的初階版。

N01

畫出方框及文字,重點為一般圖型畫法是由座標點往右下方畫,文字則為右上

void setup()
{
  size(800,800); 
}
void draw()
{
  fill(0,0,0);
  rect(100,100,200,90);
  textSize(50);
  fill(255,255,255);
  text("ImIndia",110,160);
}



N02

按下任意按鍵會發射子彈的程式碼

*缺點是只有一顆子彈,按一次就沒了

void setup()
{
  size(800,800); 
}
void draw()
{
  background(255);
  fill(255,0,0);  rect(100,100,200,90);
  textSize(50);
  fill(255,255,255);  text("ImIndia",110,160);
  fill(0,0,255);
  if(BFly)
  {
    ellipse(BX,BY,25,25);
    BX+=3;
  }
}
float BX=100, BY=100;
boolean BFly=false;
void keyPressed()
{
  BFly=true;
}



N03

可自訂義子彈數量的程式碼

* [ ] 是陣列的意思

*缺點是按到子彈數量上限便會當機

void setup()
{
  size(800,800); 
}
void draw()
{
  background(255);
  fill(255,0,0);  rect(100,100,200,90);
  textSize(50);
  fill(255,255,255);  text("ImIndia",110,160);
  for(int i=0;i<BN;i++)
  {
    fill(0,0,255);
    if(BFly[i])
    {
      ellipse(BX[i],BY[i],25,25);
      BX[i]+=3;
    }
  }
}
float []BX=new float[50];
float []BY=new float[50];
int BN=0;
boolean []BFly=new boolean[50];
void keyPressed()
{
  BFly[BN]=true;
  BX[BN]=50;
  BY[BN]=50;
  BN++;
}



N04

讀入圖檔,並可以用滑鼠在任意位置發射

PImage imgBG,imgCH,imgBALL;
void setup()
{
  size(800,600);
  imgBG = loadImage("bg.jpg");
  imgCH = loadImage("cha.png");
  imgBALL = loadImage("ball.png");
  imageMode(CENTER);
}
void draw()
{
  image(imgBG, width/2, height/2, width, height);
  image(imgCH, mouseX, mouseY, 100, 100);
  for(int i=0; i<BN;i++)
  {
    if(BFly[i])
    {
      image(imgBALL, BX[i], BY[i], 100, 100);
      BX[i]+=5;
    }
  }
}
float [] BX = new float[100];
float [] BY = new float[100];
boolean [] BFly = new boolean[100];
int BN=0;
void mousePressed()
{
  BFly[BN]=true;
  BX[BN]=mouseX;
  BY[BN]=mouseY;
  BN++;
}



N05

更改程式碼,使其變為有重力的程式

PImage imgBG,imgCH,imgBALL;
void setup()
{
  size(800,600);
  imgBG = loadImage("bg.jpg");
  imgCH = loadImage("cha.png");
  imgBALL = loadImage("ball.png");
  imageMode(CENTER);
}
void draw()
{
  image(imgBG, width/2, height/2, width, height);
  image(imgCH, mouseX, mouseY, 100, 100);
  for(int i=0; i<BN;i++)
  {
    if(BFly[i])
    {
      image(imgBALL, BX[i], BY[i], 50, 50);
      BY[i]+=BVY[i];
      BVY[i]+=0.98;
    }
  }
}
float [] BX = new float[100];
float [] BY = new float[100];
float [] BVY = new float[100];
boolean [] BFly = new boolean[100];
int BN=0;
void mousePressed()
{
  BFly[BN]=true;
  BX[BN]=mouseX;
  BY[BN]=mouseY;
  BVY[BN]=-20;
  BN++;
}

沒有留言:

張貼留言