顯示具有 week19 標籤的文章。 顯示所有文章
顯示具有 week19 標籤的文章。 顯示所有文章

2013年1月20日 星期日

WEEK 19,Final




程式碼:
processing:


import processing.serial.*;
Serial myPort;

int i,d1=0,d2=0,d3=0,d4=0;
int score=0, lastTime=60;
int[] value = new int[50000];
color[] c=new color[4];
int a,count=0;
int tmp=0;
int startt;
int val;
void setup(){
  size(500,600);
  myPort = new Serial(this, "COM4", 9600);
 
  c[0]=color(255,0,0);
  c[1]=color(0,255,0);
  c[2]=color(0,0,255);
  c[3]=color(255,255,255);
  for(i=0;i<10000;i++)
  {
    a=int(random(0,3.99));
    /*
    if(i%25==0)
      fill(255,0,0);
      else  if(i%30==0)
          fill(0,255,0);
          else  if(i%40==0)
              fill(0,0,255);
              else
              fill(255,255,255);
    */
    count%=50000;
    value[count++]=a;
  }
}
void draw(){
  background(255,200,100);
  if(myPort.available()>0){
    val = myPort.read();//
第三個重點:如果讀到數字就拿出來用
    //println("123");
  }
textSize(30);
fill(100,0,50);
text("Time:"+lastTime, 50,80);
text("Score:"+score, 0,220);
 if(lastTime==0){
      stop();
      textFont(createFont("3", 60));
      background(100,100,100);
      fill(255,255,255);
      text("Game Over ",120,250);
      text("Your score:"+score,0,350);
 }
lastTime=30-millis()/1000;

    i=200;
    for(int k=startt;k<startt+20;k++){
    fill(c[value[k]]);
    rect(i,100,80,80);
    i+=20;
    }
 




  fill(255,200,100);
  rect(0,100,200,80);

  if(d1==1){
    fill(128,128,128);
  }else{
    fill(255,0,0);
  }
  rect(60,400,80,80);
 
  if(d2==1){
    fill(128,128,128);
  }else{
    fill(0,255,0);
  }
  rect(360,400,80,80);
  if(d3==1){
    fill(128,128,128);
  }else{
    fill(0,0,255);
  }
  rect(260,400,80,80);
  if(d4==1){
    fill(128,128,128);
  }else{
    fill(255,255,255);
  }
  rect(160,400,80,80);
 
  if(val=='A'){
    d1=1;
    if(value[startt]==0){
      startt++;
      score+=10;
    }
  }
  else if(val=='S'){
 
    d2=1;if(value[startt]==1){
      startt++;
      score+=10;
    }
  }
  else if(val=='D'){
    d3=1;if(value[startt]==2)
    {  startt++;
    score+=10;
    }
  }else if(val=='F'){
    d4=1;if(value[startt]==3)   
     { startt++;
     score+=10;
     }
  }
  else if(val=='H'){
   d1=0;
  d2=0;
 d3=0;
d4=0;
  }
}
void keyPressed(){
  if(key=='4'){
    d1=1;
    if(value[startt]==0){
      startt++;
      score+=10;
    }
  }else if(key=='5'){
 
    d2=1;if(value[startt]==1){
      startt++;
      score+=10;
    }
  }else if(key=='r'){
    d3=1;if(value[startt]==2)
    {  startt++;
    score+=10;
    }
  }else if(key=='t'){
    d4=1;if(value[startt]==3)   
     { startt++;
     score+=10;
     }
  }
}
void keyReleased(){
  if(key=='4'){
    d1=0;
  }else if(key=='5'){
    d2=0;
  }else if(key=='r'){
    d3=0;
  }else if(key=='t'){
    d4=0;
  }
}


arduino:

int buttonPin5 = 5;
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;

int buttonState5 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
void setup() {,tgy

  pinMode(buttonPin5, INPUT);    
  pinMode(buttonPin2, INPUT);    
  pinMode(buttonPin3, INPUT);     
  pinMode(buttonPin4, INPUT);
 
  Serial.begin(9600);

}
void loop() {
  buttonState5 = digitalRead(buttonPin5);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
 
  if (buttonState5 == HIGH) {    
   //tone(8, 131, 300);
   Serial.write('A');
  } 
  else if (buttonState5 == LOW) {
   //Serial.write('Z');
  }
 
  if (buttonState2== HIGH) {    
   //tone(8, 147 , 300);
   Serial.print('S');
  }
  else if (buttonState2 == LOW) {
   //Serial.write('X');
  }

  if (buttonState3 == HIGH) {    
   //tone(8, 165 , 300);
   Serial.print('D');
  }
   else if (buttonState3 == LOW) {
   //Serial.write('C');
  }

  if (buttonState4 == HIGH) {    
   //tone(8, 175 , 300);
   Serial.print('F');
  }
   else if (buttonState4 == LOW) {
   //Serial.write('V');
  }
  if(buttonState2 == LOW&&buttonState3 == LOW&&buttonState4 == LOW&&buttonState5 == LOW){
  Serial.write('H');
  }
   delay(500);
}


心得:
在眾多的課程當中我覺得這是最需要創意且最有難度的一堂課,要會打程式要會麵包板還需要很多靈感來完成作品,而這堂課除了要感謝老師也要感謝助教、學長還有同學的幫忙,讓我們這組能順利的打完程式碼。雖然這堂課困難重重,但我又比別人多會了一樣的東西。

組員分工:
程式碼:朱席寬
電路板:林品妤
找題材:朱席寬
期末上台報告:林品妤

2013年1月18日 星期五

Week 19 , Final

系統名稱: 直升機保衛戰


1. 遊戲進行和裝置介紹

 

遊戲剛開始畫面















直升機射擊時的畫面

  

攔截失敗時的畫面(損失生命)














操作裝置(搖桿+射擊按鈕)






2. 遊戲玩法說明

        一開始進入遊戲, 畫面上方會隨機出現轟炸飛彈,此時必須使用搖桿控制直升機左右移動,並且按射擊鍵攔截飛彈,如果攔截飛彈失敗或是碰到機身,此時就會扣掉右下角的生命值,當生命值為0時,遊戲結束並且秀出得分狀況。


3. 遊戲Demo影片

 

4.程式碼

 
 /*Arduino*/
int pushButton = 2;
int pushButton2 = 6;
int pushButton3 = 7;
void setup() {

 Serial.begin(9600);
 pinMode(pushButton, INPUT_PULLUP);
 pinMode(pushButton2, INPUT_PULLUP);
 pinMode(pushButton3, INPUT_PULLUP);
}
int a=0;
int a2=0;
int a3=0;
// the loop routine runs over and over again forever:
void loop() {
 // read the input pin:
 int buttonState = digitalRead(pushButton);
 int buttonState2 = digitalRead(pushButton2);
  int buttonState3 = digitalRead(pushButton3);
 // print out the state of the button:
 if (buttonState == HIGH && a==0 ) { // If switch is ON,
   Serial.print(1); 
   a=1; // send 1 to Processing
 }
 if (buttonState == LOW && a==1) { // If the switch is not ON,
   Serial.println(0); 
   a=0; // send 0 to Processing
 }
 if (buttonState2 == HIGH && a2==0 ) { // If switch is ON,
   Serial.print(2); 
   a2=2; // send 1 to Processing
 }
 if (buttonState2 == LOW && a2==1) { // If the switch is not ON,
   Serial.println(3); 
   a2=3; // send 0 to Processing
 }
 if (buttonState3 == HIGH && a3==0 ) { // If switch is ON,
   Serial.print(4); 
   a3=4; // send 1 to Processing
 }
 if (buttonState3 == LOW && a3==1) { // If the switch is not ON,
   Serial.println(5); 
   a2=5; // send 0 to Processing
 }
 // Serial.println(buttonState);
 delay(10); // delay in between reads for stability
}
   

/*Processing*/
import processing.serial.*;
Serial myPort;  
int val;
PImage bg,helicopter,bomb;
PImage [] life = new PImage[5];
float hx=350,hy=400;
PImage [] missile = new PImage[5];
int[] mx = new int [5];
int[] my = new int [5];
int bx=0,by=0;

void setup(){
  size(840,525);
  String portName = Serial.list()[0];
  myPort = new Serial(this, "COM5", 9600);
  
  bg =loadImage("sky.jpg");
  helicopter= loadImage ("Helicopter.png");
  for(int i=0;i<5;i++){
   life[i]= loadImage("Life.png");
  }
  for(int i=0;i<5;i++){
   missile[i]= loadImage("Missile.png");
  }
  for(int i=0;i<5;i++){
    mx[i]=i*200;
    my[i]=(-300+i*40);
  }
  bomb= loadImage ("bomb.png");
}
void draw(){
  background(bg);
  life();
  helicopterA();
  missilefly();
  myserial();
  bomb();
}
void  helicopterA(){
  image(helicopter,hx,hy);
}
void life(){
  fill(180,180,180);
  rect(740,340,60,160);
  fill(255,0,0);
  rect(740,300,60,40);
  fill(255,255,255);
  textFont(createFont("Georgia", 25));
  text("LIFE ",742,330);
  for(int i=0;i<5;i++){
  image(life[i],690,320+(30*i),150,100);
  }

}
void missilefly(){
  for(int i=0;i<5;i++){
    image(missile[i],mx[i],my[i],50,100);
  }
  for(int i=0;i<5;i++){
    my[i]+=2;
  }
  for(int i=0;i<5;i++){
    if(my[i]==600){
      my[i]=(-300+i*40);
    }
  }
}

void myserial(){
  if ( myPort.available() &rt; 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  if (val == 0) {              // If the serial value is 0,
    translate(hx,hx+=10);
  } 
  else if(val == 2){                       // If the serial value is not 0,
    translate(hx,hx-=10);   
  }

}
void bomb(){
 int p=0;
  image(bomb,hx+90,hy+by,10,15);  
    if(val != 4 && by!=0){
      by-=10;
    }
  
    if (val == 4){
       by-=10;
    }
  if((hy+by)<=0){
    by=0;
 }
} 


5.心得

這次得專題製作跟以前碰過的都不太相同,除了還是有程式的撰寫之外,還必須同時結合到2個程式,所以中間的連結很重要。

此外在接Arduino線路時,必須克服許多問題(ex.接觸不良、接腳錯誤.....等)因此也花了較多的時間在處理硬體上面的問題,但是還好最後都有想出辦法解決(ex.絕緣膠帶,多接幾次線路並檢查)。

麒時後來在Demo時還覺得蠻有成就感的,畢竟是自己從無到有生出來的, 所以很開心!!

也謝謝各位同學在票選時對我們的厚愛,害我們自己都下了一跳呢! 謝謝大家 :)


6.工作分配



遊戲作品構思:

李之寧 、邱垂傑



遊戲程式:

Arduino + 合併 李之寧

Processing 邱垂傑




遊戲硬體:

Arduino 各線路連接 李之寧

搖桿本體製作 邱垂傑

 

  

Week19, Final


這次的期末作品是拿我們的期中作品來改良的,把原本用滑鼠移動的海綿寶寶改用Arduino的按鍵來坐上下左右的移動

Arduino程式碼:
int buttonPin5 = 5;
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;

int buttonState5 = 0;  
int buttonState2 = 0;  
int buttonState3 = 0;  
int buttonState4 = 0;  
void setup() {

  pinMode(buttonPin5, INPUT);     
  pinMode(buttonPin2, INPUT);     
  pinMode(buttonPin3, INPUT);      
  pinMode(buttonPin4, INPUT);
  
  Serial.begin(9600);

}
void loop() {
  buttonState5 = digitalRead(buttonPin5);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  
  if (buttonState5 == HIGH) {     
   //tone(8, 131, 300);
   Serial.write('A');
  }  
  else if (buttonState5 == LOW) { 
   //Serial.write('Z');
  }
  
  if (buttonState2== HIGH) {     
   //tone(8, 147 , 300);
   Serial.print('S');
  }
  else if (buttonState2 == LOW) { 
   //Serial.write('X');
  }

  if (buttonState3 == HIGH) {     
   //tone(8, 165 , 300);
   Serial.print('D');
  }
   else if (buttonState3 == LOW) { 
   //Serial.write('C');
  }

  if (buttonState4 == HIGH) {     
   //tone(8, 175 , 300);
   Serial.print('F');
  }
   else if (buttonState4 == LOW) { 
   //Serial.write('V');
  }
  if(buttonState2 == LOW&&buttonState3 == LOW&&buttonState4 == LOW&&buttonState5 == LOW){
  Serial.write('H');
  }
   delay(500);
}







processing程式碼:


import processing.serial.*;
Serial myPort;

int popox=0,popoy=0,popoy1=0,popoy2=0,popoy3=0,popoy4=0,popoy5=0;
int fishx=0,fishy=0;
int score=600;
int lasttime=60;

import ddf.minim.*;
AudioPlayer  player,wate,cheers;
Minim minim;

PImage popo;
//PImage finger;
PImage bobo;
PImage back;
PImage fish;
void setup()
{
  size(800,600);
  myPort = new Serial(this, "COM3", 9600);
  fish=loadImage("fish.png"); //游動的魚
  bobo=loadImage("bobo.png"); //海綿寶寶
  //finger=loadImage("finger.png"); //手指
  popo=loadImage("popo.png"); //泡泡
  back=loadImage("back.jpeg"); //背景圖
  minim=new Minim(this);
  //wate=minim.loadFile("wate.wav", 2048);
player=minim.loadFile("file.mp3"); //背景音樂
player.play();
}
int val;
int b_x=400,b_y=300;
int w=5,s=5,a=5,d=5;
void draw()
{
  if(myPort.available()>0){
    val = myPort.read();//第三個重點:如果讀到數字就拿出來用
    println(val);
}

  image(back,0,0);
  //image(bobo,250,100);


  popoy-=3; //泡泡移動量
  image(popo,popox+300,popoy+600,100,100);  //泡泡坐標,大小
 
  if (b_x > popox+300 && b_x< popox+400 && b_y<popoy+700 && b_y>popoy+600) //判斷滑鼠是否點到泡泡的範圍
   {
     popoy=600; //泡泡回到下面再跑一次
   score-=10; //碰到泡泡減十分
   wate=minim.loadFile("wate.wav"); //戳到泡泡時播放音效
   wate.play();
   }
 
   if (b_x > popox+200 && b_x< popox+300 && b_y<popoy1+600 && b_y>popoy1+500)
  {
    popoy1=500;
   score-=10;
   wate=minim.loadFile("wate.wav");
   wate.play();
  }
   if (b_x > popox+100 && b_x< popox+200 && b_y<popoy2+800 &&  b_y>popoy2+700)
  {
    popoy2=700;
   score-=10;
   wate=minim.loadFile("wate.wav");
   wate.play();
 }
   if (b_x > popox+500 && b_x< popox+600 && b_y<popoy3+750 && b_y>popoy3+650)
   {
     popoy3=600;
   score-=10;
   wate=minim.loadFile("wate.wav");
   wate.play();
   }
  if (b_x > popox+400 && b_x< popox+500 && b_y<popoy4+950 && b_y>popoy4+850)
  {
    popoy4=600;
  score-=10;
  wate=minim.loadFile("wate.wav");
   wate.play();
  }
    if (b_x > popox+600 && b_x< popox+700 &&  b_y<popoy5+1050 && b_y>popoy5+950)
  {
    popoy5=700;
  score-=10;
  wate=minim.loadFile("wate.wav");
   wate.play();
  }
 
  if(popoy+600<0) //判斷泡泡是否跑出視窗
  {

    popoy=0; //泡泡回到原點再跑一次
  }
   popoy1-=2;
  image(popo,popox+200,popoy1+500,100,100);
   if(popoy1+500<0)
  {

    popoy1=0;
  }
   popoy2-=3;
  image(popo,popox+100,popoy2+700,100,100);
   if(popoy2+700<0)
  {

    popoy2=0;
  }
   popoy3-=5.5;
  image(popo,popox+500,popoy3+650,100,100);
   if(popoy3+650<0)
  {
    popoy3=0;
  }
  popoy4-=2;
  image(popo,popox+400,popoy4+850,100,100);
   if(popoy4+850<0)
  {
    popoy4=0;
  }
  popoy5-=3;
  image(popo,popox+600,popoy5+950,100,100);
   if(popoy5+950<0)
  {
    popoy5=0;
  }

  image(bobo,b_x,b_y,60,60);
  fishx++;
  image(fish,fishx,fishy+200);
  {
    if(fishx>800) //判斷魚是否跑出視窗
    {
    fishx=0;
    fishy=250;
    }
  }
  world();

 if(val=='A'){
    b_x-=4;
  }
 if(val=='S'){
    b_x+=4;
  }
 if(val=='F'){
    b_y-=4;
  }
 if(val=='D'){
    b_y+=4;
  }
  if(val=='H'){
   b_y+=0;
    b_x+=0;
  }
 
  if(b_x<0)
  {
    b_x=0;
  }
 
    if(b_x>745)
  {
    b_x=745;
  }
 
    if(b_y<0)
  {
    b_y=0;
  }

   if(b_y>540)
  {
    b_y=540;
  }
}

  void world()
  {
     textFont(createFont("Georgia", 48));
      fill(255);
  text("score:"+score, 20, 550); //分數
 
  textFont(createFont("Georgia", 48));
      fill(255);
  text("Time:"+lasttime, 250, 550); //計時器
  lasttime=60-millis()/1000;
 
      if(lasttime==0) //當時間=0,秀出得分
      {
      stop();
      textFont(createFont("Georgia", 80));
      background(51,102,51);
      fill(255,255,255);
      text("Game Over",200,300);
      text("Your score:"+score,180,400);
      cheers=minim.loadFile("cheers.wav");
      cheers.play();
      }
  }

心得:
這次的期末作業我們覺得沒有做得很好,Demo到後面的時候有點乾
按鍵道具的部分也是用課堂上的案件,並沒有花心思在其他道具上,但在Arduino的程式上,我們花了不少時間去研究,也學到了不少的東西.

Week19, Final



期末DEMO
聲音不夠大聲,所以有後製補上聲音。
畫面稍嫌小,不過仔細看還是看的到音符落點。

這學期這堂課學到很多,
也感謝老師的獎品鼓勵,
未來將會用它做出更棒更完整的作品。
秒咪記憶遊戲
一,遊戲構思和介紹
動機:
由於現在人使用大量的電子產品,很多事情都賴於墊子產品的紀錄功能,而使用到腦袋來記憶的機會越來越少,因此我們想設計出一個記憶遊戲,除了富含娛樂功能,有能激活大家的記憶細胞:D
介紹;
就跟大部分的記憶遊戲玩法差不多。主遊戲畫面上共排列不同的五張貓咪圖片,當遊戲開始時,會依照所選擇的難度,隨機亮起不同的圖片,而玩家須要記得圖片的排列順序,依照順序按下按鈕後,再按下確認鍵確認答案,接著遊戲會給予回饋,看是答對或失敗,也可在選擇難度繼續遊戲,或是離開。

二,畫面介紹
↓ 剛進入的遊戲起始畫面:
↓ 按下確認鍵後,會進入到選擇難度的畫面:


↓ 選擇難度後,會進入到遊戲主畫面:
 
↓ 會隨機出現
 
 ↓ 遊戲過關畫面:
↓ 遊戲失敗畫面: 
 
↓ 遊戲離開畫面:



↓ 成功破最難關卡的畫面: 
  
三,硬體
遊戲裝置上有六個按鈕,其中五個白色保麗龍按鈕是用來回答題目順序的,而右下角的紅色按鈕則是確認紐。


四,程式碼
Arduino:
//大的圓形鈕
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 7;  //確認鈕

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int input=0, oldInput=0, a=0;

void setup(){
  Serial.begin(9600);   
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
}

void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);  
  buttonState3 = digitalRead(buttonPin3); 
  buttonState4 = digitalRead(buttonPin4); 
  buttonState5 = digitalRead(buttonPin5); 
  buttonState6 = digitalRead(buttonPin6); 
  
  if(buttonState1== HIGH) input=1;
  else if(buttonState2== HIGH) input=2;
  else if(buttonState3== HIGH) input=3;
  else if(buttonState4== HIGH) input=4;
  else if(buttonState5== HIGH) input=5;
  else if(buttonState6== HIGH) input=6;
  else input=0;
  
  if(input!=oldInput){
    if(input==1){
      Serial.print(1);
      oldInput=input;
      a=1;
    }else if(input==2){
      Serial.print(2);
      oldInput=input;
      a=2;
    }else if(input==3){
      Serial.print(3);
      oldInput=input;
      a=3;
    }else if(input==4){
      Serial.print(4);
      oldInput=input;
      a=4;
    }else if(input==5){
      Serial.print(5);
      oldInput=input;
      a=5;
    }else if(input==6){
      Serial.print(6);
      oldInput=input;
      a=6;
    }
  }
}
Processing:
import processing.serial.*;
Serial myport;

PImage rule, start;  //開始
PImage easy, nor, hard, bEasy, bNormal, bHard;  //選單
PImage gmioumi1, gmioumi2, gmioumi3, gmioumi4, gmioumi5;  //底圖
PImage mioumi1, mioumi2, mioumi3, mioumi4, mioumi5;  //亮圖
PImage pass, over, win, End;//結果
float x=0, y=0, z=0, oldx=0, nowx=0;
float xTime=3;
String str="", str2="",oldstr2="", a="6",b="6", c="", oldc="";
int inByte=0, choose=0, end=3, e=0, f=0, g=0;

void setup(){
  myport=new Serial(this,"COM3",9600);
  //開始
  rule = loadImage("rule.jpg");
  start = loadImage("start.png");
  //選單
  easy = loadImage("easy.png");
  nor = loadImage("normal.png");
  hard = loadImage("hard.png");
  bEasy = loadImage("bEasy.jpg");
  bNormal = loadImage("bNormal.jpg");
  bHard = loadImage("bHard.jpg");
  //底圖
  gmioumi1 = loadImage("g1.jpg");
  gmioumi2 = loadImage("g2.jpg");
  gmioumi3 = loadImage("g3.jpg");
  gmioumi4 = loadImage("g4.jpg");
  gmioumi5 = loadImage("g5.jpg");
  //亮圖
  mioumi1 = loadImage("c1.png");
  mioumi2 = loadImage("c2.png");
  mioumi3 = loadImage("c3.png");
  mioumi4 = loadImage("c4.png");
  mioumi5 = loadImage("c5.png"); 
  //結果
  pass = loadImage("pass.jpg");
  over = loadImage("over.jpg");
  win = loadImage("win.jpg");
  End = loadImage("end.jpg");
  size(600,400);
  background(0,0,0);
}

void draw(){
  if(myport.available()>0){  //測試有無訊號來
    inByte=myport.read();  //如果有來就read
    if(inByte==1+48) c="1";
    else if(inByte==2+48) c="2";
    else if(inByte==3+48) c="3";
    else if(inByte==4+48) c="4";
    else if(inByte==5+48) c="5";
    else if(inByte==6+48) c="6";
  }
  if(c!=oldc){
    println(c);
    oldc=c;
  }
  if(c==""){
    image(rule,50,50,500,250);
    image(start,200,320,200,50);
    e=1;
  }else if(c=="6" && e==1){
    fill(0,0,0);
    rect(0,0,600,400);
    image(easy,50,50,160,50);
    image(nor,220,50,160,50);
    image(hard,390,50,160,50);
    image(bEasy,50,100,160,250);
    image(bNormal,220,100,160,250);
    image(bHard,390,100,160,250);
    c="0";
  }  
  if(c=="1" && e==1){
    choose=1;
    e=2;
    c="0";
  }else if(c=="3" && e==1){ 
    choose=3;
    e=2;
    c="0";
  }else if(c=="5" && e==1){
    choose=5;
    e=2;
    c="0";
  }  
  if(choose==1){
    //easy
    if(y<6){
      if(millis()-xTime>1000){
        x=random(5); 
        b=a;
        nowx=(int)x/1;
        if(nowx==oldx){
          if(nowx==4){
            x=1;
            oldx=(int)x/1;
          }else if(nowx==5){
            x=2;
            oldx=(int)x/1;
          }else{ 
            x=x+1;
            oldx=(int)x/1;
          }  
        }else oldx=(int)x/1;
        if(x>0 && x<=1){
          fill(0,0,0);
          rect(0,0,600,400); 
          image(mioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="1";
        }else if(x>1 && x<=2){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi2,150,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="2";
        }else if(x>2 && x<=3){  
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi3,250,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="3";
        }else if(x>3 && x<=4){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi4,350,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="4";
        }else if(x>4 && x<=5){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi5,450,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          a="5";
        } 
        xTime=millis();
        y=y+1;
        if(y<6 && b!=a){ 
          str=str+a;
          println("*"+str);
          z=0;
        }else if(y<6 && z==0 && b==a){
          str=str+a;
          println("*"+str);
          z=1;
        } 
      }else if(millis()-xTime>600 && millis()-xTime<800){
        fill(0,0,0);
        rect(0,0,600,400);
        image(gmioumi1,50,100,100,100);
        image(gmioumi2,150,100,100,100);
        image(gmioumi3,250,100,100,100);
        image(gmioumi4,350,100,100,100);
        image(gmioumi5,450,100,100,100);
      }
    }else if(e==2){
      fill(0,0,0);
      rect(0,0,600,400);
      image(gmioumi1,50,100,100,100);
      image(gmioumi2,150,100,100,100);
      image(gmioumi3,250,100,100,100);
      image(gmioumi4,350,100,100,100);
      image(gmioumi5,450,100,100,100);
    }
    if(c!=oldstr2){
      if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ 
        oldstr2=c;
        str2=str2+c;
        println("**"+str2+"**");
      }
    } 
  }else if(choose==3){
    //normal
    if(y<8){
      if(millis()-xTime>1000){
        x=random(5); 
        b=a;
        nowx=(int)x/1;
        if(nowx==oldx){
          if(nowx==4){
            x=1;
            oldx=(int)x/1;
          }else if(nowx==5){
            x=2;
            oldx=(int)x/1;
          }else{ 
            x=x+1;
            oldx=(int)x/1;
          }  
        }else oldx=(int)x/1;
        if(x>0 && x<=1){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="1";
        }else if(x>1 && x<=2){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi2,150,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="2";
        }else if(x>2 && x<=3){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi3,250,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="3";
        }else if(x>3 && x<=4){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi4,350,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="4";
        }else if(x>4 && x<=5){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi5,450,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          a="5";
        } 
        xTime=millis();
        y=y+1;
        if(y<8 && b!=a){ 
          str=str+a;
          println(str);
          z=0;
        }else if(y<8 && z==0 && b==a){
          str=str+a;
          println(str);
          z=1;
        } 
      }else if(millis()-xTime>600 && millis()-xTime<800){
        fill(0,0,0);
        rect(0,0,600,400);
        image(gmioumi1,50,100,100,100);
        image(gmioumi2,150,100,100,100);
        image(gmioumi3,250,100,100,100);
        image(gmioumi4,350,100,100,100);
        image(gmioumi5,450,100,100,100);
      }
    }else if(e==2){
      fill(0,0,0);
      rect(0,0,600,400);
      image(gmioumi1,50,100,100,100);
      image(gmioumi2,150,100,100,100);
      image(gmioumi3,250,100,100,100);
      image(gmioumi4,350,100,100,100);
      image(gmioumi5,450,100,100,100);
    }
    if(c!=oldstr2){
      if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ 
        oldstr2=c;
        str2=str2+c;
        println("**"+str2+"**");
      }
    }
  }else if(choose==5){
    //hard
    if(y<9){
      if(millis()-xTime>500){
        x=random(5); 
        b=a;
        nowx=(int)x/1;
        if(nowx==oldx){
          if(nowx==4){
            x=1;
            oldx=(int)x/1;
          }else if(nowx==5){
            x=2;
            oldx=(int)x/1;
          }else{ 
            x=x+1;
            oldx=(int)x/1;
          }  
        }else oldx=(int)x/1;
        if(x>0 && x<=1){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="1";
        }else if(x>1 && x<=2){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi2,150,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="2";
        }else if(x>2 && x<=3){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi3,250,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="3";
        }else if(x>3 && x<=4){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi4,350,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="4";
        }else if(x>4 && x<=5){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi5,450,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          a="5";
        } 
        xTime=millis();
        y=y+1;
        if(b!=a){ 
          str=str+a;
          println(str);
          z=0;
        }else if(z==0 && b==a){
          str=str+a;
          println(str);
          z=1;
        } 
      }else if(millis()-xTime>200 && millis()-xTime<400){
        fill(0,0,0);
        rect(0,0,600,400);
        image(gmioumi1,50,100,100,100);
        image(gmioumi2,150,100,100,100);
        image(gmioumi3,250,100,100,100);
        image(gmioumi4,350,100,100,100);
        image(gmioumi5,450,100,100,100);
      }
    }else if(e==2){
      fill(0,0,0);
      rect(0,0,600,400);
      image(gmioumi1,50,100,100,100);
      image(gmioumi2,150,100,100,100);
      image(gmioumi3,250,100,100,100);
      image(gmioumi4,350,100,100,100);
      image(gmioumi5,450,100,100,100);
    }
    if(c!=oldstr2){
      if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ 
        oldstr2=c;
        str2=str2+c;
        println("**"+str2+"**");
        e=3;
      }
    }
  }
  
  if(str=="" || str2==""){
    str="1";
    str2="1";
  }
  
  f=Integer.parseInt(str);
  g=Integer.parseInt(str2);

  //f= new Integer(str).intValue();
  //g= new Integer(str2).intValue(); 
  
  if(c=="6" && e==2){
    if(f==g){
      image(pass,0,0,600,400);
    }else if(f!=g){
      image(over,0,0,600,400);
    }
  }else if(c=="6" && e==3){
    if(f==g){
      image(win,0,0,600,400);
    }else{
      image(over,0,0,600,400);
    }
  }
  if(mousePressed && mouseX>500 && mouseY>300){
    if(choose==1){ 
      choose=3;
      str="";
      str2="";
    }else if(choose==3){ 
      choose=5;
      str="";
      str2="";
    }
    e=1;
  }else if(mousePressed && mouseX<100 && mouseY>300){
    image(End,0,0,600,400);
    c="0";
    e=4;
  }  
}


五,影片
課堂Demo影片:


這是成功最難的關卡之後的影片和畫面:
是顛倒的,抱歉QQ



六,心得
這次的遊戲,在規則上和設計上其實不難,但是我們寫程式的經驗依然不多,導致在過程中遇到了不少有關判斷式與環境不熟悉的問題,也因為這學期實在太多大型作業擠在一起的關係,看到我們自己與各組的作品完成度都沒有很高,感覺有點失望。
我們也是在考完期末考後的幾天才開始趕工、拚到凌晨,雖然很辛苦,但是也學到不少東西,是個難忘的回憶。對我們來說,這作業不只是學到技術層面的東西,也讓我們學到人與人之間的互先幫助與默契。對我們而言,這才是最可貴的。
程式方面,還是有一些bug未修復,但是,我們想說還是有機會的話能把它解決,讓作品呈現最完美的狀態。最後,還是感謝老師、助教和所以同學對我們作品的幫忙與支持。
謝謝 : D

2013年1月16日 星期三

Week19,Final

作品名稱:跳不停

1.DEMO影片










2.操作界面


3.程式碼(Arduino)

int sensorPin = A0;
int sensorValue = 0;
int oldValue =0;
int val[6];
int oldval[6];
void setup() {
  Serial.begin(9600);
  for(int i=2;i<7;i++) pinMode(i, INPUT);
}
void loop() {
  sensorValue = analogRead(sensorPin);
  if(sensorValue-oldValue>100){
    Serial.print(8);
    //Serial.write(sensorValue);
    oldValue = sensorValue;
  }
  else if(sensorValue-oldValue<-10){
    Serial.print(9);
    //Serial.write(sensorValue);
    oldValue = sensorValue;
  }
  for(int i=2;i<7;i++) val[i-2] = digitalRead(i);
  if (val[0] != oldval[0]) {
      Serial.print(0);
      //Serial.write(0);
      oldval[0] = val[0];
  }
  if (val[1] != oldval[1]) {
      Serial.print(1);
      //Serial.write(1);
      oldval[1] = val[1];
  }
  if (val[2] != oldval[2]) {
      Serial.print(2);
      //Serial.write(2);
      oldval[2] = val[2];
  }
  if (val[3] != oldval[3]) {
      Serial.print(3);
      //Serial.write(3);
      oldval[3] = val[3];
  }
  if (val[4] != oldval[4]) {
      Serial.print(4);
      //Serial.write(4);
      oldval[4] = val[4];
  }
}

4.程式碼(Processing)

import ddf.minim.*;
Minim minim;
AudioPlayer bgm0;//背景音樂1
AudioPlayer bgm1;//背景音樂2
AudioPlayer bgm2;//背景音樂3
import processing.serial.*;
Serial myPort;
int val;
int mute,nowbgm;//靜音,現正播放
int m,savedtime,stage=0;
int gameover=0;
int character,count;
int score,time;//現在時間,現在分數
float characterX=50,characterY=0;//人物位置
float vx=1,vy;//人物速度
float g=0.1;//場景重力
float stageX,startX;//場景位移,怪物位移
float[] monsterX = new float[20];//怪物位置
int nowbtn=1;
PImage Pstage,Pstop,help,title;
PImage[][] Pcharacter = new PImage[20][6];//人物
PImage[][] Pmonster = new PImage[20][6];//怪物
PImage[][] Pbutton = new PImage[2][2];//選單按鈕
PImage Pscore,Ptime,dotImage,xx;//分數,時間,冒號,乘號
PImage[] num = new PImage[10];
PImage[] sound = new PImage[2];//音樂開關
PImage[] playing = new PImage[3];//播放中
void setup(){
  String portName = Serial.list()[0];
  myPort = new Serial(this, "COM3", 9600);
  size(1280,720);
  Pstage=loadImage("stage.png");
  Pstop=loadImage("stop.png");
  help=loadImage("help.png");
  title=loadImage("title.png");
  for(int i=0;i<20;i++){
    for(int j=0;j<6;j++){
      Pcharacter[i][j] = loadImage("/character/Frame"+i+"_"+j+".png");//人物
      Pmonster[i][j] = loadImage("/monster/Frame"+i+"_"+j+".png");//怪物
      if(i<2&&j<2) Pbutton[i][j] = loadImage("button"+i+"_"+j+".png");//選單按鈕
    }
    monsterX[i]=monsterX[i]=320+random(6400);//怪物位置
    if(i<10) num[i] = loadImage("/count/"+i+".png");//場景
    if(i<2) sound[i] = loadImage("sound"+i+".png");//音樂開關
    if(i<3) playing[i] = loadImage((i+1)+".png");//播放中 
  }
  Pscore = loadImage("/count/score.png");//分數
  Ptime = loadImage("/count/time.png");//時間
  dotImage = loadImage("/count/dot.png");//冒號
  xx = loadImage("/count/x.png");
  minim = new Minim(this);
  bgm0 = minim.loadFile("/bgm/bgm0.mp3");//背景音樂1
  bgm1 = minim.loadFile("/bgm/bgm1.mp3");//背景音樂2
  bgm2 = minim.loadFile("/bgm/bgm2.mp3");//背景音樂3
}
void draw(){
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();
    control(val);
  }
  m = millis();
  showstage(stage);
  showsound(mute,nowbgm);//音樂控制
}
void showmonster(int x,float y){
  image(Pmonster[x][m/100%5],y,500);
  if(characterX>y&&characterX<y+44&&characterY>433.5&&characterY<566.5){
    gameover=1;
  }
}
void showstage(int x){
  switch(x){
    case 0: 
    image(Pstage,0,0);
    image(title,0,0);
    image(Pbutton[0][0],440,300);//選單按鈕1
    image(Pbutton[1][0],440,450);//選單按鈕2
    if(nowbtn==1) image(Pbutton[0][1],440,300);//選單按鈕1
    else if(nowbtn==2) image(Pbutton[1][1],440,450);//選單按鈕2
    break;
    case 1:
    if(stageX<-1280)  stageX+=1280;
    image(Pstage,0,0);
    image(Pstage,stageX+1280,0);
    image(Pcharacter[character][m/100%5],characterX,characterY);
    for(int i=0;i<20;i++){
    if(monsterX[i]+startX<-100) monsterX[i]+=1280+random(6400);
    showmonster(i,monsterX[i]+startX);
    }
    vy-=g;
    if(abs(vy)<0.1) vy=0;
    characterY-=vy;
    if(characterY>=500){
      vy=0;
      characterY=500;
    }
    if(characterY<=0) characterY=0;
    vx*=0.999;
    characterX+=vx;
    if(characterX>640){
      characterX=640;
      stageX-=vx;
      startX-=vx;
    }
    if(gameover==1){
      vx=0;
      vy=0;
      g=0;
      image(Pstop,0,0);
    }
    if(vx!=0){
    score++;
    int  passedtime = millis() - savedtime;
    time=passedtime;
    }
    showScore(score);
    showTime(time/10); 
    break;
    case 2:
    image(Pstage,0,0);
    image(title,0,0);
    image(help,320,180);
    break;
  }
}
void showScore(int x){
  int a,b,c,d,e,f;
  f=x%10; x/=10;
  e=x%10; x/=10;
  d=x%10; x/=10;
  c=x%10; x/=10;
  b=x%10; x/=10;
  a=x%10;
  image(Pscore,920,10);
  image(num[a],1080,10);
  image(num[b],1110,10);
  image(num[c],1140,10);
  image(num[d],1170,10);
  image(num[e],1200,10);
  image(num[f],1230,10);
}
//顯示時間
void showTime(int x){
  int a,b,c,d,e,f;
  f=x%10; x/=10;
  e=x%10; x/=10;
  d=x%10;  x/=10;
  c=x%6; x/=6;
  b=x%10; x/=10;
  a=x;
  image(Ptime,10,10);
  image(num[a],120,10);
  image(num[b],150,10);
  image(dotImage,180,10);
  image(num[c],210,10);
  image(num[d],240,10);
  image(dotImage,270,10);
  image(num[e],300,10);
  image(num[f],330,10);
}
void keyPressed() {
  switch(stage){
    case 0:
    if (key == CODED) {
      if (keyCode == UP) {
        nowbtn=1;
      } else if (keyCode == DOWN) {
        nowbtn=2;
      }
    }
    if(key == 'z'){
        stage=nowbtn;
    }
    break;
    case 1:
    if(key == 'x'){
      if(gameover==0)
      if(characterY>=500) vy+=10;
    }
    if(key == 'c'){
        if(gameover==0) vx++;
    }
    if(key == 'z'){
      if(gameover==1)
        exit();
    }
    break;
    case 2:
    if(key == 'z'){
        stage=0;
    }
    break;
  }
}
void control(int x){
  if(x == '4'){
      mute++;
  }
  if(x == '8'){
      if(nowbgm<3) nowbgm++;
  }
  else if(x == '9'){
     if(nowbgm>0)nowbgm--;
  }
  switch(stage){
   case 0:
    if (x == '1') {
      nowbtn=1;
    } else if (x == '2') {
      nowbtn=2;
    }
    if(x == '0'){
        stage=nowbtn;
    }
    break;
    case 1:
    if(x == '1'){
      if(gameover==0)
      if(characterY>=500) vy+=10;
    }
    if(x == '2'){
        if(gameover==0) vx++;
    }
    if(x == '0'){
      if(gameover==1)
        exit();
    }
    if(x == '3'){
      if(gameover==0){
        count++;
        character=count%20;
      }
    }
    break;
    case 2:
    if(x == '0'){
        stage=0;
    }
    break;
  }
}
void showsound(int x,int y){
  image(num[1],70,660);
  image(num[2],110,660);
  image(num[3],150,660);
  if(x%4==2){
    image(sound[0],10,660);
    switch(y){
      case 0:
      bgm0.play();
      bgm1.pause();
      bgm2.pause();
      image(playing[0],70,660);
      break;
      case 1:
      bgm1.play();
      bgm0.pause();
      bgm2.pause();
      image(playing[1],110,660);
      break;
      case 2:
      bgm2.play();
      bgm1.pause();
      bgm0.pause();
      image(playing[2],150,660);
      break;
    }
  }else if(x%4==0){
    image(sound[1],10,660);
    bgm0.pause();
    bgm1.pause();
    bgm2.pause();
  }
}


5.心得

這次製作其實沒有想像中的簡單,大概是因為我對電路不夠熟悉的緣故
同時在Processing跟Arduino的連結也很差勁
其實感覺還有很多可改進的部分。