顯示具有 00160804楊澤宇 標籤的文章。 顯示所有文章
顯示具有 00160804楊澤宇 標籤的文章。 顯示所有文章

2014年1月12日 星期日

期末作品 神奇小筆

神奇小筆

三種顏色的筆
紅色鋪路,藍色檔烏龜,綠色擋香菇
60秒會碰到公主及過關







<arduino>

int switchStateRed = 0;
int switchStateBlue = 0;
int switchStateGreen = 0;


void setup(){
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);

  Serial.begin(9600);
}

void loop(){
  switchStateBlue = digitalRead(2);
  switchStateGreen = digitalRead(3);
  switchStateRed = digitalRead(4);

  if(switchStateRed == HIGH) {
    //Serial.write(3);
    Serial.print(3);
  }
  if(switchStateGreen == HIGH) {
    //Serial.write(2);
    Serial.print(2);
  }
  if(switchStateBlue== HIGH) {
    //Serial.write(1);
    Serial.print(1);
  }

  delay(100);
}

<processing>


import processing.serial.*;
Serial serial;

import ddf.minim.*;
Minim minim;
AudioPlayer star;

PImage imgBG, imgGreen, imgBlue, imgRed, cloudA, cloudB;
PImage man, man1, man2, man3, man4, wife;
PImage mon1, mon2, winImage,paint;
int index;
boolean Game=false;
int wifeX=640, wifeY=250;
boolean wifeF=false;
int cloudX[]= {
  640, 800, 900, 1000
};
int cloudY[]= {
  100, 150, 80, 75
};
int cloudType[]= {
  1, 2, 1, 1
};

int[] I = {
  -1282, -1281, -1280, -1279, -1278, 
  -642, -641, -640, -639, -638, 
  -2, -1, 0, 1, 2, 
  638, 639, 640, 641, 642, 
  1279, 1278, 1280, 1281, 1282
};

int[] I2 = {
  -1279, -1278, -1277, -1276, -1275, 
  -639, -638, -637, -636, -635, 
  1, 2, 3, 4, 5, 
  641, 642, 643, 644, 645, 
  1281, 1282, 1283, 1284, 1285
};

int hole=100;
int HTime=300;

void setup() {
  serial=new Serial(this, "COM3", 9600);
  minim = new Minim(this);
  star=minim.loadFile("Mario.mp3");
  music();
  size(640, 480);
  man=loadImage("man.png");
  man1=loadImage("man1.png");
  man2=loadImage("man2.png");
  man3=loadImage("man3.png");
  man4=loadImage("man4.png");
  mon1=loadImage("air.png");
  mon2=loadImage("mon.png");  
  winImage=loadImage("win.jpg");
  wife=loadImage("wife.png");
  cloudA=loadImage("cloud.png");
  cloudB=loadImage("cloudB.png");
  paint=loadImage("pen.png");
  imgBG=createImage(640, 480, RGB);
  imgGreen=createImage(640, 480, RGB);
  imgRed=createImage(640, 480, RGB);
  imgBlue=createImage(640, 480, RGB);

  clBK();
  clPen(imgGreen);
  clPen(imgRed);
  clPen(imgBlue);

  road();
}
int mon1X[]= {
  640, 800
};
int mon1Y[]= {
  210, 250
};
int manX=200, manY=250;
int mon2Y=-100;

int S=0, S2=0;
int Color=0;
boolean death=false;
int dlong=80;//if or not fall
int s, read;
boolean win=false;
boolean lossmousic=false;

void draw() {
  noCursor();
  background(imgBG);
  clBK();

  if (serial.available()>0)
  {
    read=serial.read();
    println(read);
    if (read==49)Color=0;
    if (read==50)Color=1;
    if (read==51)Color=2;
  }

  bkDraw();

  if (Game) {
    if (s==0)s=60;
    int s2 = second();
    if (s-1==s2)wifeF=true;
    if (wifeF) {
      image(wife, wifeX, wifeY, 100, 100);
      wifeX--;
      if (wifeX-manX<50) {
        win=true;
        Game=false;
      }
    }
    for (int i=0;i<4;i++) {
      if (cloudType[i]%2==0)image(cloudA, cloudX[i], cloudY[i], 100, 50);
      else image(cloudB, cloudX[i], cloudY[i], 100, 50);
      cloudX[i]--;
      if (cloudX[i]<0)newcloud(i);
    }

    runman();

    for (int i=0;i<2;i++) {
      image(mon1, mon1X[i], mon1Y[i], 100, 100);
      int I=(mon1Y[i]-1)*640+mon1X[i]-1;
      imgBlue.loadPixels();
      for (int j=0;j<100;j++) {
        if (imgBlue.pixels[I]==color(#1B16D6)) {
          mon1X[i]=640+(int)random(5)*50;
          mon1Y[i]=200+(int)random(100);
          clPen(imgBlue);
          break;
        }
        I+=640;
      }
      imgBlue.updatePixels();
      if (dist(manX, manY, mon1X[i], mon1Y[i])<=95) {
        Game=false;
        death=true;
        lossmousic=true;
      }
      mon1X[i]-=3;
    } //tortoise

    image(mon2, 200, mon2Y, 100, 100);
    int J=(mon2Y+100-1)*640+200-1;
    imgGreen.loadPixels();
    for (int j=0;j<100;j++) {
      if (J<0)break;
      if (imgGreen.pixels[J]==color(#297111)) {
        mon2Y=-(int)random(100)-100;
        clPen(imgGreen);
        break;
      }
      J++;
    }

    imgGreen.updatePixels();
    if (dist(manX, manY, 200, mon2Y)<=95) {
      Game=false;
      death=true;
      lossmousic=true;
    }
    mon2Y++;//mushroom

    back();
    boolean fall=true;
    imgRed.loadPixels();
    for (int i=24;i>=0;i--) {
      if (imgRed.pixels[223659+I2[i]]==color(#FF0000)) {
        manY=(223659+I2[i])/640-100;
        fall=false;
        dlong=80;
      }
    }
    if (fall) {
      dlong--;
      if (dlong==0) {
        manY=400;
        Game=false;
        death=true;
        lossmousic=true;
      }
    }


    imgRed.updatePixels();



    if (Color==0)pen(imgBlue, color(#1B16D6));
    else if (Color==1)pen(imgGreen, color(#297111));  
    else pen(imgRed, color(#FF0000));
  }
  else {
    if (win) {
      image(winImage, 0, 0, 640, 480);
    }
    else {
      s = second();
      if (death) {
        image(man4, manX, manY, 100, 100);
        if (lossmousic) {
          star.close(); 
          star=minim.loadFile("loss.mp3");
          music();
        }
      }
      else image(man, manX, manY, 100, 100);
      lossmousic=false;
    }
  }
  
  image(paint,mouseX-10,mouseY-80,100,100);
}

void runman() {
  if (S%20==0)S2++;
  if (S2%3==0)image(man1, manX, manY, 100, 100);
  else if (S2%3==1)image(man3, manX, manY, 100, 100);
  else image(man2, manX, manY, 100, 100);
  S++;//run
}

void mouseClicked() {
  Game=true;
}

void newcloud(int i) {
  cloudX[i]=640+(int)random(100);
  cloudY[i]=(int)random(100);
  cloudType[i]=(int)random(10);
}


void road() {
  imgRed.loadPixels();
  int RI=350*640;
  for (int i=350*640;i<640*480;i++) {
    if (i<352*640)imgRed.pixels[i]=color(#FF0000);
    else imgRed.pixels[i]=color(#AA5D1D);
  }

  imgRed.updatePixels();
}

void back() {
  if (HTime==0) {
    hole=((int)random(2)+1)*100;
    HTime=((int)random(3)+2)*60;
  }
  if (hole<=0) {
    HTime--;
  }
  imgRed.loadPixels();
  for (int i = 1;i<imgRed.pixels.length-1;i++) {
    if ((i+1)%640==0) {
      imgRed.pixels[i]=color(#000000);
      if (i>=224639&&(i-224639)%640==0) {
        if (hole<0) {
          if (i==224639||i==225279)imgRed.pixels[i]=color(#FF0000);
          else imgRed.pixels[i]=color(#AA5D1D);
        }
      }
      continue;
    }
    imgRed.pixels[i]=imgRed.pixels[i+1];
  }
  hole--;
  imgRed.updatePixels();
}

void bkDraw() {
  imgBG.loadPixels();
  imgRed.loadPixels();
  imgBlue.loadPixels();
  imgGreen.loadPixels();
  for (int i = 1;i<imgRed.pixels.length-1;i++) {
    if (imgRed.pixels[i]!=color(#000000))
      imgBG.pixels[i]=imgRed.pixels[i];
    if (imgBlue.pixels[i]!=color(#000000))
      imgBG.pixels[i]=imgBlue.pixels[i];
    if (imgGreen.pixels[i]!=color(#000000))
      imgBG.pixels[i]=imgGreen.pixels[i];
  }
  imgBG.updatePixels();
  imgRed.updatePixels();
  imgBlue.updatePixels();
  imgGreen.updatePixels();
}

void clBK() {
  imgBG.loadPixels();
  for (int i = 0;i<imgBG.pixels.length;i++) {
    imgBG.pixels[i]=color(#18DBEA);
  }
  imgBG.updatePixels();
}
void clPen(PImage img) {
  img.loadPixels();
  for (int i = 0;i<imgBG.pixels.length;i++) {
    img.pixels[i]=color(#000000);
  }
  img.updatePixels();
}

void pen(PImage img, color c) {
  index=(mouseY-1)*640+mouseX-1;
  img.loadPixels();
  int I2;
  for (int i=0;i<25;i++) {
    I2=index+I[i];
    if (mouseX>=638||mouseY<=2||mouseX<=2||mouseY>=478)break;
    img.pixels[I2]=c;
  }
  img.updatePixels();
}
void music() {
  star.loop();
}
void stop() {
  star.close();
  minim.stop();
  super.stop();
}



2013年12月8日 星期日

第十四週

期末企劃:
電腦版的包飯糰遊戲只能用滑鼠玩,我們做成可以手動版的包飯糰遊戲,
遊戲中可以手動按按鈕加各種料,最後再用幾個按鈕模擬捏飯糰的動作。
在時間內包越多飯糰越高分,速度越快分數也越高。



類似的遊戲:





2013年12月1日 星期日

WEEK13 課堂作業


(CH05) 馬達轉轉

#include <Servo.h> Servo myServo; int const potPin = A0; int potVal; int angle; void setup(){ myServo.attach(9); Serial.begin(9600); } void loop(){ potVal = analogRead(potPin); Serial.print("potVal: "); Serial.print(potVal); angle = map(potVal, 0, 1023, 0, 179); Serial.print(", angle: "); Serial.println(angle); myServo.write(angle); myServo.write(angle); delay(15); }


(CH06) 用光發出聲音


int sensorValue;
int sensorLow=1023;
int sensorHigh=0;

const int ledPin=13;

void setup(){
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,HIGH);

  while(millis()<5000){
    sensorValue=analogRead(A0);
    if(sensorValue>sensorHigh){
      sensorHigh=sensorValue;
    }
    if(sensorValue<sensorLow){
      sensorLow=sensorValue;
    }
  }
  digitalWrite(ledPin,LOW);
}

void loop(){
  sensorValue=analogRead(A0);
  int pitch=map(sensorValue,sensorLow,sensorHigh,50,4000);
  tone(8,pitch,20);
  delay(10);
}





(CH07) 按鈕聲音


int notes[]={262,294,330,349};

void setup(){
  Serial.begin(9600);
}
void loop(){
  int keyVal=analogRead(A0);
  Serial.println(keyVal);
  if(keyVal==1023){
    tone(8,notes[0]);
  }
  else if(keyVal>=800 && keyVal<=1021){
    tone(8,notes[1]);
  }
  else if(keyVal>=400&& keyVal<=515){
    tone(8,notes[2]);
  }
  else if(keyVal==641){
    tone(8,notes[3]);
  }
  else {
    noTone(8);
  }
}



2013年11月24日 星期日

Week12 鄭羽婷

<HW1 依照溫度亮燈>


const int sensorPin=A0;
const float baselineTemp=22.0;

void setup(){
  Serial.begin(9600);
  for(int pinNumber=2;pinNumber<5;pinNumber++){
    pinMode(pinNumber,OUTPUT);
    digitalWrite(pinNumber,LOW);
  }
}

void loop(){
  int sensorVal=analogRead(sensorPin);
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);
  float voltage=(sensorVal/1024.0)*5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  Serial.print(", degree C: ");
  float temperature=(voltage-.5)*100;
  Serial.println(temperature);

  if(temperature<baselineTemp){
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  else if(temperature>=baselineTemp+2 && temperature< baselineTemp+4){
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);

  }
  else if(temperature>=baselineTemp+4 && temperature<baselineTemp+6){
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);

  }

  else if(temperature>=baselineTemp+6){
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);

  }
  delay(1);

}


<HW2 LED顏色變換>


const int greenLEDPin = 9; const int redLEDPin = 11; const int blueLEDPin = 10;
const int redSensorPin = A0; const int greenSensorPin = A1; const int blueSensorPin = A2;
int redValue = 0; int greenValue = 0; int blueValue = 0;
int redSensorValue = 0; int greenSensorValue = 0; int blueSensorValue = 0;
void setup(){ Serial.begin(9600); pinMode(greenLEDPin,OUTPUT); pinMode(redLEDPin,OUTPUT); pinMode(blueLEDPin,OUTPUT); } void loop(){ redSensorValue = analogRead(redSensorPin); delay(5); greenSensorValue = analogRead(greenSensorPin); delay(5); blueSensorValue = analogRead(blueSensorPin); Serial.print("Raw Sensor Values \t Red: "); Serial.print(redSensorValue); Serial.print("\t Green: "); Serial.print(greenSensorValue); Serial.print("\t Blue: "); Serial.println(blueSensorValue); redValue = redSensorValue/4; greenValue = greenSensorValue/4; blueValue = blueSensorValue/4; Serial.print("Mapped Sensor Values \t Red: "); Serial.print(redValue); Serial.print("\t Green: "); Serial.print(greenValue); Serial.print("\t Blue: "); Serial.println(blueValue); analogWrite(redLEDPin, redValue); analogWrite(greenLEDPin, greenValue); analogWrite(blueLEDPin, blueValue); }


2013年11月17日 星期日

第11週 課堂作業

1. 練習第一個範例 File-Examples-Basic-Blink

2. 修改程式,變成展得比較特別的作法



void setup(){
 pinMode(13, OUTPUT);  //13號孔的燈
}
void loop(){
  digitalWrite(13, HIGH);
  delay(200); //只亮一下
  digitalWrite(13, LOW);
  delay(200);
  
  digitalWrite(13, HIGH);
  delay(1000); //亮很久
  digitalWrite(13, LOW);
  delay(200);
}




課堂練習:


2013年11月10日 星期日

第九週 課堂作業

1.時間倒數和計分系統




2.有加速機制

PImage imgball, imgtrash, imgbomb, imgbasket;

int N = 4;
float []ballX = new float[N], ballY = new float[N], ballType = new float[N];
int a=0, b=0;

void setup() {
  size(600, 600);
  imgball = loadImage("baseketball.png");
  imgtrash = loadImage("trash.png");
  imgbomb = loadImage("bomb.png");
  imgbasket = loadImage("basket.png");

  for (int i = 0; i < N ; i++) {
    newBall(i);
  }
}
int countDown = 4*40;
int countDown2 = 60*40;
void draw() {
  if ( countDown > 0 ) {
    background(255, 0, 0);
    countDown--;
    textSize(60);
    text("READY"+ countDown/40, 200, 250);
    return ;
  }
  if ( b == 0 && countDown2 > 0 ) {
    countDown2--;

    background(#2B85DE);
    for (int i=0 ; i<N ; i++) {
      if ( ballType[i] == 1 ) drawGood(i);
      else if (ballType[i] == 2 ) drawBad(i);
      else if (ballType[i] == 3 ) drawTrash(i);
     
      if( countDown2/40 >= 40 ){
        ballY[i] += 4;
      }
      else if( countDown2/40 >= 30 ){
        ballY[i] += 6;
      }
      else if( countDown2/40 >= 15 ){
        ballY[i] += 8;
      }
      else if( countDown2/40 >= 15 ){
        ballY[i] += 12;
      }
      else
        ballY[i] += 14;
     

      if ( ballY[i] >= 500 && ballY[i] <= 512 && ballX[i] >= (mouseX-50) && ballX[i] <= mouseX+50 ) {
        ballY[i] = 601;
        if ( ballType[i] == 1 ) {
          background(255, 0, 0);
          a++;
        }
        if ( ballType[i] == 2 ) {
          b=1;
        }
        if ( ballType[i] == 3 ) {
          a--;
        }
      }
      fill(0);
      textSize(32);
      text("SCORE:", 410, 40);
      fill(255, 0, 0);
      textSize(45);
      text(a, 530, 45);
     
      fill(0);
      textSize(45);
      text("Time:"+ countDown2/40, 0, 50);

      if ( ballY[i] > 600 ) newBall(i);
    }
    image( imgbasket, mouseX, 500, 50, 50);
    //image(imgbasket, basketX, basketY, 50, 50); // keyboard control

    //}
  }
  else {
    background(0);
    fill(255);
    textSize(80);
    text("GAME", 200, 180);
    text("OVER", 200, 280);
    fill(240, 0, 0);
    text("grade:", 140, 380);
    text(a, 410, 380);
  }
}
/*void keyPressed(){
 if(keyCode == UP) basketY -= 10;
 if(keyCode == DOWN) basketY += 10;
 if(keyCode == RIGHT) basketX += 10;
 if(keyCode == LEFT) basketX -= 10;
 }*/
void newBall(int i) {
  ballY[i] = -random(500)+100;
  ballX[i] = int(random(10))*60;
  ballType[i] = int(random(3))+1;
}
void drawGood(int i) {
  image( imgball, ballX[i], ballY[i], 50, 50);
}
void drawBad(int i) {
  image( imgbomb, ballX[i], ballY[i], 50, 50);
}
void drawTrash(int i) {
  image( imgtrash, ballX[i], ballY[i], 50, 50);
}


第八週 課堂作業

1.起始畫面  倒數計時





2.結束畫面 game over



week10 鄭羽婷示範, 期中作品展示

1. 把大家的期中作品準備好, 要在老師的電腦跑展示(廣播並票選)
2. 今天要上傳影片 (自己錄好,上傳youtube, 留下連結) (仿上學期電腦圖學課的期末作品)

=====
3. 發 Arduino Starter Kit (包含各式電子零件,及Arduino UNO板) 及 Arduino Mega 2560板

介紹:
轉珠遊戲!!贏書有兩種結果!!結束後會自動跳回起始畫面!!
不過因為玩的人有點爛!!所以它分成了兩部影片!!哈哈




import ddf.minim.*;
Minim minim;
AudioPlayer star;
int a=1;

int Xmax=7, Ymax=5;
int c[][]=new int [Xmax][Ymax];//chose color index
int Color[]=new int [5];//Chose color
int test=1, t;//read mouse event in first time
int X=-1, Y=-1;//=-1 loop unMousePressed don't do that
int Cx, Cy;//change ball X &Y value
int TC;//record move ball color

int N[][]=new int [Xmax][Ymax];
boolean clear=false;
int n;
boolean MoveBall=true;

int sum[]=new int [5];
boolean attack=false;
boolean Ustop=true;
int Blood=360, MBlood=360;

PImage background, ATTACK, win, lose, P1, P2, P3, P4, P5;
PImage play1, play2;

void setup() {
  minim = new Minim(this);

  background= loadImage("0.jpg");
  ATTACK=loadImage("attack.jpg");
  win=loadImage("win.jpg");
  lose=loadImage("lose.jpg");
  P1=loadImage("a.jpg");
  P2=loadImage("b.jpg");
  P3=loadImage("c.jpg");
  P4=loadImage("d.jpg");
  P5=loadImage("e.jpg");
  play1=loadImage("play.png");
  play2=loadImage("play2.png");
  size(400, 600);
  Color[0]=#E51E1E;
  Color[1]=#201EE5;
  Color[2]=#F6FF00;
  Color[3]=#03FF00;
  Color[4]=#5D1E9D;
  for (int i=0;i<Xmax;i++) {
    for (int j=0;j<Ymax;j++) {
      c[i][j]=(int)random(5);
    }
  }
}

int AT=8;
boolean Win=false;
boolean Loss=false;
boolean stage=false;

int Time=20;

void draw() {

  if (stage) {
    if (Win||Loss) {
      if (Win){image(win, 0, 0);}
      else {image(lose, 0, 0);}
   
      if(a==3){star.close();star=minim.loadFile("c.mp3");music();}
      if(a==4){star.close();star=minim.loadFile("d.mp3");music();}
      a=1;
      if (Time<0) {
        star.close();
        Time=10;
        AT=8;
        Win=false;
        Loss=false;
        stage=false;

        for (int i=0;i<Xmax;i++) {
          for (int j=0;j<Ymax;j++) {
            c[i][j]=(int)random(5);
          }
        }

        Blood=360;
        MBlood=360;
        clear=false;
        MoveBall=true;
        attack=false;
        Ustop=true;
      }
      Time--;
    }

    else {
      if(a==2){star.close();star=minim.loadFile("b.mp3");music();}
      a=3;
      image(background, 0, 0);
      image(P1, 5, 210);
      image(P2, 85, 210);
      image(P3, 165, 210);
      image(P4, 245, 210);
      image(P5, 325, 210);

      stroke(0);
      drawBall();


      strokeCap(ROUND);
      if (attack) {

        if (AT>6) {
          noFill();
          for (int i=0;i<5;i++) {
            stroke(Color[i]);
            strokeWeight(10);
            if (sum[i]!=0)
              bezier(150+i*20, 80, i*80+20, 25, i*80+20, 100, i*80+40, 200);
          }
        }

        else {
          if (AT>4) {
            for (int i=0;i<5;i++) {
              Blood-=sum[i]*2;
              sum[i]=0;
            }

            if (Blood<0) {
              Blood=0;
              Win=true;
            }
          }
          else {
            if (AT>2) {
              image(ATTACK, 50, 5);
            }
            else if (AT>1) {
              MBlood-=(random(20)+40);
              if (MBlood<0) {
                MBlood=0;
                Loss=true;
                a=4;
              }
            }
          }
        }
        AT--;
        if (AT<=0) {
          attack=false;
        }
      }
      strokeWeight(12);
      stroke(#C10A0A);
      strokeCap(PROJECT);
      line(20, 10, 20+Blood, 10);
      line(20, 320, 20+MBlood, 320);

      strokeWeight(5);
      stroke(#52091A);
      fill(#F041B6);
      ellipse(20, 10, 25, 25);
      ellipse(20, 320, 25, 25);


      stroke(0);
      if (Ustop) {
        if (MoveBall==true&&attack==false) {
          frameRate(20);
          if (mousePressed==true) {
            fill(Color[TC]);
            ellipse(mouseX, mouseY, 40, 40);
            Cx=mouseX/10*10/50-1;
            Cy=(mouseY-300)/10*10/50-1;
            if (mouseX%50>=25&&mouseX%50<50)Cx+=1;
            if ((mouseY-300)%50>=25&&(mouseY-300)%50<50)Cy+=1;
            if (mouseX>350)Cx=6;//range
            if (mouseY>550)Cy=4;
            if (mouseX<50)Cx=0;
            if (mouseY<350)Cy=0;
            if (Cx!=X||Cy!=Y) {//not the same region
              c[X][Y]=c[Cx][Cy];
              X=Cx;
              Y=Cy;
            }
          }
        }
        else {
          frameRate(3);
          if (n%3==0)score();
          else if (n%3==1)fallBall();
          else {
            addBall();
            Ustop=false;
          }
          n++;
          test();
        }
      }
      else {
        score();
        Ustop=true;
        test();
        if (MoveBall)attack=true;
      }
    }
  }

  else {
    background(#13D5E5);
    image(play1, 150, 150);
    image(play2, 150, 280);
    if (mousePressed==true) {
      if (abs(mouseX-200)<100&&abs(mouseY-300)<100)
        stage=true;
    }

      if(a==1){star=minim.loadFile("a.mp3");music();}
      a=2;
  }
}

void music(){
  star.loop();

}

void stop() {
  star.close();
  minim.stop();
  super.stop();
}

void mousePressed() {
  if (t==0) {
    X=mouseX/10*10/50-1;
    Y=(mouseY-300)/10*10/50-1;
    if (mouseX%50>=25&&mouseX%50<50)X+=1;
    if ((mouseY-300)%50>=25&&(mouseY-300)%50<50)Y+=1;
    if (mouseX>350)X=6;//range
    if (mouseY>550)Y=4;
    if (mouseX<50)X=0;
    if (mouseY<350)Y=0;
    TC=c[X][Y];
  }
  t=test;//read mosue event on first time
  AT=8;
}
void mouseReleased() {
  c[X][Y]=TC;
  X=-1;
  Y=-1;
  t=0;
  n=0;
  MoveBall=false;
}

void test() {
  MoveBall=true;
  for (int i=0;i<Xmax;i++) {
    for (int j=0;j<Ymax;j++) {
      if (N[i][j]!=0)MoveBall=false;
    }
  }
}

void drawBall() {

  for (int i=0;i<Xmax;i++) {
    for (int j=0;j<Ymax;j++) {
      if (i==X&&j==Y)continue;//mousePressed do
      if (N[i][j]!=0)continue;
      fill(Color[c[i][j]]);
      ellipse((i+1)*50, (j+1)*50+300, 40, 40);
    }
  }
}

void addBall() {
  for (int i=0;i<Xmax;i++) {
    for (int j=0;j<Ymax;j++) {
      if (N[i][j]==6) {
        c[i][j]=(int)random(5);
        N[i][j]=0;
      }
    }
  }
}

void fallBall() {
  for (int i=0;i<Xmax;i++) {
    for (int j=0;j<Ymax;j++) {
      if (N[i][j]!=0) {
        for (int k=j;k>0;k--) {
          c[i][k]=c[i][k-1];
          N[i][k]=N[i][k-1];
        }
        N[i][0]=6;
      }
    }
  }
}

void score() {
  int count=0, count2=0;

  for (int i=0;i<Xmax;i++) {
    for (int j=0;j<Ymax;j++) {

      for (int k=i+1;k<Xmax;k++) {
        if (c[i][j]==c[k][j])count++;
        else break;
      }

      for (int k=j+1;k<Ymax;k++) {
        if (c[i][j]==c[i][k])count2++;
        else break;
      }

      if (count>1) {
        for (int k=i;k<=i+count;k++) {
          N[k][j]=c[i][j]+1;
          sum[c[i][j]]++;
        }
      }

      if (count2>1) {    
        for (int k=j;k<=j+count2;k++) {
          N[i][k]=c[i][j]+1;
          sum[c[i][j]]++;
        }
      }
      count=0;
      count2=0;
    }
  }
}

2013年10月20日 星期日

第七週 課堂作業

期中作品預計進度:

1. 這禮拜要寫出下雨
2. 寫出接金幣有分數金幣掉到地上會消失
3. 因為這是國民遊戲


課堂進度:

完成隨機掉下的三種物件,還有可以用滑鼠或鍵盤控制接住東西的網子。


下週進度:

當接到物件時,會算分,接到垃圾會扣分,接到炸彈結束。


2013年10月13日 星期日

第六週 課堂作業

1.特訓99程式第一步



2.特訓99程式第二步


3.加入飛機



期中進度:
這週先定義圖片,建立陣列。



2013年10月6日 星期日

第五週 課堂作業

1.使用 translate(), rotate() 涵式,搭配 for迴圈




2.花跟著滑鼠移動,且隨著位置改變顏色
   translate(mouseX, mouseY);
   fill(mouseX, mouseY, 480);



3.滑鼠按一次就印上一朵花,用陣列存100朵花




期中作品計畫:
預計做一個接東西的遊戲,會有許多物件從上面隨機掉下,控制下面的主角接住掉下的東西,並閃躲會砸到的東西
有時間倒數計時,和記分板、配樂



2013年9月30日 星期一

第四週 課堂作業

課堂目標: 小畫家

程式碼:

void setup() {
  size(800, 800);
  background(255);

  //設定顏色選取方塊
  noStroke();
  fill(#FF0900); //顏色用代碼,不受 RGB或 HSB影響
  rect(0, 0, 100, 100);
  fill(#FF7300);
  rect(0, 100, 100, 100);
  fill(#FFEA00);
  rect(0, 200, 100, 100);
  fill(#36FF00);
  rect(0, 300, 100, 100);
  fill(#00FFE8);
  rect(0, 400, 100, 100);
  fill(#001BFF);
  rect(0, 500, 100, 100);
  fill(#8B00FF);
  rect(0, 600, 100, 100);

  //color Mode調色盤
  noStroke(); //去除邊線
  colorMode(HSB, 100); // (調色盤模式, 最大值MAX) 
  for (int i = 0; i < 100; i++) {
    for (int j = 700; j < 800; j++) {
      stroke(i, j, 100); // (i, j, alpha)
      point(i, j);
    }
  }

  //筆粗
  stroke(0);
  fill(255);
  rect(700, 0, 100, 100);
  fill(0);
  strokeWeight(30);
  stroke(0);
  point(750, 50);

  stroke(0);
  strokeWeight(1);
  fill(255);
  rect(700, 100, 100, 100);
  fill(0);
  strokeWeight(15);
  stroke(0);
  point(750, 150);

  stroke(0);
  strokeWeight(1);
  fill(255);
  rect(700, 200, 100, 100);
  fill(0);
  strokeWeight(3);
  stroke(0);
  point(750, 250);

  //存檔
  strokeWeight(1);
  fill(255);
  rect(700, 300, 100, 100);
  fill(0);
  text("save image", 720, 350);
}
void draw() {
}
void mouseDragged() {
  //畫線
  if (mouseX>100){
    if(mouseX<700 || mouseY>400){
      line( mouseX, mouseY, pmouseX, pmouseY );
    }
  }
}
void mouseClicked() {
  //選擇顏色
  if (mouseX<100 && mouseY<100) stroke(#FF0900);
  else if (mouseX<100 && mouseY<200) stroke(#FF7300);
  else if (mouseX<100 && mouseY<300) stroke(#FFEA00);
  else if (mouseX<100 && mouseY<400) stroke(#36FF00);
  else if (mouseX<100 && mouseY<500) stroke(#00FFE8);
  else if (mouseX<100 && mouseY<600) stroke(#001BFF);
  else if (mouseX<100 && mouseY<700) stroke(#8B00FF);
  else if (mouseX<100 && mouseY<800) stroke(mouseX, mouseY, 100);

  //選擇粗細
  if (mouseX>700 && mouseY<100) strokeWeight(30);
  else if (mouseX>700 && mouseY<200) strokeWeight(15);
  else if (mouseX>700 && mouseY<300) strokeWeight(3);
  else if (mouseX>700 && mouseY<400) save("practice.png");
}



2013年9月22日 星期日

第三週 課堂作業

課堂目標:畫五子棋盤

1.
使用 Tool- Color Selector, 挑出要的顏色
用fill()修改填充的顏色, noFill()讓圖形變為空心
stroke() 來調畫線stroke的顏色
strokeWeight() 來設定線的權重粗細


2.用for迴圈畫出內部格線,分為橫的X 和直的Y


3.用 mouseMoved 涵式來偵測滑鼠移動的位置
   要設變數 nowX, nowY 
   畫出跟著滑鼠移動的紅色方形


4.點滑鼠,方格變為黑色
   
   設一陣列 array 
   宣告方法 int [][] array = new int [15][15];
   用 mousePressed 涵式來改變陣列的值為 1
   用for迴圈跑完陣列 將值為1的設成黑色   


5.用 if else ,設一個 nowColor變數讓黑白子輪流下


6.調整滑鼠移動位置,讓橢圓形顯示在格線上


7.滑鼠超過陣列範圍會當機
   設定陣列若超過14,則等於14

8.程式碼


int nowColor = 1;// 變數 設定初始值
int nowX, nowY;// 設定變數 現在滑鼠所在的位置在第幾格
int [][] array = new int [15][15]; // 宣告陣列

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

void draw() {
  background(247, 210, 150);// 用tools → color seloector選顏色
  // 亦可用 background(#F7D296);
  
  noFill();// 畫空心圖形
  strokeWeight(6);// 設圖形線的寬度 
  rect( 50, 50, 700, 700);
  
  // 畫內部格線
  strokeWeight(1);
  for(int i = 0 ; i < 800 ; i+=50){
    line( 50, 50 + i, 750, 50 + i);// 畫橫線
    line( 50 + i, 50, 50 + i, 750);// 畫直線
  }

  fill(255,0,0);
  ellipse(nowX*50+50, nowY*50+50, 50, 50);
  
  //用陣列改變顏色 搭配mousePressed涵式
  //fill(0); 原來的做法

  for(int i = 0 ; i < 15 ; i++){
    for(int j = 0 ; j < 15 ; j++){

      if(array[i][j] == 1){
        fill(0);
        ellipse( i*50 + 50, j*50 + 50, 50, 50);
      }

      if(array[i][j] == 2){
        fill(255);
        ellipse( i*50 + 50, j*50 + 50, 50, 50);
      }
    }
  }
}
void mousePressed(){ // 用涵式來改變陣列的值
  
  array[nowX][nowY] = nowColor;
  
  if(nowColor == 1) nowColor =  2;
  else if(nowColor == 2) nowColor =1;

}
void mouseMoved(){

  // 現在滑鼠所在的位置在第幾格
  nowX = (mouseX-25)/50;
  nowY = (mouseY-25)/50;
  if(nowX > 14) nowX = 14;
  if(nowY > 14) nowY = 14;
    
}


中間過程圖:



最後結果圖:


8.課堂相關Processing 影片

processing寫的遊戲

2013年9月15日 星期日

第二週 課堂作業

1. 用雙層 for迴圈畫正方形

void setup(){
  size(600,400);
}

void draw(){
  for(int x = 0 ; x < 600 ; x += 100){
    for(int y = 0 ; y < 400 ; y += 100){
      rect(x,y,100,100);
    }
  }

}




2. 用 Processing-Help-Reference 找 rect() 函式

 看看 rect() 樓上樓下的相關函式, 並嘗試畫出和老師不同的圖

  fill() 是填充的顏色, stroke()是筆的顏色






3.使用dist()涵式 
    離滑鼠越近,圓形越小,反之則否。
      fill(0, 255, 0,50);
      ellipse(x+51,y+51,dist(x,y,mouseX,mouseY)/10+90,dist(x,y,mouseX,mouseY)/10+90);




      離滑鼠越近,越透明
      fill(0, 255, 0,dist(x,y,mouseX,mouseY)*2);
      ellipse(x+51,y+51,90,90);



4.使用 random涵式
     宣告陣列,把顏色先存起來到陣列,固定顏色。

color [][] myColor = new color[6][4];//宣告24個陣列
//int [] array = new int [5]//Java-styel
//int array[5];//C-styel  

void setup(){
  size(600,400);
  for(int x = 0 ; x < 600 ; x += 100){
    for(int y = 0 ; y < 400 ; y += 100){
      //定義好陣列中24個位置的顏色
      myColor[x/100][y/100] = color(random(256), random(256), random(256) ); //用256不用255,是因為256以下的不包含256
    }
  }
}

void draw(){
  for(int x = 0 ; x < 600 ; x += 100){
    for(int y = 0 ; y < 400 ; y += 100){
      fill(255, 0, 0,50);// (r , g , b , alpha透明度) 

      rect(x,y,100,100);
   
      fill(myColor[x/100][y/100]); // 帶入陣列設好的顏色
      triangle(x, y+100,x+50, y, x+100, y+100);
   
      fill(0, 255, 0,dist(x,y,mouseX,mouseY)*2);
      ellipse(x+51,y+51,90,90); //畫圓形
   
    }
  }
}




5.課堂練習
上層綠色的圓形類似遮罩,移動滑鼠增加透明度後,就顯出下層的圖案

void setup() {
  size(600, 400);
}

void draw() {
  for (int x = 0 ; x < 600 ; x += 100) {
    for (int y = 0 ; y < 400 ; y += 100) {
      fill(183, 231, 240);// (r , g , b , alpha透明度) 
      rect(x, y, 100, 100);

      fill(0, 0, 255);
      ellipse(x+31, y+41, 20, 20); //畫圓形
     
      fill(0, 0, 255);
      ellipse(x+71, y+41, 20, 20); //畫圓形
     
      fill(255, 0, 0);
      rect(x+35, y+55, 30, 30 ,0 ,0 ,20 ,20); //rect(a, b, c, d, tl, tr, br, bl) 後四個是圓滑度
     
      fill(0, 255, 0, dist(x, y, mouseX, mouseY));
      ellipse(x+51, y+51, 90, 90); //畫圓形
    }
  }
}



6.課堂心得
老師上課的速度剛剛好,一步一步跟老師做都可以跟的上,也越來越了解processing的使用,希望之後能有更多應用,期末能有更多創新的想法。

2013年9月8日 星期日

第一週 課堂作業

1. 看一下去年學長姐們做的成果 (划船、吃角子老虎、數位手套、直昇機控制搖桿), 聖誔老公公下樓梯
2. 試著執行 Processing
3.下載processing軟體 http://processing.org/
4.線上自學影片 https://class.coursera.org/digitalmedia-001/lecture/index



開始練習Processing:

size(600,600); //設定視窗大小
background(255, 0, 0); //背景顏色
rect(100,100, 200,200); //畫四邊形, 設定位置 大小































秀出網路上的圖片:

PImage imgBird = loadImage("網址");

PImage imgPig = loadImage("http://www.cmlab.csie.ntu.edu.tw/~jsyeh/processing/angryBirdsBrick/pig.gif");

image(imgBird,0,0); //秀出圖片
image(imgPig,200,200,400,400);  //設定位置和大小

















寫成涵式寫法:

PImage imgBird, imgPig; //宣告

void setup() {
  size(800, 800);
  imgBird = loadImage("http://www.cmlab.csie.ntu.edu.tw/~jsyeh/processing/angryBirdsBrick/bird.png");
  imgPig = loadImage("http://www.cmlab.csie.ntu.edu.tw/~jsyeh/processing/angryBirdsBrick/pig.gif");

  imageMode(CENTER); //把圖中心點放在游標上, 所有圖的座標位置都從左上角變到中心點
}

void draw() {
  background(0, 255, 0);
  image(imgBird, mouseX, mouseY);
  image(imgPig, 200, 200, 400, 400);
}