2013年11月10日 星期日

Week10 葉玉婷 期中作品

遊戲影片

第一次為失血而Game over
第二次為沒準時回家而Game over
第三次為拿到鑰匙並準時回家而Win



遊戲畫面
開始畫面(遊戲解說及倒數)

小蝸回不了家
必須先拿完鑰匙才可以回家
在時間內完成這件事
PS.碰到其他人會扣血、吃到棒棒糖會加分呦

遊戲中畫面(左上角血量,左下角房子,右上角鑰匙,人物隨機由上往下,糖果隨機跑)

血量沒有時死掉的畫面

沒有在時間內拿鑰匙並回家

有在時間內拿鑰匙並回家(評價依吃到的棒棒糖數量評)




程式碼
int []sea=new int[1000];
int []seaNo=new int[1000];
int blood=30;  //血量
float ManX=25, ManY=480; //小蝸的位置
PImage []img = new PImage[4];
PImage bg; //背景圖
PImage bg0; //鳳梨屋
PImage key1; //鑰匙
PImage Man; //小蝸
int a=1; //判斷是否拿到鑰匙
int S=20; //判斷是否被人碰到或是吃到糖果
float [] ballX=new float [50],ballY=new float[50]; //隨機亂跑的棒棒糖
float [] ballVX=new float [50],ballVY=new float[50]; //隨機亂跑的棒棒糖
PImage imgBall; //棒棒糖

//音樂
import ddf.minim.*;
Minim m;
AudioPlayer player;
AudioPlayer da;

void setup()
{
  m=new Minim(this);

  for(int i=0;i<50;i++) //棒棒糖
    {
      newBall(i);
    }
  player=m.loadFile("C://Users//S405-14//Desktop//processing//music.mp3");  //背景音樂
  da=m.loadFile("C://Users//S405-14//Desktop//processing//001.mp3"); //小蝸走動的聲音
  player.play();  //播放
  imgBall=loadImage("C://Users//S405-14//Desktop//processing//candy.png"); //糖果圖
  bg = loadImage("C://Users//S405-14//Desktop//processing//back.jpg");  //背景圖
  bg0=loadImage("C://Users//S405-14//Desktop//processing//home.png"); //鳳梨屋圖
  Man= loadImage("C://Users//S405-14//Desktop//processing//e.png"); //小蝸圖
  key1=loadImage("C://Users//S405-14//Desktop//processing//key.png"); //鑰匙圖
  size(500, 500);  //視窗尺寸

  img[0]=loadImage("C://Users//S405-14//Desktop//processing//a.png"); //人物一
  img[1]=loadImage("C://Users//S405-14//Desktop//processing//b.png"); //人物二
  img[2]=loadImage("C://Users//S405-14//Desktop//processing//c.png"); //人物三
  img[3]=loadImage("C://Users//S405-14//Desktop//processing//d.png"); //人物四


  for (int i=0;i<1000;i++) //用來隨機產生人
  {
    sea[i]=int(random(4));
    seaNo[i]=int(random(4));
  }
}

int tick=0;
int now=0;
int total=0; //總分
int countDown=4*30; //開始前的倒數
int countDown2=60*30; //遊戲中的倒數

void draw()
{
  if (countDown>0) //遊戲開始前
  {
    background(0, 0, 0);
    countDown--; //倒數
    //遊戲解說
    text("小蝸被鎖在門外了!!  請幫助小蝸拿到鑰匙(右上角)", 150, 100);
    text("請注意別被其他人撞到囉!! (會扣血)", 150, 150);
    text("吃到棒棒糖可以加分喲!", 150, 200);
    text("倒數"+countDown/30+"秒", 150, 250);
    return;
  }

  if (countDown2>0) //遊戲開始
  {

    background(bg); //背景
    image(key1, 490, 20); //鑰匙位置
    if(a==0)//如果吃到鑰匙則消失
    {
      image(bg,250,250);
    }
 
    countDown2--; //倒數
    text("剩下"+countDown2/30+"秒", 250, 250); //顯示倒數時間

    for (int i=now;i<now+5;i++) //人物跑動的位置
    {
      image(img[seaNo[i]], sea[i]*100+100, tick+400-(i-now)*100); //人物在跑

      if(dist(sea[i]*100+100,tick+400-(i-now)*100,ManX,ManY)<S) //判斷有沒有接觸到人
      {
          /*sea[i]=500;*/
          blood-=1; //碰到血會變少

      }
    }
    tick+=2;
    if (tick>100)
    {
      tick=0;
      now++;
    }
 
    for(int i=0;i<50;i++) //棒棒糖隨機跑
    {
     image(imgBall,ballX[i],ballY[i],20,20);
     ballX[i]+=ballVX[i];
     ballY[i]+=ballVY[i];
     if(ballX[i]<0 || ballX[i]>500 || ballY[i]<0 || ballY[i]>500) //判斷有沒有超出視窗
     {
       newBall(i);
     }
     if(dist(ballX[i],ballY[i],ManX,ManY)<S) //判斷有沒有吃到棒棒糖
     {
       ballX[i]=0;
       total++;
     }
    }
    if (blood<=0) //血沒了
    {
      m.stop(); //音樂暫停
      background(0, 0, 0); //背景為黑
      text("Game over",250,150);
      text("死掉惹",250,200);
      text("You lose!", 250,250);
    }
  }
  else //血還有但是沒有成功達到任務
  {
    m.stop();
    background(0, 0, 0);
    fill(255,255,255);
    text("Game over",250,150);
    text("要趕快拿鑰匙回家啊!!!",250,200);
    text("You lose!", 250,250);
 
  }

  if(a==0 && ManX<=50 && ManY>=480) //有吃到鑰匙且成功回到屋子
  {
    m.stop(); //音樂暫停
    background(0, 0, 0); //背景為黑
    fill(255,255,255);
    text("You win!",250,150); //贏了
    text("恭喜小蝸賀喜小蝸",250,200);
    //評語
    if(total<=20)
      text("評價:蛤太弱了喔", 250,250);
    if(total<=40 && total>20)
      text("評價:普普通通啦", 250,250);
    if(total<=55 && total>40)
      text("評價:還不賴嘛!", 250,250);
    if(total>55)
      text("評價:厲害!", 250,250);
  }

  imageMode(CENTER);
  fill(255, 0, 0);
  rect(0, 0, blood*3, 50); //血量

  text("得"+total+"分", 250, 300); //倒數時間

  if(mouseX<100 && mouseY<50) //解說
    text("血量", 180, 30);
  if(mouseX>450 && mouseY<50) //解說
    text("鑰匙", 400, 30);
  if(mouseX<100 && mouseY>400) //解說
    text("鳳梨屋", 150, 450);

     
  if(ManX>=470 && ManY<=20) //如果拿到鑰匙
  {
     fill(0,0,0);
     text("得到鑰匙", 400, 20);
     text("快回家吧", 400, 50);
     a=0;
  }

    image(bg0, 40, 430,100,150); //鳳梨屋圖
    image(Man, ManX, ManY); //小蝸圖
}

void keyPressed()
{
  if (key=='8') //上
  {
    da.rewind();
    da.play();
    ManY-=20;
  }
  if (key=='2') //下
  {
    da.rewind();
    da.play();
    ManY+=20;
  }
  if (key=='6') //左
  {
    da.rewind();
    da.play();
    ManX+=20;
  }
  if (key=='4') //右
  {
    da.rewind();
    da.play();
    ManX-=20;
  }
}

void newBall(int i) //隨機棒棒糖
{
  float angle=random(2*PI);
  ballVX[i]=cos(angle);
  ballVY[i]=sin(angle);
  int dir=int(random(4));

  if(dir==0)
  {
     ballX[i]=random(600); ballY[i]=0;
  }
  else if(dir==1)
  {
     ballX[i]=random(600); ballY[i]=0;
  }
  else if(dir==2)
  {
     ballX[i]=0; ballY[i]=random(600);
  }
  else if(dir==3)
  {
     ballX[i]=600; ballY[i]=random(600);
  }

}

沒有留言:

張貼留言