2014年1月12日 星期日

期末作品-Temple Hit



程式碼:
Processing
PImage bg,blu,white,yellow,blu2,white2;
int N=6;
int count = 1500;
float []thingX=new float[6], thingY=new float[6],thingType = new float[6];
int grade=0;
import processing.serial.*;
Serial myPort;
int val;

import ddf.minim.*;
Minim minim;
AudioPlayer song;
int a=1;
void setup(){
  bg = loadImage("bg.jpg");
  blu = loadImage("blu.jpg");
  white = loadImage("white.jpg");
  yellow = loadImage("yellow.jpg");
  blu2 = loadImage("blu2.jpg");
  white2 = loadImage("white2.jpg");
  size(400,600);
  for(int i=0;i<N;i++){
    newThing(i);
  }
  String [] all = Serial.list();
  String portName = all[all.length-1];
  myPort = new Serial(this, portName, 9600);
  minim = new Minim(this);
  song = minim.loadFile("abc.mp3");

}
int countDown=10*30;
void draw(){
  image(bg,0,0, 400,600);
  image(blu,13,475,60,15);
  image(white,73,475,66,15);
  image(yellow,139,475,88,15);
  image(blu2,293,475,64,15);
  image(white2,227,475,66,15);

  if ( myPort.available() > 0) {
    val = myPort.read();  
  }

  if(countDown>0){

    background(255,0,255);
    countDown--;
    text("Count Down"+countDown/50,170,300);
    return;
  }
    if (a==1)   song.play();
    a=2;
  for(int i=0;i<N;i++){
    if(thingType[i]==1)
    {
     drawblu(i);
        if(val == '5')
        {
        if(430<thingY[i] && thingY[i]<500)
        {
          thingY[i] = 650; grade++; println(grade);
          fill(#0046FF,90);
          rect(10,475,66,-500);
        }
       }  
    }
    else if(thingType[i]==2)
    {
      drawwhite(i);
   
        if(val == '4')
        {
        if(430<thingY[i] && thingY[i]<500)
        {
        thingY[i] = 650; grade++; println(grade);
        fill(255,90);
        rect(73,475,66,-500);
        }
       }
    }
    else if(thingType[i]==3)
    {
      drawyellow(i);
   
        if(val == '3')
        {
        if(430<thingY[i] && thingY[i]<500)
        {
        thingY[i] = 650; grade++; println(grade);
        fill(#FFF700,90);
        rect(139,475,66,-500);
      }
     }
    }
    else if(thingType[i]==5)
    {
      drawblu2(i);
        if(val == '1')
        {
        if (430<thingY[i] && thingY[i]<500)
        {
          thingY[i] = 650; grade++; println(grade);
          fill(#0046FF,90);
          rect(293,475,66,-500);
        }
       }
    }
    else if(thingType[i]==5)
    {
      drawwhite2(i);

        if(val == '2')
        {
        if (430<thingY[i] && thingY[i]<500)
        {
          thingY[i] = 650; grade++; println(grade);
          fill(255,90);
          rect(227,475,66,-500);
        }
       }
    }
    thingY[i]+=4;//掉的速度
    if(thingY[i]>600) newThing(i);
   }
  if(count>0){
    count--;
    text("Time: "+count/50,170,300);
    }
    if(count==0){
      background(#000302);
      textSize(48);
      text("Score: "+grade,90,300);
      fill(0, 102, 153);
      return;
    }
}
void newThing(int i){
  thingY[i] = -random(200);
  thingX[i] = random(400);
  thingType[i] = int(random(5))+1;
}
void drawblu(int i){
  thingX[i]=int(random(13,13));
  image(blu,thingX[i],thingY[i],60,15);
}
void drawwhite(int i){
  thingX[i]=int(random(73,73));
  image(white,thingX[i],thingY[i],66,15);
}
void drawyellow(int i){
  thingX[i]=int(random(139,139));
  image(yellow,thingX[i],thingY[i],88,15);
}
void drawblu2(int i){
  thingX[i]=int(random(293,293));
  image(blu2,thingX[i],thingY[i],66,15);
}
void drawwhite2(int i){
  thingX[i]=int(random(227,227));
  image(white2,thingX[i],thingY[i],66,15);
}
void stop() {
  song.close();
  minim.stop();
  super.stop();
}

Arduino:

int switchStatea = 0;
int switchStateb = 0;
int switchStatec = 0;
int switchStated = 0;
int switchStatee = 0;
const int ledPin = 13;

void setup(){
   pinMode(ledPin, OUTPUT);
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
   Serial.begin(9600);
}

void loop(){
  switchStatea = digitalRead(A0);
   switchStateb = digitalRead(A1);
      switchStatec = digitalRead(A2);
      switchStated = digitalRead(A3);
      switchStatee = digitalRead(A4);

   if(switchStatea == HIGH) {Serial.write('1');}
   if(switchStateb == HIGH) {Serial.write('2');}
   if(switchStatec == HIGH) {Serial.write('3');}
   if(switchStated == HIGH) {Serial.write('4');}
   if(switchStatee == HIGH) {Serial.write('5');}
   delay(33);

}

沒有留言:

張貼留言