2020年10月12日 星期一

week05 水果忍者

今天要來做個小遊戲啦~

不過今天在打程式的時候忽然停電了,資料都不見了...雖然說也沒太多進度


首先先下載一顆水果的圖片,加入速寫本,然後:

PImage img;

float x=100, y=500, vx=3,vy=-30;

void setup(){

  size(500,500);

  img=loadImage("fruit.png");

}

void draw(){

  clear();

  image(img,x,y,100,100);

  x+=vx;

  y+=vy;

  vy+=0.98;

  

  if(y>500)

  {

    x=random(400); y=500; vx=random(-5,5); vy=-30;

  }

}


PImage img;

float x=100, y=500, vx=3,vy=-30;

int fruitDie=0;

void setup(){

  size(500,500);

  img=loadImage("fruit.png");

}

void draw(){

  clear();

  image(img,x,y,100,100);

  x+=vx;

  y+=vy;

  vy+=0.98;

  

  if(y>500) fruitDie=1;

  if(x<mouseX&&mouseX<x+100 && y<mouseY&&mouseY<y+100)fruitDie=1; (★)

  stroke(255); line(mouseX,mouseY,pmouseX,pmouseY);

  

  if(fruitDie==1){

    x=random(100); y=500; vx=random(-5,5); vy=-30;

    fruitDie=0;

  }

}

追加動作偵測擴充 (必須要有動作才算切到)


int mouseMove=0;(宣告時)

 if(x<mouseX&&mouseX<x+100 && y<mouseY&&mouseY<y+100 && mouseMove==1)fruitDie=1; (★替換)

if(mouseX==pmouseX && mouseY==pmouseY) mouseMove=0; (void draw()內追加)

else mouseMove=1; (void draw()內追加)



追加聲音擴充 (在此之前請先下載額外的library (共兩個:"Sound"+"Minim"))


import processing.sound.*;(最初兩行)

SoundFile sound1;(最初兩行)


sound1= new SoundFile(this, "Clean-Slice-1.wav"); (void setup()內追加)

if(x<mouseX&&mouseX<x+100 && y<mouseY&&mouseY<y+100) sound1.play(); fruitDie=1; (★替換)







(我的最終成果)

import processing.sound.*;

SoundFile sound1;

SoundFile sound2;

SoundFile sound3;

SoundFile sound4;


PImage img;

float x=100, y=500, vx=3,vy=-30;

int fruitDie=0;

int point=0;

int mouseMove=0;

void setup(){

  size(500,500);

  sound1= new SoundFile(this, "blade-ink-2.wav");

  sound2= new SoundFile(this, "Bonus-Explosion-1.wav");

  sound3= new SoundFile(this, "Bonus-Explosion-3.wav");

  sound4= new SoundFile(this, "Bonus-Explosion-5.wav");

  img=loadImage("fruit.png");

}

void draw(){

  clear();

  image(img,x,y,100,100);

  x+=vx;

  y+=vy;

  point++;

  vy+=0.98;

  

  if(y>500) fruitDie=1;

  if(x<mouseX&&mouseX<x+100 && y<mouseY&&mouseY<y+100 && mouseMove==1)

  {

    sound1.play();

    fruitDie=1;

    if(point<10) sound4.play();

    else if(point<20) sound3.play();

    else if(point<30)sound2.play();

  }

  stroke(255); line(mouseX,mouseY,pmouseX,pmouseY);

  

  if(mouseX==pmouseX && mouseY==pmouseY) mouseMove=0;

  else mouseMove=1;

  

  if(fruitDie==1){

    

    x=random(400); y=500; vx=random(-5,5); vy=-30;

    fruitDie=0;

    point=0;

  }

}





沒有留言:

張貼留言