2019年1月3日 星期四

week17 矮矮的筆記

期末作品呈現
.
https://youtu.be/334yih0cmzA

import ddf.minim.*;
import processing.serial.*;
Serial myPort;
Minim minim;
AudioPlayer player , hit , brick , ball , die ,win ;
float[] brickY = new float[80];
float[] brickX = new float[80];
int [] bricklife = new int[80];
int old1=0,old2=0,old3=0,old4=0;
int button1=0,button2=0,button3=0,button4=0;
byte [] data=new byte[5];
int bX=400;
int a = 0 , score = 0 , state = 0 , base = 150 , brX = 80 , brY = 30 ,baX = 50 , baY = 50 ,ballX = 400 , ballY = 560 , ballVX = 0 , ballVY = 0 , ball2VX = 0 , ball2VY = 0 ,ball3VX = 0 , ball3VY = 0 ;
int ball2X = 400 , ball2Y = 560 , ball3X = 400 , ball3Y = 560 ;
boolean dieA=false,dieB=false,dieC=false;
void setup()
{
  size(800,600);
  myPort = new Serial(this,"COM7",9600);
  minim = new Minim(this);
  hit = minim.loadFile("hit.mp3");
  brick = minim.loadFile("brick.mp3");
  die = minim.loadFile("die.mp3");
  ball = minim.loadFile("ball.mp3");
  win = minim.loadFile("win.mp3");
  player = minim.loadFile("Halloween.mp3");
  player.play();
  fill(255,60,53);
  for(int i=0;i<80;i++)
  {
    brickX[i]=(i%10)*90;
    brickY[i]=int (i/10)*35;
    bricklife[i]=1;
  }
}
boolean flying =false,gameover=false,d=true,e=true,f=true,g=true;//d = a = 5 , e = a = 25 , f = a = 45 , g = a = 65
void draw()
{
  if(state==0)  drawOP();
  else if(state==1)drawfirst();
  else if(state==2)drawsecond();
  else if(state==3)drawthird();
  else if(state==4)drawend();
  else if(state==5)drawEN();
  getData();
  if(myPort.available()>0)
  {
    int val=myPort.read();
    bX=val*6;
    println(val);
  }
}
void getData()
{
  if(myPort.available()>0)
  {
  myPort.readBytes(data);
  old1=button1;
  old2=button2;
  old3=button3;
  old4=button4;
  button1=data[4];
  button2=data[1];
  button3=data[2];
  button4=data[3];
  println(data);
  int press=-1;
  if(data[4]==0 && old1==0) press=1;
  else if(data[1]==0 && old2==0) press=2;
  else if(data[2]==0 && old3==0) press=3;
  else if(data[3]==0 && old4==0) press=4;
 
  if(press==1)
  {
    state=1;
    die.pause();
    player.rewind();
    player.play();
  }
  else if(press==2)
  {
    state=2;
    die.pause();
    player.rewind();
    player.play();
  }
  else if(press==3)
  {
    state=3;
    die.pause();
    player.rewind();
    player.play();
  }
  else if(press==4) mousePressed();
  }
}
void drawOP()
{
  background(157,157,255);
  fill(255,255,0);
  textSize(72);
  text("CHOSE LEVELS",140,200);
  fill(232,98,247);
  rect(45, 290 , 200 , 95 );
  rect(295, 290 , 200 , 95 );
  rect(545, 290 , 200 , 95 );
  fill(17,238,200);
  textSize(48);
  text("LEVEL 1", 55  , 350 );
  text("LEVEL 2", 305 , 350 );
  text("LEVEL 3", 555 , 350 );
  /*if(keyPressed && key == '1' )state = 1 ;
  if(keyPressed && key == '2' )state = 2 ;
  if(keyPressed && key == '3' )state = 3 ;*/
}
void mousePressed()
{
 
  flying=true;
  if(dieA==false)
  {
    ballVY=-20; ballVX=3;
  }
  if(dieB==false)
  {
    ball2VX=2; ball2VY=10;
  }
  if(dieC==false )
  {
    ball3VX=5; ball3VY = 15;
  }
}
void drawfirst()
{
  background(0);
  fill(255,0,255);
  rect (bX-70,580,base ,20);
  fill(123,213,0);
  ellipse(ballX,ballY,baX,baY);
  fill(123,123,156);
  for(int i=0;i<80;i++)
  {
    if(bricklife[i]==0) continue;//brick will disppearance
    rect(brickX[i],brickY[i],brX,brY);
    if(brickX[i] < ballX+25 /*left*/&& ballX-25 < brickX[i]+80/*right*/ && abs(ballY-brickY[i])<30)
    {
      bricklife[i]=0;
      hit.rewind();
      hit.play();
      a++;
      score+=10;
    }
    if(bricklife[i]==0 )
    {
      if(ballX>=brickX[i])ballVX=10;
      if(ballX<=brickX[i]) ballVX=-10;
      if(ballY>=brickY[i]) ballVY=10;
      if(ballY<=brickY[i]) ballVY=-10;
    }//has problem
  }
  if( a > 5 && d )
  {
    base = 120 ;
    baX = 40 ;
    baY = 40 ;
    ball.rewind();
    ball.play();
    d = false;
  }
  if( a > 25 && e )
  {
    base = 100 ;
    baX = 30 ;
    baY = 30 ;
    ball.rewind();
    ball.play();
    e = false;
  }
  if( a > 45 && f )
  {
    base = 80 ;
    baX = 20 ;
    baY = 20 ;
    ball.rewind();
    ball.play();
    f = false;
  }
  if( a > 65 && g )
  {
    base = 60 ;
    baX = 10 ;
    baY = 10 ;
    ball.rewind();
    ball.play();
    g = false;
  }
  if(flying &&!gameover)
  {
    ballX+=ballVX;
    ballY+=ballVY;
    if(ballX < 0 ) ballVX = abs(ballVX);
    if(ballX > 780 ) ballVX = -abs(ballVX);
    if(ballY < 0 ) ballVY = abs(ballVY);
    if(ballY > 560 && abs(bX-ballX)<75)
    {
      ballVY = -abs(ballVY);
      ballY=560;
    }
    if(ballY > 600)
    {
      gameover=true;
      player.pause();
      die.rewind();
      die.play();
      state = 4 ;
      for(int i=0;i < 80 ; i++)   bricklife[i]=1;
      d = true;
      e = true;
      f = true;
      g = true;
    }
  }
  else ballX=bX;
  fill(255,200,80);
  textSize(60);
  text("Score:"+score,500,100);
  if(gameover)
  {

  }
  if( a == 72  )
  {
    player.pause();
    win.rewind();
    win.play();
    ballY = 560;
    for( int i = 0 ; i < 80 ; i++ ) bricklife[i] = 1 ;
  }
}
void drawsecond()
{
  background(0,0,128);
  fill(147,251,197);
  rect(bX-70 , 580 , base , 20 );
  fill(255,128,255);
  for(int i = 0 ; i < 80 ; i+=3 )
  {
    if (bricklife[i] == 0 ) continue ;
    rect(brickX[i],brickY[i],brX,brY);
  }
  fill(255,197,168);
  for( int i = 1 ; i < 80 ; i+=3)
  {
    if (bricklife[i] == 0 ) continue ;
    rect(brickX[i],brickY[i],brX,brY);
  }
  fill(228,249,134);
  for( int i = 2 ; i < 80 ; i+=3)
  {
    if (bricklife[i] == 0 ) continue ;
    rect(brickX[i],brickY[i],brX,brY);
  }
  fill(237,73,175);
  ellipse(ballX+2,ballY+2,baX-10,baY-10);
  for(int i = 0 ; i < 80 ; i++ )
  {
    if (bricklife[i] == 0 ) continue ;
    if(brickX[i] < ballX+20 && ballX-20 < brickX[i]+80 && abs(ballY-brickY[i])<30)
    {
      bricklife[i]=0;
      hit.rewind();
      hit.play();
      a++;
      score+=10;
    }
    if(bricklife[i]==0 )
    {
      if(ballX>=brickX[i] ) ballVX=-12;
      if(ballX<=brickX[i] ) ballVX=12;
      if(ballY==brickY[i] ) ballVY=12;
      if(ballY==brickY[i] ) ballVY=-12;
    }
  }
  if(flying &&!gameover)
  {
    ballX+=ballVX;
    ballY+=ballVY;
    if(ballY>560 && abs(bX-ballX)<75)ballVY=-abs(ballVY);
    if(ballY >600 )
    {
      gameover=true;
      player.pause();
      die.rewind();
      die.play();
      state = 4 ;
      for(int i=0;i < 80 ; i++)   bricklife[i]=1;
    }
    if(ballX < 0 ) ballVX = abs(ballVX);
    if(ballX > 780 ) ballVX = -abs(ballVX);
    if(ballY < 0 ) ballVY = abs(ballVY);
    if(ballY > 560 && abs(bX-ballX)<75)
    {
      ballVY = -abs(ballVY);
      ballY=560;
    }
  }
  else ballX=bX;
  fill(61,135,115);
  textSize(60);
  text("Score:"+score,500,100);
  if( a == 72  )
  {
    player.pause();
    win.rewind();
    win.play();
    ballY = 560;
    for( int i = 0 ; i < 80 ; i++ ) bricklife[i] = 1 ;
    state = 5;
  }
}

void drawthird()
{
  background(175,248,254);
  fill(255,255,60);
  rect(bX-70 , 580 , base , 20 );
  fill(255);
  for(int i = 1 ; i < 80 ; i+=6 )
  {
    if(bricklife[i] == 0 ) continue ;
    rect(brickX[i],brickY[i],brX-10,brY-10);
    if(brickX[i] < ballX+10 && ballX-10 < brickX[i]+60 && abs(ballY-brickY[i])<10)
    {
      bricklife[i]=0;
      hit.rewind();
      hit.play();
      a++;
      score+=10;
    }
    if(brickX[i] < ball2X+10 && ball2X-10 < brickX[i]+60 && abs(ball2Y-brickY[i])<10)
    {
      bricklife[i]=0;
      hit.rewind();
      hit.play();
      a++;
      score+=10;
    }
    if(brickX[i] < ball3X+10 && ball3X-10 < brickX[i]+60 && abs(ball3Y-brickY[i])<10)
    {
      bricklife[i]=0;
      hit.rewind();
      hit.play();
      a++;
      score+=10;
    }
    if(bricklife[i]==0)
    {
      if(ballX>=brickX[i]) ballVX=10;
      else if(ballX<=brickX[i]) ballVX=-10;
      else if(ballY>=brickY[i]) ballVY=10;
      else if(ballY<=brickY[i]) ballVY=-10;
      else if(ball2X>=brickX[i]) ball2VX=15;
      else if(ball2X<=brickX[i]) ball2VX=-15;
      else if(ball2Y>=brickY[i]) ball2VY=15;
      else if(ball2Y<=brickY[i]) ball2VY=-15;
      else if(ball3X>=brickX[i]) ball3VX=8;
      else if(ball3X<=brickX[i]) ball3VX=-8;
      else if(ball3Y>=brickY[i]) ball3VY=8;
      else if(ball3Y<=brickY[i]) ball3VY=-8;
    }
  }
  fill(72,164,255);
  ellipse(ballX+5,ballY+5,baX-20,baY-20);
  fill(100,150,200);
  ellipse(ball2X+5,ball2Y+5,baX-20,baY-20);
  fill(230,250,20);
  ellipse(ball3X+5,ball3Y+5,baX-20,baY-20);
 
  if(flying && !gameover)
  {
    ballX += ballVX;
    ballY += ballVY;
    ball2X += ball2VX;
    ball2Y += ball2VY;
    ball3X += ball3VX;
    ball3Y += ball3VY;
    if(ballY>800){ballVX=0;ballVY=0;dieA=true;}
    if(ball2Y>800){ball2VX=0;ball2VY=0;dieB=true;}
    if(ball3Y>800){ball3VX=0;ball3VY=0;dieC=true;}
    if(dieA==false && ballY>560 && abs(bX-ballX)<75)ballVY=-abs(ballVY);
    if(dieB==false && ball2Y>560 && abs(bX-ball2X)<75)ball2VY=-abs(ball2VY);
    if(dieC==false && ball3Y>560 && abs(bX-ball3X)<75)ball3VY=-abs(ball3VY);
   
    if(ballY >600 && ball2Y >600 && ball3Y >600)
    {
      gameover=true;
      player.pause();
      die.rewind();
      die.play();
      state = 4 ;
      for(int i=0;i < 80 ; i++)   bricklife[i]=1;
    }
    if(ballX < 0 ) ballVX = abs(ballVX);
    if(ball2X < 0 ) ball2VX = abs(ball2VX);
    if(ball3X < 0 ) ball3VX = abs(ball3VX);
    if(ballX > 780 ) ballVX = -abs(ballVX);
    if(ball2X > 780 ) ball2VX = -abs(ball2VX);
    if(ball3X > 780 ) ball3VX = -abs(ball3VX);
    if(ballY < 0 ) ballVY = abs(ballVY);
    if(ball2Y < 0 ) ball2VY = abs(ball2VY);
    if(ball3Y < 0 ) ball3VY = abs(ball3VY);
  }
  else
  {
    ballX = bX ;
    ball2X = bX ;
    ball3X = bX ;
  }
  fill(61,135,115);
  textSize(60);
  text("Score:"+score,500,100);
  if( a == 11  )
  {
    player.pause();
    win.rewind();
    win.play();
    ballY = 560;
    for( int i = 0 ; i < 80 ; i++ ) bricklife[i] = 1 ;
    state = 5;
  }
}
void drawend()
{
  background(53,18,194);
  fill(255,255,0);
  textSize(120);
  text("Game Over",80,200);
  fill(232,98,247);
  rect(45, 290 , 200 , 95 );
  rect(295, 290 , 200 , 95 );
  rect(545, 290 , 200 , 95 );
  fill(17,238,200);
  textSize(48);
  text("LEVEL 1", 55  , 350 );
  text("LEVEL 2", 305 , 350 );
  text("LEVEL 3", 555 , 350 );
  gameover = false;
  flying = false;
  dieA=false;dieB=false;dieC=false;
  a = 0;
  score = 0 ;
  baX = 50;
  baY = 50;
  base = 150;
  ballY = 560 ;ballX = bX;
  ball2Y = 560 ;ball2X = bX;
  ball3Y = 560 ;ball3X = bX;
  /*if(state==1 )
  {
    die.pause();
    player.rewind();
    player.play();
  }
  if(state==2)
  {
    die.pause();
    player.rewind();
    player.play();
  }
  if(state==3)
  {
    die.pause();
    player.rewind();
    player.play();
  }*/
}
void drawEN()
{
 background(252,107,205);
 fill(255,255,0);
 textSize(72);
 text("You Win!!!!!",200,200);
 fill(232,98,247);
 rect(45, 290 , 200 , 95 );
 rect(295, 290 , 200 , 95 );
 rect(545, 290 , 200 , 95 );
 fill(17,238,200);
 textSize(48);
 text("LEVEL 1", 55  , 350 );
 text("LEVEL 2", 305 , 350 );
 text("LEVEL 3", 555 , 350 );
 score = 0 ;
 a = 0 ;
 flying=false;
 ballY = 560 ;ballX = bX;
 ball2Y = 560 ;ball2X = bX;
 ball3Y = 560 ;ball3X = bX;
 dieA=false;dieB=false;dieC=false;
 /*if(state == 1 )
 {
   win.pause();
   player.rewind();
   player.play();
 }
 if(state == 2 )
 {
   win.pause();
   player.rewind();
   player.play();
 }
 if(state == 3 )
 {
   win.pause();
   player.rewind();
   player.play();
 }*/
}

作品解釋
第一關是正常關卡 用滑鼠玩也很難過關,所以用搖桿更是一大挑戰
第二關其實算放水關卡,用滑鼠玩很好過關,但搖桿就沒那麼容易了
第三關混淆視聽,兩種晚發都只要接到最慢的那顆球就能過關了


沒有留言:

張貼留言