2018年9月27日 星期四

QAQ筆記 WEEK03

1.酷炫的六角形


程式碼如下:
void setup()
{
  size(600,600);
}
void draw()
{
  ellipse(300,300,540,540);
  for(float angle=0; angle<PI*2; angle+=PI/3)
  {
    for(int R=0; R<280; R+=10)
    {
      noFill();
      triangle(300,300,300+R*cos(angle),300+R*sin(angle),300+R*cos(angle+PI/3),
                   300+R*sin(angle+PI/3));
    }
  }
}


2.很多小三角形



程式碼如下:
void setup()
{
  size(800,600);
}
void myTriangle(int cx, int cy)
{
  float a=-PI/2;
  for(int R=15; R>0; R-=5)
  {
    if(R%2==0){
      stroke(0,150,100);
      triangle(cx+R*cos(a),cy+R*sin(a),cx+R*cos(a+2*PI/3),cy+R*sin(a+2*PI/3),
                   cx+R*cos(a-2*PI/3),cy+R*sin(a-2*PI/3));
    }
    else{
      stroke(0,0,200);
      triangle(cx+R*cos(a),cy+R*sin(a),cx+R*cos(a+2*PI/3),cy+R*sin(a+2*PI/3),
                   cx+R*cos(a-2*PI/3),cy+R*sin(a-2*PI/3));
    }
  }
}
void draw()
{
  background(255);
  for(int cx=20; cx<800; cx+=45)
  {
    for(int cy=20; cy<600; cy+=45)
    {
      myTriangle(cx,cy);
    }
  }
}


3.可以用鍵盤上下左右控制圖片移動

程式碼如下:
PImage imgMario;
float marioX=0, marioY=0;
void setup()
{
  size(800,600);
  imgMario = loadImage("mario.jpg");
}
void draw()
{
  background(255);
  image(imgMario, marioX,marioY, 200,200);
}
void keyPressed()
{
  if(keyCode==UP)     marioY-=5;
  if(keyCode==DOWN)   marioY+=5;
  if(keyCode==LEFT)   marioX-=5;
  if(keyCode==RIGHT)  marioX+=5;

}


4.像螢幕保護程式一樣讓圖片彈跳


程式碼如下:
PImage imgMario;
float marioX=0, marioY=0, marioVX=2, marioVY=0, marioAX=0, marioAY=0.1;
void setup()
{
  size(800,600);
  imgMario = loadImage("mario.jpg");
}
void draw()
{
  image(imgMario, marioX,marioY, 200,200);
  marioX += marioVX; marioVX += 0;
  marioY += marioVY; marioVY += 0.98;
  if(marioY>400)
  {
    marioVY = - marioVY * 0.8;
    marioY=400;
  }
  if(marioX>600)  marioVX = -marioVX;
}

沒有留言:

張貼留言