2018年1月22日 星期一

Week18

processing : 完成品 打靶
youtube:打靶


import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player1;
Serial myPort;
int val;
PImage []img = new PImage[10];
PImage[] air = new PImage[7];
PImage target;
PImage target1;
PImage back;
float []airx = new float [3];
float []airy = new float [3];
float px[] = new float [10];
float py[] = new float [10];

int count=0;
int timer=3051;
float movex=0, movey=0;
float speedx=5,speedy=5;
float movex1=700, movey1=0;
float speedx1=10,speedy1=10;

boolean air0xy = true;
boolean air1xy = true;
boolean air2xy = true;

boolean pig0 = true;
boolean pig1 = true;
boolean pig2 = true;
boolean pig3 = true;
boolean pig4 = true;

boolean t = true;
void setup()
{
  size(700,500);
  minim = new Minim(this);
  player1 = minim.loadFile("sound.mp3");

    target = loadImage("target.png");
    target1 = loadImage("target2.png");
    air[0] = loadImage("5.png");
    air[1] = loadImage("5.png");
    air[2] = loadImage("5.png");
    img[0] = loadImage("1.png");
    img[1] = loadImage("2.png");
    img[2] = loadImage("3.png");
    img[3] = loadImage("4.png");
    img[4] = loadImage("4.png");
    back = loadImage("a.jpg");
    px[0] = 200;
    px[1] = 300;
    px[2] = 250;
    px[3] = 250;
    px[4] = 250;
    py[0] = 250;
    py[1] = 320;
    py[2] = 400;
    py[3] = 450;
    py[4] = 250;
     
    airx[0] = 500;
    airy[0] = 150;
    airx[1] = 700;
    airy[1] = 70;
    airx[2] = 550;
    airy[2] = 100;
    myPort = new Serial(this,"COM6",115200);
}

void draw()
{
  if(timer>10)
  {
   player1.play();
   timer-=2.5;
   background(220);
   image(back,350,250,width,height);
   imageMode(CENTER);
   textSize(32);
   fill(#AA0202);
   text("Count:"+int(count),450,50);
   text("Time:"+int(timer/60),40,50);
   textSize(25);
   fill(#AA0202);
   airxy();
   pig();
   move();
   image(target,movex,movey,60,60);
   image(target1,movex1,movey1,40,40);
  }
  else end();
}

void move()
{
  movex = movex+speedx;
  movey = movey+speedy;
  movex1 = movex1+speedx1;
  movey1 = movey1+speedy1;
  if(dist(movex,movey,movex1,movey1)<51){
    speedx = speedx*-1;
    speedy = speedy*-1;
    speedx1 = speedx1*-1;
    speedy1 = speedy1*-1;
  }

  if(movex>640){
    speedx = -15;
  }
  if(movex<40){
    speedx = 15;
  }
  if(movey>440){
    speedy = -15;
  }
  if(movey<40){
    speedy = 15;
  }
   if(movex1>660){
    speedx1 = -10;
  }
  if(movex1<40){
    speedx1 = 10;
  }
  if(movey1>460){
    speedy1 = -10;
  }
  if(movey1<40){
    speedy1 = 10;
  }
}

void end()
{
  background(0);

   textSize(100);
   fill(255);
   text("Score  "+int(count),50,50);


   fill(255,50,100);
   textSize(100);
   text("END",0,500);

}

void keyPressed(){
  timer=3051;
  count=0;
}

airplane:
void airxy()
{
     if(air0xy)
  {
      airx[0]-=20;
      image(air[0],airx[0],airy[0],180,180);
  }

     if(air1xy)
  {
      airx[1]-=15;
      image(air[1],airx[1],airy[1],150,150);
  }

     if(air2xy)
  {
      airx[2]-=10;
      image(air[2],airx[2],airy[2],120,120);
  }

  if(airx[0]<-150){
    airx[0]=700;
  }
  if(airx[1]<-150){
    airx[1]=1000;
  }
  if(airx[2]<-200){
    airx[2]=750;
  }
  if(myPort.available()>0){
    int pin2 = myPort.read();
    println(pin2);
  if(dist(movex,movey,airx[0],airy[0])<30 && pin2>0){
   air0xy=false;
  }
  if(dist(movex,movey,airx[1],airy[1])<30 && pin2>0){
   air1xy=false;
  }
  if(dist(movex,movey,airx[2],airy[2])<30 && pin2>0){
   air2xy=false;
  }

   if(dist(movex1,movey1,airx[0],airy[0])<30 && pin2>0){
   air0xy=false;
  }
  if(dist(movex1,movey1,airx[1],airy[1])<30 && pin2>0){
   air1xy=false;
  }
  if(dist(movex1,movey1,airx[2],airy[2])<30 && pin2>0){
   air2xy=false;
  }
    if(pin2>0 && air0xy==false)
  {
    air0xy=true;
    airx[0] = 1000;
    airy[0] = 150;
    count++;
  }
   if(pin2>0 && air1xy==false)
  {
    air1xy=true;
    airx[1] = 1000;
    airy[1] = 70;
    count++;
  }
   if(pin2>0 && air2xy==false)
  {
    air2xy=true;
    airx[2] = 1000;
    airy[2] = 100;
    count++;
    }
  }



}

pig:

void pig()
{
    if(pig0)
  {
    image(img[0],px[0],py[0],100,100);
  }
  if(pig1)
  {
    image(img[1],px[1],py[1],80,80);
  }
  if(pig2)
  {
    image(img[2],px[2],py[2],150,150);
  }
  if(pig3)
  {
    image(img[3],px[3],py[3],150,150);
  }
  if(pig4)
  {
    image(img[4],px[4],py[4],150,150);
  }
  if(myPort.available()>0){
    int pin2 = myPort.read();
  if(dist(movex,movey,px[0],py[0])<50 && pin2>0){
    pig0 = false;
  }
  if(dist(movex,movey,px[1],py[1])<50 && pin2>0){
    pig1 = false;
  }
  if(dist(movex,movey,px[2],py[2])<50 && pin2>0){
    pig2 = false;
  }
  if(dist(movex,movey,px[3],py[3])<50 && pin2>0){
    pig3 = false;
  }
  if(dist(movex,movey,px[4],py[4])<50 && pin2>0){
    pig4 = false;
  }

   if(dist(movex1,movey1,px[0],py[0])<50 && pin2>0){
    pig0 = false;
  }
  if(dist(movex1,movey1,px[1],py[1])<50 && pin2>0){
    pig1 = false;
  }
  if(dist(movex1,movey1,px[2],py[2])<50 && pin2>0){
    pig2 = false;
  }
  if(dist(movex1,movey1,px[3],py[3])<50 && pin2>0){
    pig3 = false;
  }
  if(dist(movex1,movey1,px[4],py[4])<50 && pin2>0){
    pig4 = false;
  }

  if(pig0==false&&pin2>0)
  {
    pig0=true;
    px[0] = random(40,650);
    py[0] = random(250,300);
    count++;
  }
    if(pig1==false&&pin2>0)
  {
    pig1=true;
    px[1] = random(40,650);
    py[1] = random(300,350);
    count++;
  }
    if(pig2==false&&pin2>0)
  {
    pig2=true;
    px[2] = random(40,650);
    py[2] = random(350,400);
    count++;
  }
   if(pig3==false&&pin2>0)
  {
    pig3=true;
    px[3] = random(75,625);
    py[3] = random(400,425);
    count++;
  }
   if(pig4==false&&pin2>0)
  {
    pig4=true;
    px[4] = random(75,625);
    py[4] = random(400,425);
    count++;
    }
  }

}

arduino:
static int ledStatus;
static unsigned long lastDebounceTime;
#define DEBOUNCE_DELAY 200

void setup(){
  Serial.begin(115200);
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);

  ledStatus = LOW;
  digitalWrite(13, ledStatus);
}

void updateLed(){
  unsigned long currentTime = millis();
  if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
    lastDebounceTime = currentTime;
 
    ledStatus = ledStatus == HIGH ? LOW : HIGH;
    digitalWrite(13, ledStatus);
    Serial.println(ledStatus);
  }
}

void loop(){
  int switchStatus = digitalRead(2);

  if(switchStatus == HIGH){
    updateLed();
  }
}

沒有留言:

張貼留言