2014年10月27日 星期一

期中作品

組員: 01160741 王奕勝
          01160723 邱詠縉

遊戲名:跳跳火柴人

玩法:
這是一款訓練反應的小遊戲
按下任意鍵可以開始遊戲
按滑鼠左鍵或是空白鍵可以跳躍
越過障礙物可得一分
碰到障礙物則結束遊戲

https://www.youtube.com/watch?v=cu6KQay9WsQ&feature=youtu.be

people p = new people();
wall[] w = new wall[3];
boolean end=false;
boolean intro=true;
int score=0;


void setup()
{
  size(550, 750);
  for (int i = 0; i<3; i++)
  {
    w[i]=new wall(i);
  }

  strokeWeight(5);
}

void draw()
{
  background(255);
  if (end)
  {
    p.move();
  }
  p.people();
  if (end)
  {
    p.drag();
  }
  p.checkCollisions();
  for (int i = 0; i<3; i++)
  {
    w[i].drawwall();
    w[i].checkPosition();
  }
  fill(255);
  stroke(0);

  textSize(32);
  if (end)
  {
    rect(20, 20, 100, 50);
    fill(0);
    text(score, 30, 58);
  } else
  {
    rect(150, 100, 230, 50);
    rect(150, 200, 200, 50);
    fill(0);
    if (intro)
    {
      text("Press space or", 155, 140);
      text("Click to Play", 155, 240);
    } else
    {
      text("Game over", 170, 140);
      text("Score", 180, 240);
      text(score, 280, 240);
    }
  }
}

class people
{
  float xPos, yPos, ySpeed;
  people()
  {
    xPos = 250;
    yPos = 400;
  }

  void people()//people
  {
    fill(0, 0, 128);
    ellipse(xPos+5, yPos+40, 30, 30);
    rect(xPos, yPos+55, 10, 25); //body

    rect(xPos+5, yPos+60, 20, 5); //right hand
    rect(xPos+20, yPos+50, 5, 10); //right hand

    rect(xPos+5, yPos+75, 20, 5); //right feet
    rect(xPos+20, yPos+80, 5, 12); //right feet

    rect(xPos-15, yPos+60, 20, 5); //left hand
    rect(xPos-15, yPos+65, 5, 7); //left hand

    rect(xPos-15, yPos+75, 20, 5); //left feet
    rect(xPos-15, yPos+80, 5, 12); //left feet
  }

  void jump()
  {
    ySpeed=-10;
  }

  void drag()
  {
    ySpeed+=0.4;
  }

  void move()
  {
    yPos+=ySpeed;
    for (int i = 0; i<3; i++)
    {
      w[i].xPos-=3;
    }
  }

  void checkCollisions()
  {
    if (yPos>750)
    {
      end=false;
    }
    for (int i = 0; i<3; i++)
    {
      if ((xPos<w[i].xPos+15&&xPos>w[i].xPos-15)&&(yPos<w[i].wallY-125||yPos>w[i].wallY+20))
      {
        end=false;
      }
    }
  }
}

class wall
{
  float xPos, wallY,xObj;
  boolean cashed = false;
  wall(int i)
  {
    xPos = 100+(i*200);  
    wallY = random(500)+100;
  }

  void drawwall()
  {
 
    stroke(255,0,0);
 
    line( xPos, 0, xPos, wallY-100);
    line( xPos, wallY+100, xPos, 750);
 
  }

  void checkPosition()
  {
    if (xPos<0)
    {
      xPos+=(200*3);
      wallY = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false)
    {
      cashed=true;
      score++;
    }
  }
}

void reset()
{
  end=true;
  score=0;
  p.yPos=400;
  for (int i = 0; i<3; i++)
  {
    w[i].xPos+=550;
    w[i].cashed = false;
  }
}

void mousePressed()
{
  p.jump();
  intro=false;
  if (end==false)
  {
    reset();
  }
}

void keyPressed()
{
  p.jump();
  intro=false;
  if (end==false)
  {
    reset();
  }
}

沒有留言:

張貼留言