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 各線路連接 李之寧

搖桿本體製作 邱垂傑

 

  

沒有留言:

張貼留言