2020年1月2日 星期四

week17

期末作品
https://youtu.be/-Tq9bn92Gig


import processing.sound.*;
SoundFile player;
SoundFile player1;
PImage []Bg = new PImage[10];
PImage []imgRole = new PImage[4];
PImage []Button = new PImage[7];
PImage []Trap = new PImage[4];
PImage []Trap1 = new PImage[4];
PImage []Fire = new PImage[4];
PImage []Add = new PImage[2];
PImage []Add1 = new PImage[2];
PImage []Fire1 = new PImage[4];
PImage []num=new PImage[10];
PImage []Heart=new PImage[2];
PImage imgTemp, imgNow, imgBuilding;
float roleX=50, roleY=700, roleVX=4, roleVY=0;
float x=0, shiftX=0, x1=100, y1=400, x3=550, y3=400, x4=325, y4=400, x5=335, y5=500, x6=650, y6=680, x7=650, y7=680;
int roleID=0, jumping=0, stage=0, life=100, wid=110, hei=100, wid1=110, hei1=100, wid2=110, hei2=100, wid3=110, hei3=100, wid4=110, hei4=100, wid5=110, hei5=100;
boolean flying=false;
boolean Touch(float x, float y, float x2, float y2) {
  if (x1<x2 && x2<x1+100 && y1<y2 && y2<y1+100) return true;
  else return false;
}
boolean Touch1(float x3, float y3, float x2, float y2) {
  if (x3<x2 && x2<x3+100 && y3<y2 && y2<y3+100) return true;
  else return false;
}
boolean Touch2(float x4, float y4, float x2, float y2) {
  if (x4<x2 && x2<x4+100 && y4<y2 && y2<y4+100) return true;
  else return false;
}
boolean Touch3(float x5, float y5, float x2, float y2) {
  if (x5<x2 && x2<x5+100 && y5<y2 && y2<y5+100) return true;
  else return false;
}
boolean Touch4(float x6, float y6, float x2, float y2) {
    if (x6<x2 && x2<x6+100 && y6<y2 && y2<y6+100) return true;
    else return false;
}
boolean Touch5(float x7, float y7, float x2, float y2) {
    if (x7<x2 && x2<x7+100 && y7<y2 && y2<y7+100) return true;
    else return false;
}
void setup() {
  size(800, 800);
  player = new SoundFile(this,"music.wav");
  player1 = new SoundFile(this,"game.wav");
  player.play();
  Bg[1] = loadImage("bg.png");
  Bg[2] = loadImage("bg2.png");
  Bg[3] = loadImage("start.png");
  Bg[4] = loadImage("pass.png");
  Bg[5] = loadImage("trapaddbg.png");
  Bg[6] = loadImage("hurt.png");
  Bg[7] = loadImage("fail.png");
  Bg[8] = loadImage("rule.png");
  Bg[9] = loadImage("fix.png");
  Add[1] = loadImage("addlife0.png");
  Add1[1] = loadImage("addlife1.png");
  Bg[1].loadPixels();
  Bg[5].loadPixels();
  Button[1]= loadImage("pausebig.png");
  Button[2]= loadImage("rebig.png");
  Button[3]= loadImage("startsmall.png");
  Button[4]= loadImage("q small.png");
  Button[5]= loadImage("s.png");
  Heart [1]= loadImage("heart.png");
  for (int i=0; i<4; i++) imgRole[i]=loadImage("dog"+i+".png");
  rectMode(CENTER);
  for (int i=0; i<4; i++) Trap[i]=loadImage("trap"+i+".png");
  rectMode(CENTER);
  for (int i=0; i<4; i++) Trap1[i]=loadImage("trap"+i+".png");
  rectMode(CENTER);
  for (int i=0; i<4; i++) Fire[i]=loadImage("fire"+i+".png");
  rectMode(CENTER);
  for (int i=0; i<4; i++) Fire1[i]=loadImage("fire"+i+".png");
  rectMode(CENTER);
  for (int a=0; a<10; a++)num[a]=loadImage(a+".png");
}
void draw() {
  int nextX=int(roleX)+int(roleVX);
  int nextY=int(roleY);
  boolean touched = Touch(x1, y1, mouseX, mouseY);
  boolean touched1 = Touch1(x3, y3, mouseX, mouseY);
  boolean touched2 = Touch2(x4, y4, mouseX, mouseY);
  boolean touched3 = Touch3(x5, y5, mouseX, mouseY);
  boolean touched4 = Touch4(x6, y6, mouseX, mouseY);
  boolean touched5 = Touch5(x7, y7, mouseX, mouseY);
  if (nextY>=800-99) {
    nextY=800-100;
    roleY=800-100;
  }
  background(255);
  image(Bg[2], -shiftX, 0);
  if (stage==0) {
    roleX=6;
    roleY=625;
    background(Bg[3]);
    image(Button[3], x1, y1, wid, hei);
    image(Button[4], x3, y3, wid1, hei1);
    image(Button[5], x4, y4, wid2, hei2);
    if (touched==true) {
      wid=120;
      hei=110;
      if (mousePressed){
        stage=1;
        player.pause();
        player1.play();
      }
    }
    if (touched==false) {
      wid=110;
      hei=100;
    }
    if (touched1==true) {
      wid1=120;
      hei1=110;
      if (mousePressed) stage=4;
    }
    if (touched1==false) {
      wid1=110;
      hei1=100;
    }
    if (touched2==true) {
      wid2=120;
      hei2=110;
      if (mousePressed) stage=5;
    }
    if (touched2==false) {
      wid2=110;
      hei2=100;
    }
    image(imgRole[roleID], roleX, roleY, 150, 150);
    if (frameCount%30==0) {
      roleID=(roleID+1)%4;
    }
    int roleX2=300, roleY2=625;
    image(imgRole[roleID], roleX2, roleY2, 150, 150);
    if (frameCount%60==0) {
      roleID=(roleID+1)%4;
    }
    int roleX3=620, roleY3=625;
    image(imgRole[roleID], roleX3, roleY3, 150, 150);
    if (frameCount%60==0) {
      roleID=(roleID+1)%4;
    }
  }
  if (stage==1) {
    if (nextY>=0 && Bg[1].pixels[(int(nextX)+int(shiftX))+int(nextY)*3500]==color(0)) 
    {
      roleY--;
      nextY=int(roleY);
      jumping=0;
      roleVY=0;
      flying=false;
    }

    if (jumping==1) {
      roleY +=roleVY; 
      roleVY +=0.9;
    } else if ( Bg[1].pixels[(int(nextX)+int(shiftX))+int(nextY+1)*3500]!=color(0)) {
      roleY+=20;
    }
    if (roleY>697) {
      roleY=697;
      roleVY=0;
      flying=false;
    }
    if (keyCode==RIGHT||keyCode==UP||keyCode==DOWN||keyCode==LEFT) {

      if (nextY>=0 && Bg[1].pixels[(int(nextX)+int(shiftX))+int(nextY)*3500] == color(255)) {
        roleX=nextX;
        if (roleX>400 && shiftX<=2700) {
          shiftX += roleX-400;
          roleX=400;
        }
        if (roleX>690) {
          roleX=690;
        }
      }
      if (nextY>=0 && Bg[1].pixels[(int(nextX)+int(shiftX))+int(nextY)*3500] == color(255, 255, 0)) {
        stage=2;
      }
      if (nextY>=0 && Bg[5].pixels[(int(nextX)+int(shiftX))+int(nextY)*3500] == color(255, 0, 0)) {
        life=life-2;
        image(Bg[6], -shiftX, 0);
      }
      if (nextY>=0 && Bg[5].pixels[(int(nextX)+int(shiftX))+int(nextY)*3500] == color(0, 255, 0)) {
        life=life+2;
        if(life>100){
          life=100;
        }
      }
      if (keyPressed && keyCode==DOWN && roleY<650 && Bg[1].pixels[(int(nextX)+int(shiftX))+int(nextY)*3500]==color(255)) {
        roleY+=100;
      }

      image(imgRole[roleID], roleX, roleY-145, 150, 150);
      if (frameCount%20==0) {
        roleID=(roleID+1)%4;
        if (roleID==0) roleX+=1;
      }
    }
    image(Fire1[roleID], 800-shiftX+2000, 360, 150, 150);
    if (frameCount%60==0) {
      roleID=(roleID+1)%4;
    }
    image(Trap[roleID], 800-shiftX+650, 570, 150, 150);
    if (frameCount%60==0) {
      roleID=(roleID+1)%4;
    }
    image(Trap1[roleID], 800-shiftX+330, 140, 150, 150);
    if (frameCount%60==0) {
      roleID=(roleID+1)%4;
    }
    image(Fire[roleID], 800-shiftX+40, 360, 150, 150);
    if (frameCount%60==0) {
      roleID=(roleID+1)%4;
    }
    image(Add[1], 800-shiftX+400, 600, 80, 80);
    image(Add1[1], 800-shiftX+1000, 350, 100, 100);
    image(Add[1], 800-shiftX+1800, 400, 80, 80);
    image(Add1[1], 800-shiftX+170, 120, 100, 100);
    image(Heart[1], 640, 30, 50, 50);

    int[]N=new int[3];
    N[2]=life%10;
    N[1]=life/10%10;
    N[0]=life/100%10;

    for (int a=0; a<3; a++)
    {
      int now=N[a];
      image(num[now], 700+a*30, 50, 25, 25);
      if (life==0) stage=3;
    }
  }
  if (stage==2) {
    player1.pause();
    background(Bg[4]);
    image(Button[2], x5, y5, wid3, hei3);
    if (touched3==true) {
      wid3=120;
      hei3=110;
      if (mousePressed) {
        stage=0;
        roleX=50;
        roleY=700;
        roleVX=4;
        roleVY=0;
        shiftX=0;
        life=100;
      }
    }
    if (touched3==false) {
      wid3=110;
      hei3=100;
    }
  }
  if (stage==3) {
    player1.pause();
    background(Bg[7]);
    image(Button[2], x5, y5, wid3, hei3);
    if (touched3==true) {
      wid3=120;
      hei3=110;
      if (mousePressed) {
        stage=0;
        roleX=50;
        roleY=700;
        roleVX=4;
        roleVY=0;
        shiftX=0;
        life=100;
      }
    }
    if (touched3==false) {
      wid3=110;
      hei3=100;
    }
  }
  if(stage==4){
    background(Bg[8]);
    image(Button[2], x6, y6, wid4, hei4);
    if (touched4==true){
      wid4=120;
      hei4=110;
      if(mousePressed) stage=0;
    }
    if(touched4==false){
      wid4=110;
      hei4=100;
    }
  }
  if(stage==5){
    background(Bg[9]);
    image(Button[2], x7, y7, wid5, hei5);
    if (touched5==true){
      wid5=120;
      hei5=110;
      if(mousePressed) stage=0;
    }
    if(touched4==false){
      wid5=110;
      hei5=100;
    }
  }
}
void keyPressed() {
  if (keyCode==UP && flying==false) {
    jumping=1;
    roleVY=-20;
    flying=true;
    if (roleY<0) roleY=0;
  }

}

沒有留言:

張貼留言

alanhc 互動技術-week17 [final]

回顧這學期的作品:  期中作業:LANDING:PLANET 賣點&特點: 炫麗的特效 物理(星球重力及降落)及粒子系統(噴射) 世界地圖可根據視角縮放 困難點: 重寫3次最終改寫成物件導向的CLASS寫法...