2018年1月22日 星期一

Week 19 期末作品展示

04160214 郭晉瑜
04162523 劉旆君




Ardiuno 程式碼
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output


void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.write(digitalRead(2));
  Serial.write(analogRead(A0)/4);
  Serial.write(analogRead(A1)/4);
  delay(150);
}

Processing程式碼
import processing.serial.*;
Serial myPort;
PImage img0,img1,img2,img3,bg;
int stage=0;
int time=0;
void setup(){
  size(600,500);
  img0 = loadImage("Crab.png");
  img1 = loadImage("Bear.png");
  img2 = loadImage("Panda.png");
  img3 = loadImage("fox.png");
  bg = loadImage("sky.jpg");
  myPort = new Serial(this, "COM3", 115200);
}
float grabX=50, grabY=50;
float bearX=150,bearY=200;
float pandaX=350,pandaY=250;
float foxX=500,foxY=300;
float holeX=100,holeY=400;
boolean Gotit=false;
boolean Gotit1=false;
boolean Gotit2=false;
boolean bearAlive=true;
boolean pandaAlive=true;
boolean foxAlive=true;
void draw(){
  background(bg);
 if(myPort.available()>0){
 /*   t=myPort.read();
    if(t=='w') pin2=myPort.read();
    else if(t=='x') x=myPort.read();
    else if(t=='y') y=myPort.read();*/
    int pin2=myPort.read();
    int x=myPort.read();
    int y=myPort.read();
    analyze(y,x);
    if(pin2==1 && dist(grabX,grabY,bearX,bearY)<50)
    {
      Gotit=true;
    }
    else if(dist(bearX,bearY,holeX,holeY)<100){
      bearAlive=false;
    }
    if(pin2==1 && dist(grabX,grabY,pandaX,pandaY)<50){
      Gotit1=true;
    }
    else if(dist(pandaX,pandaY,holeX,holeY)<100){
      pandaAlive=false;
    }
    if(pin2==1 && dist(grabX,grabY,foxX,foxY)<50){
      Gotit2=true;
    }
    else if(dist(foxX,foxY,holeX,holeY)<100){
      foxAlive=false;
    }
    println(x+" "+y);
  }
  if(stage==0){
    textSize(80);
    text("start game",125,250);
  }
  else if(stage==1){
    ellipse(holeX,holeY,100,100);
    if(bearAlive) image(img1,bearX,bearY,100,100);
    if(pandaAlive) image(img2,pandaX,pandaY,100,100);
    if(foxAlive) image(img3,foxX,foxY,100,100);
    fill(200); image(img0,grabX,grabY,120,100);
    if(keyCode==' ' && dist(grabX,grabY,bearX,bearY)<50){
      Gotit=true;
    }
    else if(dist(bearX,bearY,holeX,holeY)<100){
      bearAlive=false;
    }
    if(keyCode==' ' && dist(grabX,grabY,pandaX,pandaY)<50){
      Gotit1=true;
    }
    else if(dist(pandaX,pandaY,holeX,holeY)<100){
      pandaAlive=false;
    }
    if(keyCode==' ' && dist(grabX,grabY,foxX,foxY)<50){
      Gotit2=true;
    }
    else if(dist(foxX,foxY,holeX,holeY)<100){
      foxAlive=false;
    }
    textSize(30);
    text("Time:"+time/60,5,35);
    time--;
    if(time<0) stage=2;
 
  }
  else if(stage==2)
  {
    textSize(80);
    text("Game Over",125,250);
  }
}
void mousePressed(){
  if(stage==0){
    stage=1;   time=60*60;
    grabX=50;  grabY=50;
    bearX=150; bearY=200;
    pandaX=350;pandaY=250;
    foxX=500;  foxY=300;
    holeX=100; holeY=400;
    Gotit=false;
    Gotit1=false;
    Gotit2=false;
    bearAlive=true;
    pandaAlive=true;
    foxAlive=true;
  }
  if(stage==2){
    stage=0;
  }
}
void analyze(int x, int y){
  if(y>200){
    grabY-=7;
    if(Gotit) bearY-=7;
    if(Gotit1) pandaY-=7;
    if(Gotit2) foxY-=7;
  }
  if(y<50){
    grabY+=7;
    if(Gotit) bearY+=7;
    if(Gotit1) pandaY+=7;
    if(Gotit2) foxY+=7;
  }
  if(x<50){
    grabX-=7;
      if(Gotit) bearX-=7;
      if(Gotit1) pandaX-=7;
      if(Gotit2) foxX-=7;
  }
  if(x>200){
    grabX+=7;
      if(Gotit) bearX+=7;
      if(Gotit1) pandaX+=7;
      if(Gotit2) foxX+=7;
   }
}
void keyPressed(){
  if(keyCode==UP){
    grabY-=7;
    if(Gotit) bearY-=7;
    if(Gotit1) pandaY-=7;
    if(Gotit2) foxY-=7;
  }
  if(keyCode==DOWN){
    grabY+=7;
      if(Gotit) bearY+=7;
      if(Gotit1) pandaY+=7;
      if(Gotit2) foxY+=7;
  }
  if(keyCode==LEFT){
    grabX-=7;
      if(Gotit) bearX-=7;
      if(Gotit1) pandaX-=7;
      if(Gotit2) foxX-=7;
  }
  if(keyCode==RIGHT){
    grabX+=7;
      if(Gotit) bearX+=7;
      if(Gotit1) pandaX+=7;
      if(Gotit2) foxX+=7;
   }
}



沒有留言:

張貼留言