顯示具有 05160053_甘珮妤 標籤的文章。 顯示所有文章
顯示具有 05160053_甘珮妤 標籤的文章。 顯示所有文章

2019年1月5日 星期六

Week14_珮妤的筆記

安裝驅動程式

選擇正確的序列埠




















選擇開發板




















利用processing的範例 SimpleReadProcessing\file\example\libaries\serial\ SimpleRead

















把50行以後的程式碼搬到arduino

Arduino : 把pinMode的INPUT改為PULLUP_INPUT , 把接腳改成2
Processing : 改成 new Serial的第二個參數改成COM7






2019年1月3日 星期四

Week17_珮妤的筆記



利用小葉老師教的內差alpha介於0到1之間,讓星星隨機散出。用搖桿的移動去吃星星,以及加上倒數計時及計分的程式,遊戲的畫面會有逐漸放大的效果,讓玩家會更有身歷其境的感覺。


youtube連結:https://youtu.be/c7O4n_iwt7U

2018年12月27日 星期四

Week16_珮妤的筆記

arduino

processing


import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
import processing.serial.*;
Serial myPort;

float[] zombieX=new float[8];
float[] zombieY=new float[8];
PImage imgZombie,imgBG,img,imgBG2;
int state=0;
int remainTime=0;
int score1=0;
int score2=0;

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

void reborn(int i){
  zombieX[i]=int (random(3))*135;
  zombieY[i]=10;
}

void setup(){
  size(400,600);
  myPort = new Serial(this,"COM7",9600);
  imgBG= loadImage("BG.jpg");
  img= loadImage("shot.jpg");
  imgBG2= loadImage("BG2.png");
  minim = new Minim(this);
  player = minim.loadFile("456.mp3");
  player.play();
  
  imgZombie=loadImage("zombie2.png");
  for(int i=0;i<8;i++){
    reborn(i);
    zombieY[i]=325-i*50;
  }
}
int old1=0, old2=0, old3=0;
int Button1=0,Button2=0,Button3=0;
void draw(){
  if(state==0){
    drawOP();
  }else if(state==1){
    drawPlayer();
  }else if(state==2){
    drawED();
  }
  getData();
}
byte [] data=new byte[3];
void getData(){
 if(myPort.available()>0){
  myPort.readBytes(data);
  old1=Button1;
  old2=Button2;
  old3=Button3;
  Button1=data[0];
  Button2=data[1];
  Button3=data[2];
  println(data);
  int press= -1;
  if(data[0]==1 && old1==0) press=1;
  else if(data[1]==1 && old2==0)press=2;
  else if(data[2]==1 && old3==0)press=3;
  
  if( press==1 && zombieX[0]==0) killOneZombie();
  else if( press==2 && zombieX[0]==135) killOneZombie();
  else if( press==3 && zombieX[0]==270) killOneZombie();
  else if(press!= -1 ) bad();
  
 }
}
void killOneZombie(){
  for(int i=0;i<8-1;i++){
    zombieX[i]=zombieX[i+1];
  }
  reborn(7);
  score1+=200;
  minim = new Minim(this);
  player = minim.loadFile("bang.wav");
  player.play();
}

void bad(){
 background(255,0,0); 
 score2+=100;
}

void keyPressed(){
  if(key=='1' && zombieX[0]==0) killOneZombie();
  else if(key=='2' && zombieX[0]==135) killOneZombie();
  else if(key=='3' && zombieX[0]==270) killOneZombie();
  else if(key=='z' && state==0) {state=1;remainTime=30*60;}
  else bad();
}

void drawOP(){
  image(imgBG2,0,0,width,height);
  fill(#FFFFFF);
  textSize(30); text("Press z to Play",95,335);
  textSize(26); text("Happy Halloween!",90,395);
}

void drawPlayer(){
  image(imgBG,0,0,width,height);
  image(img,0,480,400,120);
  for(int i=7;i>=0;i--){
    image(imgZombie, zombieX[i],zombieY[i]);
  }  
  fill(#FC0317); textSize(40); text((remainTime+59)/60,5,40);
  remainTime--;
  if(remainTime<=0) state=2;
}

void drawED(){
  image(imgBG,0,0,width,height);
  fill(#FC0317); text("    Game End\nPlus Score:"+score1,55,200);
  fill(#FC0317); text(" Minus Score:-"+score2,10,320);
  fill(#FC0317); text("Score:"+(score1-score2),140,445);
}


2018年12月20日 星期四

Week15_珮妤的筆記

Processing

import processing.serial.*;
Serial duankou;
void setup() {
  size(500,500);
  duankou = new Serial(this, "COM3", 9600);
}
int X,a;
void draw() {
  getData();
  background(127);
  println(X);
  if(X<50&&X>0){
    delay(200);
    a+=1;
  ///  println(a);
  }
  fill(255,0,0);textSize(80);text("Score:"+a,100,100);
}
byte [] data=new byte[1];
void getData(){
   if (duankou.available() > 0) {
   duankou.readBytes(data);
   X=data[0];
   println(data);
  }
}

2018年12月6日 星期四

Week13_珮妤的筆記




 int switchPin = 4;                       // Switch connected to pin 4

void setup() {
 // pinMode(switchPin, INPUT);             // Set pin 0 as an input
  pinMode(2,INPUT_PULLUP);
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(2) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

2018年11月29日 星期四

Week12_珮妤的筆記

校外參訪 - Hackerspace


拿100個燈泡


把每個燈泡的腳搬開


焊接起來每個燈泡


串接好的小燈泡,讓燈泡亮起來




大家跟自己製作好的光劍一起拍張大合照





2018年11月22日 星期四

Week11_珮妤的筆記


讓版子發光
void setup() {
  // put your setup code here, to run once:
  for(int i=2;i<=13;i++){
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i=2;i<=13;i++){
    digitalWrite(i, HIGH);
    delay(300);
  }
  for(int i=2;i<=13;i++){
    digitalWrite(i, LOW);
  }
}


會發出DO RE MI FA SOL
int piezoPin = 8;
int length = 15;
char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";      // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration)
{
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration)
{
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  for (int i = 0; i < 8; i++)
  {
    if (names[i] == note)
    {
      playTone(tones[i], duration);
    }
  }
}

void setup()
{
  pinMode(piezoPin, OUTPUT);
}

void loop()
{
  for (int i = 0; i < length; i++)
  {
    if (notes[i] == ' ')
    {
      delay(beats[i] * tempo);
    } else
      playNote(notes[i], beats[i] * tempo);
    delay(tempo / 2);
  }
}


可以播出兩種聲音
int piezoPin = 8;
int button = 2;
int lengths = 26;
char jbnotes[] = "eeeeeeegcde fffffeeeeddedg";
int jbbeats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int length = 15;
char ttlsnotes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int ttlsbeats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration)
{
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration)
{
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  for (int i = 0; i < 8; i++)
  {
    if (names[i] == note)
    {
      playTone(tones[i], duration);
    }
  }
}

void setup()
{
  pinMode(piezoPin, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop()
{
  int sensorVal = digitalRead(button);
  if (sensorVal == HIGH)
  {
    for (int i = 0; i < length; i++)
    {
      if (ttlsnotes[i] == ' ')
      {
        delay(ttlsbeats[i] * tempo);
      }
      else
        playNote(ttlsnotes[i], ttlsbeats[i] * tempo);
      delay(tempo / 2);
    }
  }
  else
  {
    for (int i = 0; i < lengths; i++)
    {
      if (jbnotes[i] == ' ')
      {
        delay(jbbeats[i] * tempo);
      }
      else
        playNote(jbnotes[i], jbbeats[i] * tempo);
      delay(tempo / 2);
    }
  }
}

int piezoPin = 8;
int button = 2;
int lengths = 26;
char jbnotes[] = "eeeeeeegcde fffffeeeeddedg";
int jbbeats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int length = 15;
char ttlsnotes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int ttlsbeats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration)
{
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration)
{
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  for (int i = 0; i < 8; i++)
  {
    if (names[i] == note)
    {
      playTone(tones[i], duration);
    }
  }
}

void setup()
{
  pinMode(piezoPin, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}
int now=0;
void loop()
{
  delay(tempo/2);
  //int sensorVal = digitalRead(button);
  if(now==0) ///if (sensorVal == HIGH)
  {
    for (int i = 0; i < length; i++)
    {
      if (ttlsnotes[i] == ' ')
      {
        delay(ttlsbeats[i] * tempo);
      }
      else
        playNote(ttlsnotes[i], ttlsbeats[i] * tempo);
        if( digitalRead(button)==LOW)break;{ now=1; break;}
      delay(tempo / 2);
    }
  }
  else///
  {
    for (int i = 0; i < lengths; i++)
    {
      if (jbnotes[i] == ' ')
      {
        delay(jbbeats[i] * tempo);
      }
      else
        playNote(jbnotes[i], jbbeats[i] * tempo);

      if( digitalRead(button)==LOW){now=0; break;}
      delay(tempo / 2);
    }
  }
}

2018年11月1日 星期四

Week8 期中作品


利用小葉老師教的內差alpha介於0到1之間,讓星星隨機散出。用滑鼠的移動去吃星星,以及加上倒數計時及計分的程式,遊戲的畫面會有逐漸放大的效果,讓玩家會更有身歷其境的感覺。


2018年10月25日 星期四

Week07

血條

int life=10;
void setup(){
  size(500,500);
}
void draw(){
  background(0,0,205);
  fill(255); rect(50,50,100,20);
  fill(255,0,0); rect(50,50, life*10,20);
}
void mousePressed(){
 life--;
}

2018年10月18日 星期四

Week06_珮妤

播放聲音
import ddf.minim.*;
Minim minim;
AudioPlayer player;
void setup(){
  minim = new Minim(this);
  player=minim.loadFile("groove.mp3");
  player.play();
}
void draw(){
}

水果忍者


float [] fruitX=new float[20];
float [] fruitY=new float[20];
float [] fruitVX=new float[20];
float [] fruitVY=new float[20];

void reborn(int i){
  fruitX[i]=random(800);   fruitVX[i]=random(6)-3;
   fruitY[i]=700;   fruitVY[i]=-35;
 }

void setup(){
  size(800,600);
  for(int i=0;i<20;i++){
   reborn(i);
 }
}
void draw(){
  background(0);
  for(int i=0;i<20;i++){
    if(dist(mouseX, mouseY, fruitX[i], fruitY[i])<=50){
      reborn(i); continue;
    }
    ellipse(fruitX[i],fruitY[i], 100,100);
    fruitX[i]+=fruitVX[i];
    fruitY[i]+=fruitVY[i];
    fruitVY[i]+=0.98;
    if( fruitY[i]>700) reborn(i);
  }
}

2018年10月11日 星期四

Week_05珮妤的筆記


PImage [] imgN=new PImage[10];
float[] eggX=new float[100];
float[] eggY=new float[100];
boolean[] eggDie=new boolean[100];
void setup() {
  size(800, 600);
  for(int i=0;i<=9;i++) imgN[i] = loadImage(i+".jpg");
  for(int i=0; i<100; i++) {
    eggX[i]=random(100, 700);
    eggY[i]=random(2000);
    eggDie[i]=false;
  }
}
int score=0;
void draw(){
  background(255);
  fill(255); rect(mouseX-50, mouseY-25, 100, 50);
  for(int i=0; i<100; i++){
    if(eggDie[i]) continue;
    ellipse(eggX[i], eggY[i], 80, 100);
    eggY[i]+=5;
    if(eggY[i]>700) eggY[i]=-2000+600;
    if(dist(mouseX, mouseY, eggX[i], eggY[i])<50 ){
      eggDie[i]=true;
      score+=100;
  }
}

int now=score;
for(int i=0; i<7; i++){
  image(imgN[(now%10)], 800-i*100, 100, 100,150);
  now/=10;
}
}


櫻桃
PImage []img= new PImage[8];
float []cherryX=new float[8];
float []cherryY=new float[8];
float []cherryVX=new float[8];
float []cherryVY=new float[8];
void cherryReborn(int i){
  cherryX[i]=random(800);
  cherryY[i]= 700;
  cherryVX[i]=random(8)-4;
  cherryVY[i]=-40;
}
void setup(){
  size(800,600);
  for(int i=0;i<8;i++) img[i]=loadImage("cherry.png");
  for(int i=0;i<8;i++) cherryReborn(i);
   for(int i=0;i<10;i++) sword[i]=new PVector();
}
PVector [] sword = new PVector[10];
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    image(img[i], cherryX[i],cherryY[i],100,150);
    cherryX[i]+=cherryVX[i];
    cherryY[i]+=cherryVY[i];
    cherryVY[i]+=0.98;
  }
  for(int i=9; i>0; i--){
    sword[i].x=sword[i-1].x; sword[i].y=sword[i-1].y;
  }
  sword[0].x=mouseX; sword[0].y=mouseY;
  for(int i=1;i<10;i++){
    line(sword[i].x, sword[i].y, sword[i-1].x, sword[i-1].y);
  }
}

2018年10月4日 星期四

Week04_珮妤的筆記


void setup(){
    size(800,600);
}
void draw(){
    background(255); ///清畫面才不會有殘影
    fill(255,0,0); rect(100,100,100,100);
    if(bulletFlying){
     ellipse(bulletX,bulletY, 50,50);
     bulletX+=3;
    }
}
float bulletX=100, bulletY=100;
boolean bulletFlying=false;
void keyPressed(){
    bulletFlying=true;
}


void setup(){
    size(800,600);
}
void draw(){
    background(255);
    fill(255,0,0); rect(100,100,100,100);
    for(int i=0;i<bulletN;i++){
    if(bulletFlying[i]){
     ellipse(bulletX[i],bulletY[i], 50,50);
     bulletX[i]+=3;
    }
   }
}
float []bulletX= new float[100];
float []bulletY= new float[100];
int bulletN=0;
boolean []bulletFlying=new boolean[100];
void keyPressed(){
    bulletFlying[bulletN] = true;
    bulletX[ bulletN ] = 100;bulletY[ bulletN ] = 100;
    bulletN++;
}

PImage imgBullet, imgBG;
void setup(){
    size(800,600);
    imgBG = loadImage("BG.jpg");
    imgBullet = loadImage("bullet.png");
    imageMode(CENTER);
}
void draw(){
  image(imgBG, width/2,height/2, width,height);
  image(imgBullet, mouseX,mouseY, 100,100);

    for(int i=0;i<bulletN;i++){
    if(bulletFlying[i]){
     image(imgBullet,bulletX[i],bulletY[i], 100,100);
     bulletX[i]+=3;
    }
   }
}
float []bulletX= new float[100];
float []bulletY= new float[100];
boolean []bulletFlying=new boolean[100];
int bulletN=0;
void mousePressed(){
    bulletFlying[bulletN] = true;
    bulletX[ bulletN ] =  mouseX;
    bulletY[ bulletN ] =  mouseY;
    bulletN++;
}

2018年9月27日 星期四

Week03_珮妤的筆記



在圓內畫一個六邊形

void setup(){
   size(500,500);
}
void draw(){
    ellipse(250,250, 480,480);
    for(float angle=0; angle<PI*2; angle+=PI/3){
      triangle(250,250,
              250+240*cos(angle), 250+240*sin(angle),
              250+240*cos(angle+PI/3), 250+240*sin(angle+PI/3) );
    }
}

void setup(){
   size(500,500);
}
void draw(){
    ellipse(250,250, 480,480);
    for(float angle=0; angle<PI*2; angle+=PI/3){
     for(int R=0; R<=240; R+=30){
       noFill();
       triangle(250,250,
              250+R*cos(angle), 250+R*sin(angle),
              250+R*cos(angle+PI/3), 250+R*sin(angle+PI/3) );
    }
  }
}

void setup(){
   size(800,400);
}
void myTriangle(int cx, int cy){
    float a= -PI/2;
     for(int R=15; R>0; R-=5){
       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+=40){
      for(int cy=20; cy<800; cy+=40){
     
        myTriangle(cx,cy);
      }
    }
}

PImage imgMario;
float marioX=0, marioY=0, marioVX=5, marioVY=10;
void setup(){
  size(400,600);
  imgMario = loadImage("mario.png");
}
void draw(){
  background(255);
  image(imgMario, marioX, marioY, 100,150);
  marioX += marioVX;
  marioY += marioVY;
}
void keyPressed(){
  if(keyCode==UP)  {marioY-=5;}
  if(keyCode==DOWN)  {marioY+=5;}
  if(keyCode==LEFT)  {marioX-=5;}
  if(keyCode==RIGHT)  {marioX+=5;}
}

PImage imgMario;
float marioX=0, marioY=0, marioVX=2, marioVY=0, marioAX=0, marioAY=0.1;
void setup(){
  size(400,600);
  imgMario = loadImage("mario.png");
}
void draw(){
  //background(255);
  image(imgMario, marioX, marioY, 100,150);
  marioX += marioVX; marioY += marioVY;
  marioVX += marioAX; marioVY += marioAY;
}
void keyPressed(){
  if(keyCode==UP)  {marioY-=5;}
  if(keyCode==DOWN)  {marioY+=5;}
  if(keyCode==LEFT)  {marioX-=5;}
  if(keyCode==RIGHT)  {marioX+=5;}
}

PImage imgMario;
float marioX=0, marioY=100, marioVX=1, marioVY=0, marioAX=0, marioAY=0.1;

void setup(){
  size(400,600);
  imgMario = loadImage("mario.png");
}
void draw(){
  //background(255);
  image(imgMario, marioX, marioY, 100,150);
  marioX += marioVX; marioVX+=0;
  marioY += marioVY; marioVY += 0.98;
  if(marioY>500){
    marioVY = - marioVY * 0.8;
    marioY=500;
}

if(marioX>300) marioVX = -marioVX;
}
void keyPressed(){
  if(keyCode==UP)  {marioY-=5;}
  if(keyCode==DOWN)  {marioY+=5;}
  if(keyCode==LEFT)  {marioX-=5;}
  if(keyCode==RIGHT)  {marioX+=5;}
}

2018年9月13日 星期四