2015年1月19日 星期一

week19

------------------------------------------Arduino--------------------------------------

void setup(){
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
}

void loop(){
  while(Serial.available() > 0){
   char data = Serial.read();
   Serial.print(data);
   }
  if(digitalRead(2)==HIGH) Serial.write(97);
  if(digitalRead(4)==HIGH) Serial.write(100);
  delay(100);
}


-----------------------------------------Processing-------------------------------------

import processing.serial.*;
Serial myPort;
import ddf.minim.*;
Minim minim;
AudioPlayer start, boom;
PImage over;
int [] cubeX = new int [17];
float [] high = new float [17];

void setup() {
  size(750, 300);
  minim = new Minim(this);
  start = minim.loadFile("spider man.mp3");
  boom = minim.loadFile("boom.mp3");
   start.play();
  for (int i=0; i<17; i++) {
    cubeX[i]=i*50;
    high[i]=random(100, 150);
  }
  //myPort = new Serial(this, "COM3", 9600);

  over = loadImage("gameover.png");
}
int posY = 180;
int a = 0;
int q=1;
int posX = 70;
int score = 0;
float speed = 1.0;
void draw() {
  background(255);

  if (a==0) {
    textSize(50);
    text("press T to START", 130, 150);
    fill(0, 102, 153, 500);

    if (keyPressed) {
      if(key == 't') a=1; 
    }
    
  } else if (a==1) {
    fill(255, 0, 0);
    ellipse(posX, posY, 40, 40);
    fill(0);
    if (keyPressed) {
      if (key=='w') posY-=3;
      if (key=='s') posY+=3;
    }
    for (int m=0; m<17; m++) {
      rect(cubeX[m], 0, 50, high[m]);
      rect(cubeX[m], high[m]+100, 50, 250-high[m]);
      if (cubeX[m]<-50) { 
        cubeX[m]+=800;
        score++;
        println(score);
        //speed+=0.05;
      }
      cubeX[m]-=3;      

      if (sqrt((cubeX[m]-posX)*(cubeX[m]-posX)+(high[m]-posY)*(high[m]-posY))<20 | sqrt((cubeX[m]-posX)*(cubeX[m]-posX)+(high[m]+100-posY)*(high[m]+100-posY))<20 | sqrt((cubeX[m]+50-posX)*(cubeX[m]+50-posX)+(high[m]-posY)*(high[m]-posY))<20 | sqrt((cubeX[m]+50-posX)*(cubeX[m]+50-posX)+(high[m]+100-posY)*(high[m]+100-posY))<20 && posX>cubeX[m] && posX<(cubeX[m]+50)) {
        a=2;
        //image(over, 0, 0, 800, 300);
        //println("Game Over");
      }
    }

    /*if (myPort.available()>0 ) {
     int index=myPort.read();
     //println(index);
     if (index==100)  posY+=3;
     else if (index==97) posY-=3;
     //println(posY);
     }*/
  } else if (a==2) {
    image(over, 0, 0, 800, 300);
    println("Game Over");
    start.close();
    boom.play();
    score = 0;
    if (keyPressed) {
      if (key=='r') {
        boom.close();
        a=0;
      }
    
    }
  }
}



沒有留言:

張貼留言