2015年1月18日 星期日

Week19_期末作品

跳跳火柴人:

玩法:
人需要穿上拖鞋,隨著遊戲人物一起跳躍,穿越障礙物來獲取高分。
開始和重新只需要用力在跳一下,即可再次遊戲。

 p語言:
import processing.serial.*;//
Serial myPort,myPort1;//
import ddf.minim.*;///
Minim minim;///
AudioPlayer bgMusic,jumpMusic,gg;///

people p = new people();
wall[] w = new wall[3];
boolean end=false;
boolean intro=true;
boolean over=true;///
boolean GG=true;///
int score=0;


void setup()
{
  size(550, 750);
  myPort=new Serial(this, "COM4", 9600);//
  minim = new Minim(this);///
  for (int i = 0; i<3; i++)
  {
    w[i]=new wall(i);
  }
  strokeWeight(5);
}

void draw()
{
  background(255);
  if (end)
  {
    p.move();
  }
  p.people();
  if (end)
  {
    p.drag();
  }
  p.checkCollisions();
  for (int i = 0; i<3; i++)
  {
    w[i].drawwall();
    w[i].checkPosition();
  }
  fill(255);
  stroke(0);

  textSize(32);
  if (end)
  {
    rect(20, 20, 100, 50);
    fill(0);
    text(score, 30, 58);
  }
  else
  {
    rect(150, 100, 230, 50);
    rect(150, 200, 200, 50);
    fill(0);
    if (intro)
    {
      text("Press space or", 155, 140);
      text("Click to Play", 155, 240);
    }
    else
    {
      text("Game over", 170, 140);
      text("Score", 180, 240);
      text(score, 280, 240);
      bgMusic.close();///
    }
  }


  if (myPort.available()>0&&myPort.read()==100)
  {
    p.jump();
    intro=false;
    if (end==false)
    {
      reset();
    }
  }
  /*if (myPort.available()>0&&myPort.read()==97)
  {
    intro=false;
  if (end==false)
  {
    reset();
  }
  }*/
}

class people
{
  float xPos, yPos, ySpeed;
  people()
  {
    xPos = 250;
    yPos = 400;
  }

  void people()//people
  {
    fill(0, 0, 128);
    ellipse(xPos+5, yPos+40, 30, 30);
    rect(xPos, yPos+55, 10, 25); //body

    rect(xPos+5, yPos+60, 20, 5); //right hand
    rect(xPos+20, yPos+50, 5, 10); //right hand

    rect(xPos+5, yPos+75, 20, 5); //right feet
    rect(xPos+20, yPos+80, 5, 12); //right feet

    rect(xPos-15, yPos+60, 20, 5); //left hand
    rect(xPos-15, yPos+65, 5, 7); //left hand

    rect(xPos-15, yPos+75, 20, 5); //left feet
    rect(xPos-15, yPos+80, 5, 12); //left feet
  }

  void jump()
  {
    ySpeed=-10;
    jumpMusic = minim.loadFile("jump.mp3");///
    jumpMusic.play();
  }

  void drag()
  {
    ySpeed+=0.4;
  }

  void move()
  {
    yPos+=ySpeed;
    for (int i = 0; i<3; i++)
    {
      w[i].xPos-=3;
    }
  }

  void checkCollisions()
  {
    if (yPos>750)
    {
      end=false;
    }
    for (int i = 0; i<3; i++)
    {
      if ((xPos<w[i].xPos+15&&xPos>w[i].xPos-15)&&(yPos<w[i].wallY-125||yPos>w[i].wallY+45))
      {
        end=false;
      }
    }
  }
}

class wall
{
  float xPos, wallY, xObj;
  boolean cashed = false;
  wall(int i)
  {
    xPos = 100+(i*200);  
    wallY = random(500)+100;
  }

  void drawwall()
  {

    stroke(255, 0, 0);

    line( xPos, 0, xPos, wallY-100);
    line( xPos, wallY+130, xPos, 750);
  }

  void checkPosition()
  {
    if (xPos<0)
    {
      xPos+=(200*3);
      wallY = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false)
    {
      cashed=true;
      score++;
    }
  }
}

void reset()
{
  end=true;
  score=0;
  p.yPos=400;
  for (int i = 0; i<3; i++)
  {
    w[i].xPos+=550;
    w[i].cashed = false;
  }
  if(over)
    {
      bgMusic = minim.loadFile("bgmusic1.mp3");///
      bgMusic.play();
    }
 
}

void mousePressed()
{
  p.jump();
  intro=false;
  if (end==false)
  {
    reset();
  }
}

void keyPressed()
{
  p.jump();
  intro=false;
  if (end==false)
  {
    reset();
  }
}

arduino:
void setup(){
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(12, OUTPUT); 
  //pinMode(13, OUTPUT); 
}
int a=0;
void loop(){
  int sensorVal = digitalRead(2);
  int sensorVal2 = digitalRead(4);

  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } 
  else {
    digitalWrite(13, HIGH);
    Serial.write(100);
    delay(100);
  }
  
  if(sensorVal2 == LOW)a=0;
  if(sensorVal2 == HIGH && a==0){Serial.write(97); a=1;}
  /*if (sensorVal2 == HIGH) {
    digitalWrite(12, LOW);
  } 
  else {
    digitalWrite(12, HIGH);
    Serial.write(65);
    delay(100);
  }*/
}


沒有留言:

張貼留言