2017年1月12日 星期四

Week 19 期末作品DEMO

作品DEMO

控制搖桿來讓噴火龍移動搭配攻擊按鍵來打擊敵人,當被砲彈與敵人攻擊會扣除血量,歸零則死亡,努力閃避怪獸並打敗他們來挑戰最終BOSS吧



實體圖:

設備主要是我們的搖桿 利用將下方固定住然後加上彈簧,藉此能夠上下左右的移動,來達到移動的功能,還有一個為確認鍵(控制發射砲彈&開始結束遊戲)



                                                              程式碼請按按鈕解鎖
Processing:

import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val;// Data received from the serial port

int num=15;
int m1=30;
int BossHP=20;
int score =0;
int hp=20;
float time=0;
float hurttime=0.0;

int i, j, k, m=0;

PImage charizard;
PImage charizard1;
PImage charizard2;
PImage cannon[]=new PImage [3];
PImage img;
PImage monster[]=new PImage [num];
PImage fire []=new PImage [m1];
PImage bomb[]=new PImage [3];
PImage Boss;
PImage Zapdos;
PImage first;
PImage win;
PImage Rayquaza;
PImage winBackground;
PImage sad;


float charizardX=300;
float charizardY=300;

float BossX=1500;
float BossY=350;
float BossSpeed=0.2;

float ZapdosX;
float ZapdosY;
float ZapdosSpeed;
float ZapdosHp;

float monsterX[]=new float [num];
float monsterY[]=new float [num];
float monsterSpeed[]=new float [num];
float fireX[]=new float [m1];
float fireY[]=new float [m1];
float fireSpeed[]=new float [m1];

float bombShoot[]=new float [3];
float bombX[]=new float[3];
float bombY[]=new float[3];

boolean bombTF[]=new boolean [3];
boolean fireTF[]=new boolean [m1];
boolean timerTF=true;
boolean bossTF=false;
boolean beginTF=false;
boolean hurtTF=false;
boolean ZapdosTF=false;

int gamestateTF=0;

void setup()
{
  size(1024, 768);
  myPort = new Serial(this, "COM4", 9600);
  img = loadImage("back.jpg");
  first=loadImage("first.jpg");
  charizard=loadImage("charizard.png");
  charizard1=loadImage("charizardtext.png");
  charizard2=loadImage("CharizardWinner.png");
  Boss=loadImage("boss.png");
  win=loadImage("win.png");
  Rayquaza=loadImage("Rayquaza.png");
  winBackground=loadImage("winbackground.jpg");
  sad=loadImage("sad.jpg");
  Zapdos=loadImage("Zapdos.png");

  frameRate(25);

  for (int i=0; i<num; i++)
  {
    monster[i]=loadImage("monster.png");
    monsterX[i]=random(900, 10000);
    monsterY[i]=random(150, 600);
    monsterSpeed[i]=-8;
  }
  for (int i=0; i<m1; i++)
  {
    fire[i]=loadImage("fire.png");
    fireSpeed[i]=10;
    fireTF[i]=false;
  }

  for (int i=0; i<3; i++)
  {
    cannon[i] = loadImage("cannon.png");
  }
  for (int i=0; i<3; i++)
  {
    bomb[i] = loadImage("bomb.png");
    bombX[i]=240*(i+1);
    bombY[i]=650;
    bombShoot[i]=0;
    bombTF[i]=false;
  }

  ZapdosX=1200;
  ZapdosY=random(150, 600);
  ZapdosSpeed=-0.5;
  ZapdosHp=3;


  imageMode(CENTER);
}
int cd=0;
void draw()
{
  while ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
    if (val==50 && charizardY <= 550)
    {
      charizardY+=1;
    } else if (val==49 && charizardY >= 170)
    {
      charizardY-=1;
    } else if (val==52 && charizardX<=900)
    {      
      charizardX+=1;
    } else if ( val==51&& charizardX >= 50)
    {     
      charizardX-=1;
    } 
    else if (val==53)
    {
      if (gamestateTF==0)
      {
        gamestateTF=1;
      } else if (cd<=0 && gamestateTF==1)
      {
        if (k==m1 ) k=1;
        fireTF[k]=true;

        fireX[k]=charizardX+120;
        fireY[k]=charizardY;

        k++;
        cd=5;
      } else if (gamestateTF==2)
      {

       
        Restart();
      }
    }

    /* else  if(val == 53&& cd<=0 && gamestateTF==1)
     {
     
     if(k==m1 ) k=1;
     fireTF[k]=true;
     
     fireX[k]=charizardX+120;
     fireY[k]=charizardY;
     
     k++;
     cd=5;
     
     }
    else if (gamestateTF==2 && val==53)
    {
      gamestateTF=1;
      Restart();
    }*/
  }
  if (cd>0) cd--;
  if (gamestateTF==0)
  { 
    background(first);

    stroke(0, 0, 0);
    textSize(60);
    fill(0);
    ellipse(520, 375, 165, 165);
    fill(225, 0, 0);
    ellipse(520, 375, 150, 150);

    fill(0);
    text("Start", 450, 395);

    if (mousePressed && mouseX>=420 && mouseX<=600 && mouseY>=280 && mouseY<=450)
    {     
      gamestateTF=1;
    }
  }  
  if (gamestateTF==1)
  {
    background(img);
    line(0, 100, 1024, 100);

    fill(125);
    ellipse(100, 50, 150, 100);
    image(charizard1, 100, 50, 100, 100);

    if (frameCount%18==0)
    {
      time+=1;
      if (time%7==0 && time !=0)
      {
        ZapdosTF=true;
      }
    }


    textSize(60);
    fill(0, 95, 135);
    text("HP:"+hp+"   ", 200, 85);
    text("Score:", 425, 85);
    text(score, 625, 85);
    text("Time:", 750, 85);
    text((int)time, 925, 85);

    if (bossTF==true) {
      image(Boss, BossX, BossY, 450, 450);
      fill(255, 0, 0);
      rect(BossX-180, BossY-220, 24*BossHP, 10, 7);
      rect(ZapdosX-50, ZapdosY-100, 30*ZapdosHp, 10, 7);
    }
    image(charizard, charizardX, charizardY, 280, 280);
    image(Zapdos, ZapdosX, ZapdosY, 200, 200);

    //image(charizard,mouseX,mouseY,200,200);
    for (int t=0; t<num; t++)
    {
      image(monster[t], monsterX[t], monsterY[t], 200, 200);
      monsterX[t]+=monsterSpeed[t];

      for ( m=0; m<m1; m++)
      {
        if (fireX[m]+100>=monsterX[t]&&fireX[m]-50<=monsterX[t]&&fireY[m]+100>=monsterY[t]&&fireY[m]-100<=monsterY[t])
        {
          monsterX[t]=random(1000, 3500);
          fireX[m]=-100;
          fireY[m]=1000;
          score+=1;
        }

        if (fireX[m]>=1000)
        {
          fireTF[m]=false;
          fireX[m]=-100;
          fireY[m]=1000;
        }

        if (fireX[m]+250>=BossX &&fireX[m]-250<=BossX&&fireY[m]+250>=BossY&&fireY[m]-250<=BossY)
        {
          fireX[m]=-100;
          fireY[m]=1000;
          BossHP-=1;
          score+=1;

          if (BossHP==0)
          {
            BossHP=0;
            score+=50;
            bossTF=false;
            BossX=1000;
          }
        }
        if (fireX[m]+100>=ZapdosX &&fireX[m]-100<=ZapdosX && fireY[m]+100>=ZapdosY && fireY[m]-100<=ZapdosY)
        {
          fireX[m]=-100;
          fireY[m]=1000;
          ZapdosHp-=1;
          if (ZapdosHp==0)
          {

            score+=3;
            ZapdosX=1200;
            ZapdosY=random(150, 600);
            ZapdosTF=false;
            ZapdosHp=3;
          }
        }
        if (ZapdosX<=-100)
        {
          ZapdosX=1200;
          ZapdosTF=false;
          ZapdosHp=3;

          if (score>0) score-=1;
          else score = 0;
        }

        if (charizardX+100>=monsterX[t]&&charizardX-100<=monsterX[t]&&charizardY+100>=monsterY[t]&&charizardY-100<=monsterY[t])
        {
          if (hurtTF==false)
          {
            hp-=1;
            hurtTF=true;
          } else 
          {
            hurttime+=0.001;
            if ((int)hurttime==1)
            {
              hurtTF=false;
              hurttime=0.0;
            }
          }
        }   

        if (charizardX+200>=BossX && charizardX-200<=BossX && charizardY+200>=BossY && charizardY-200<= BossY)
        {
          if (hurtTF==false)
          {
            hp-=1;
            hurtTF=true;
          } else 
          {
            hurttime+=0.0001;
            if ((int)hurttime==1)
            {
              hurtTF=false;
              hurttime=0.0;
            }
          }
        }
        if (charizardX+150>=ZapdosX && charizardX-150<=ZapdosX && charizardY+150>=ZapdosY && charizardY-150<= ZapdosY)
        {
          if (hurtTF==false)
          {
            hp-=1;
            hurtTF=true;
          } else 
          {
            hurttime+=0.0001;
            if ((int)hurttime==1)
            {
              hurtTF=false;
              hurttime=0.0;
            }
          }
        }
      }
      if (monsterX[t]<=-200)
      {
        monsterX[t]=random(1000, 7000);
        if (score>0) score-=1;
        else score =0;
      }
      for (int i=0; i<3; i++)
      {
        image(bomb[i], bombX[i], bombY[i], 45, 45);
        bombShoot[i]=(int)random(0, 1000);

        if (bombShoot[i]==1)
        {
          bombTF[i]=true;
        }
        if (bombTF[i]==true)
        {
          bombY[i]-=0.7;
          if (bombX[i]+100>=charizardX && bombX[i]-100<=charizardX && bombY[i]+100>=charizardY && bombY[i]-100<=charizardY)
          {
            hp-=1;
            bombY[i]=650;
            bombTF[i]=false;
          }

          if (bombY[i]<=-100)
          {
            bombY[i]=650;
            bombTF[i]=false;
          }
        }
      }
      for (int i=0; i<3; i++)
      {
        image(cannon[i], 240*(i+1), 700, 80, 110);
      }


      if (ZapdosTF)
      {
        ZapdosX+=ZapdosSpeed;
      }

      if (BossHP>0)
      {
        bossTF=true;
        if (time>=50)
        {
          BossX-=BossSpeed;
        }
      }
    }

    for (int k=0; k<m1; k++)
    {
      if (fireTF[k]==true)
      {
        image(fire[k], fireX[k], fireY[k], 75, 75);
        fireX[k]+=fireSpeed[k];
      }
    }
  }

  if (hp<=0 || BossX <=200)
  {
    Gameover();
  }
  if (BossHP==0)
  {
    WinGame();
  }
}

void keyPressed()
{
  if (keyCode=='A')
  {
    if (k==m1) k=1;
    fireTF[k]=true;

    fireX[k]=charizardX+120;
    fireY[k]=charizardY;

    k++;
  }
}

void Gameover()
{
  gamestateTF=2;
  background(sad);
  textSize(150);
  fill(255, 0, 0);
  text("GameOver", 150, 550);
  if (mousePressed)
  {
    Restart();
  }
}

void WinGame()
{
  gamestateTF=2;

  background(winBackground);
  textSize(90);
  fill(255);
  text("Win the Game!!!", 150, 440);
  fill(255, 0, 0);
  text("Score:"+score, 100, 600);

  image(charizard2, 820, 600, 300, 300);
  image(win, 850, 200, 300, 300);
  image(Rayquaza, 200, 150, 400, 400);

  if (mousePressed)
  {
    Restart();
  }
}
void Restart()
{
  frameRate(25);

  gamestateTF=0;
  score =0;
  m=0;
  hp=20;
  num=15;
  m1=30;
  BossHP=15;
  time=0;
  hurttime=0.0;
  charizardX=300;
  charizardY=300;
  BossX=1500;
  BossY=350;
  BossSpeed=0.2;

  timerTF=true;
  bossTF=false;
  beginTF=false;
  hurtTF=false;
  ZapdosTF=false;

  for (int i=0; i<num; i++)
  {
    //monster[i]=loadImage("monster.png");
    monsterX[i]=random(900, 10000);
    monsterY[i]=random(150, 600);
    monsterSpeed[i]=-8;
  }
  for (int i=0; i<m1; i++)
  {
    //fire[i]=loadImage("fire.png");
    fireSpeed[i]=10;
    fireTF[i]=false;
  }

  for (int i=0; i<3; i++)
  {
    //cannon[i] = loadImage("cannon.png");
  }

  for (int i=0; i<3; i++)
  {
    //bomb[i] = loadImage("bomb.png");
    bombX[i]=240*(i+1);
    bombY[i]=650;
    bombShoot[i]=0;
    bombTF[i]=false;
  }

  ZapdosX=1200;
  ZapdosY=random(150, 600);
  ZapdosSpeed=-0.5;
  ZapdosHp=3;
  imageMode(CENTER);
}



工商時間:




請多多支持


沒有留言:

張貼留言