2024年1月7日 星期日

XX - Week14

 

=========================================================================

FOURTEENTH

沒什麼進度,只有加了遊戲開始畫面的Help按鈕

boolean gameStarted = false;//遊戲開始
boolean gameOver = false;//結束
boolean showHelp = false;//說明

int score = 0; // 分數
int startTime; // 遊戲開始時間
int CurrentPos[] = new int[2];//當前的形狀

void setup(){
  //Tetris = loadImage("Tetris.jpg");
  size(600, 750);
  background(0);
  Current = new Shapes();
  CurrentPos[0] = 4;//左右,方塊的起始位置
  CurrentPos[1] = 0;//下降,方塊的起始位置
}
int down = 0;
void draw() {
  //background(Tetris);
  background(0);
  if (!gameStarted) {//開始畫面
    if (showHelp) {//按下就是true,顯示說明畫面
      drawHelpScreen();
    }
    else {
      drawStartScreen();
    }
  }
  else if (gameOver) {//結束畫面
    drawGameOverScreen();
  }
  else {//遊戲進行中
    Grid();
    displayCurrent();
    down++;
    if (down > 20) {
      down = 0;
      CurrentPos[1]++;
      StopEmpty();
    }
    StopEmpty();
    showScoreAndTimer(); // 顯示分數計時器
  }
}
int StopShapes[][] = new int[10][20];
public void Grid(){
  for(int x = 0; x < 10; x++) {
    for(int y = 0; y < 20; y++) {
      //停下來的Tetris
      if(StopShapes[x][y] == 0) fill(100);
      else if(StopShapes[x][y] == 1) fill(#AE5FE0);//紫
      else if(StopShapes[x][y] == 2) fill(#FA980F);//橘
      else if(StopShapes[x][y] == 3) fill(#E7F000);//黃
      else if(StopShapes[x][y] == 4) fill(#4FC104);//綠
      else if(StopShapes[x][y] == 5) fill(#258BF5);//藍
      else if(StopShapes[x][y] == 6) fill(#E30000);//紅
      else fill(#FA65C6);//粉
      rect(x*30+165, y*30+50, 26, 26, 5);
    }
  }
}
void displayCurrent() {
  //正在下落的Tetris
  if(Current.getColor() == 0) fill(100);
  else if (Current.getColor() == 1) fill(#D7A7F7);//淺紫
  else if (Current.getColor() == 2) fill(#F5C56A);//淺橘
  else if (Current.getColor() == 3) fill(#F7FAB1);//淺黃
  else if (Current.getColor() == 4) fill(#95E584);//淺綠
  else if (Current.getColor() == 5) fill(#B8CBFA);//淺藍
  else if (Current.getColor() == 6) fill(#FAB8B8);//淺紅
  else fill(#FFDBF2);//淺粉
 
  for(int i = 0; i < 4; i++) {
    if((Current.getShapeY(i)+CurrentPos[1]) >= 0) {
//目前要下降所以先用getShapeY+CurrentPos[1] 因為CurrentPos[1]是跟著down一起++
      rect((Current.getShapeX(i)+CurrentPos[0])*30+165, (Current.getShapeY(i)+CurrentPos[1])*30+50, 26, 26, 5);
      //    getShapeX,CurrentPos[0]一組                 getShapeY,CurrentPos[1]一組
      //跟著Grid的rect同樣參數
    }
  }
}
Shapes Current = new Shapes();
class Shapes {//形狀
  int [][] shape = new int[4][2];
  int tetris = 0;
  
  public Shapes() {
    tetris = int(random(1, 8));//random 選擇狀態來隨機選擇圖形
    //先選擇圖形
    if(tetris == 1) {//直線
      shape[1][0] =  1;
      shape[2][0] = -1;
      shape[3][0] = -2;
    }
    else if(tetris == 2) {//方塊
      shape[1][0] = 1;
      shape[2][1] = 1;
      shape[3][0] = 1;
      shape[3][1] = 1;
    }
    else if(tetris == 3) {//右L
      shape[1][0] =  1;
      shape[2][0] = -1;
      shape[3][0] =  1;
      shape[3][1] = -1;
    }
    else if(tetris == 4) {//左L
      shape[1][0] =  1;
      shape[2][0] = -1;
      shape[3][0] = -1;
      shape[3][1] = -1;
    }
    else if(tetris == 5) {//左閃電
      shape[1][0] = -1;
      shape[2][1] = -1;
      shape[3][0] =  1;
      shape[3][1] = -1;
    }
    else if(tetris == 6) {//右閃電
      shape[1][0] =  1;
      shape[2][1] = -1;
      shape[3][0] = -1;
      shape[3][1] = -1;
    }
    else{//小T
      shape[1][0] =  1;
      shape[2][0] = -1;
      shape[3][1] = -1;
    }
  }
  //後來做旋轉
  public void Rotate() {
    for(int i = 0; i < 4; i++) {
      if(tetris==2) break;//方塊不做旋轉,會往上跑
      int temp = shape[i][0];
      shape[i][0] = shape[i][1];
      shape[i][1] = -temp;
    }
  }
  public int getShapeX(int i) {
    return shape[i][0];
  }
  public int getShapeY(int i) {
    return shape[i][1];
  }
  public int MaxRight() {
    int i = 0;
    for(int j = 0; j < 4; j++) {
      if(shape[j][0] > shape[i][0]) i = j;
    }
    return shape[i][0];
  }
  public int MinLeft() {
    int i = 0;
    for(int j = 0; j < 4; j++) {
      if(shape[j][0] < shape[i][0]) i = j;
    }
    return shape[i][0];
  }
  public int MaxUp() {
    int i = 0;
    for(int j = 0; j < 4; j++) {
      if(shape[j][1] > shape[i][1]) i = j;
    }
    return shape[i][1];
  }
  public int MinDown() {
    int i = 0;
    for(int j = 0; j < 4; j++) {
      if(shape[j][1] < shape[i][1]) i = j;
    }
    return shape[i][1];
  }
  public int getColor() {
    return tetris;
  }
}

void keyPressed() {//控制 上(旋轉)下左右
  if (keyCode == DOWN) {
    CurrentPos[1]++;//下落++
    StopEmpty();
  }
  if (keyCode == LEFT) {
    boolean stop = false;
    for (int i = 0; i < 4; i++) {
      if ((Current.getShapeX(i)+CurrentPos[0]) >=1) {//判斷到左邊界
      //目前要左橫移所以先用getShapeX+CurrentPos[0] 移動最少就是到1
        if ((Current.getShapeX(i)+CurrentPos[0]) <=8 && (Current.getShapeY(i)+CurrentPos[1]) <=18 && (Current.getShapeY(i)+CurrentPos[1]) >=1) {
          //下降最多到18 最少1
          if (StopShapes[Current.getShapeX(i)+CurrentPos[0]-1][Current.getShapeY(i)+CurrentPos[1]] !=0){
          //  StopShapes[x][y]
              stop = true;
          }
        }
      }
      else stop = true;
    }
    if (!stop) CurrentPos[0]--;//持續判斷是否stop是true 不是的話就可-- 往左邊移動
  }
  if (keyCode == RIGHT) {
    boolean stop = false;
    for (int i = 0; i < 4; i++) {
      if ((Current.getShapeX(i)+CurrentPos[0]) <= 8) {//判斷到右邊界
      //目前要右橫移所以先用getShapeX+CurrentPos[0] 右邊移動最多就是到8
        if ((Current.getShapeX(i)+CurrentPos[0]) >= 1 && (Current.getShapeY(i)+CurrentPos[1]) <=18 && (Current.getShapeY(i)+CurrentPos[1]) >=1) {
          //下降最多到18 最少1
          if (StopShapes[Current.getShapeX(i)+CurrentPos[0]][Current.getShapeY(i)+CurrentPos[1]] != 0){
          //  StopShapes[x][y]
            stop = true;
          }
        }
      }
      else stop = true;
    }
    if (!stop) CurrentPos[0]++;//持續判斷是否stop是true 不是的話就可++ 往右邊移動
  }
  if (keyCode == UP) {//旋轉
    Current.Rotate();//呼叫Shapes裡的Rotate
    //防止旋轉時會超出邊框
    if(Current.MinLeft()+CurrentPos[0] < 0){
      CurrentPos[0]=-Current.MinLeft();//目前想法,超出就回到原來的
      //CurrentPos[0]-=Current.MinLeft()+CurrentPos[0];
    }
    if(Current.MaxRight()+CurrentPos[0] > 9){
      CurrentPos[0]=-Current.MaxRight()+9;//目前想法,超出就回到原來的然後加回去
      //CurrentPos[0]-=Current.MaxRight()+CurrentPos[0]-9;
    }
    if(Current.MinDown()+CurrentPos[1] < 0){
      CurrentPos[1]=-Current.MinDown();
      //CurrentPos[1]-=Current.MinDown()+CurrentPos[1];
    }
    if(Current.MaxUp()+CurrentPos[1] > 19){
      CurrentPos[1]=-Current.MaxUp()+19;
      //CurrentPos[1]-=Current.MaxUp()+CurrentPos[1]-19;
    }
  }
  if (key == ' ') {
    while (!reachedBottom()) {//持續做直到到底
      CurrentPos[1]++; // 持續下落直到到達底部
    }
    StopEmpty(); // 停止後處理
  }
}
boolean reachedBottom() {
  for (int i = 0; i < 4; i++) {
    if (Current.getShapeY(i) + CurrentPos[1] >= 19) {
      return true; // 已經到達底部
    }
    if (StopShapes[Current.getShapeX(i) + CurrentPos[0]][Current.getShapeY(i) + CurrentPos[1] + 1] != 0) {
      return true; // 下方有方塊阻擋
    }
  }
  return false;
}
void showScoreAndTimer() {
  displayScore();
  displayTimer();
}
void displayScore() {//分數
  fill(255);
  textAlign(LEFT, TOP);
  
  fill(255);
  rect(80,55,100,50,5);
  
  fill(0);
  textSize(25);
  text("Score: " + score, 40, 50);//左邊顯示分數
}

void displayTimer() {//計時器
  int elapsedTime = millis() - startTime;
  int seconds = int(elapsedTime / 1000);
  fill(255);
  textAlign(RIGHT, TOP);
  
  fill(255);
  rect(520,55,100,50,5);
  
  fill(0);
  textSize(25);
  text("Time: " + seconds, width-45, 50);//右邊顯示計時器
}
void drawStartScreen() {//開始畫面
  fill(255);
  textAlign(CENTER, CENTER);
  textSize(40);
  text("Tetris Game", width/2, height/2-50);
  
  rectMode(CENTER);//開始遊戲按鈕 Start
  fill(150);
  rect(width/2, height/2, 200, 50, 10);
  fill(255);
  text("Start Game", width/2, height/2);
  
  fill(150);// 說明按鈕 Help
  rect(width/2, height/2 + 70, 200, 50, 10); 
  fill(255);
  textAlign(CENTER, CENTER);
  text("Help", width/2, height/2 + 70);
}
void drawHelpScreen() {//說明畫面
  // 標題
  fill(255);
  textAlign(CENTER, CENTER);
  textSize(40);
  text("Tetris Instructions", width/2, height/2-50);
  
  //說明細項
  textSize(25);
  //textAlign();
  text("Use arrow keys to move the blocks", width/2, height/2);
  text("Press space bar can down your blocks immediately", width/2, height/2+30);
}
void drawGameOverScreen() {//結束畫面
  fill(255);
  textAlign(CENTER, CENTER);
  textSize(40);
  text("Game Over", width/2, height/2-50);
  
  //重新開始按鈕
  rectMode(CENTER);
  fill(150);
  rect(width/2, height/2, 200, 50, 10);
  fill(255);
  text("Restart", width/2, height/2);
}
void mousePressed() {
  if (!gameStarted && mouseX > width/2-100 && mouseX < width/2+100 && mouseY > height/2-25 && mouseY < height/2+25) {
    gameStarted = true;//按下按鈕,開始遊戲
    startTime = millis();//設置遊戲開始時間
  }
  else if (!gameStarted && mouseX > width/2-100 && mouseX < width/2+100 && mouseY > height/2+45 && mouseY < height/2+95) {
    // 操作說明按鈕
    showHelp = true;//true,顯示說明畫面
  }
  else if (gameOver && mouseX > width/2-100 && mouseX < width/2+100 && mouseY > height/2-25 && mouseY < height/2+25) {
    //按下按鈕,重新開始遊戲
    gameOver = false;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 20; j++) {
        StopShapes[i][j] = 0;//重置停止的形狀
      }
    }
  }
}
public void StopEmpty() {
  boolean stop = false;
  for(int i = 0; i < 4; i++) {
    if(Current.getShapeY(i)+CurrentPos[1] <= 18) {
      if(StopShapes[Current.getShapeX(i)+CurrentPos[0]][Current.getShapeY(i)+CurrentPos[1]+1] != 0) {
        //StopShapes[x][y]                                         i=3,ShapeY(i)=-1,       所以至少要+1變回0
        stop = true;
      }
    }
  }
  if (Current.MaxUp()+CurrentPos[1] == 19 || stop) {
      //上限+目前
    for(int i = 0; i < 4; i++) {
      if((Current.getShapeY(i)+CurrentPos[1])<0){
        gameOver = true;
      }//
      else StopShapes[(Current.getShapeX(i)+CurrentPos[0])][(Current.getShapeY(i)+CurrentPos[1])] = Current.getColor();
    }
    for(int y = 0; y < 20; y++) {
      boolean empty = true;
      for(int x = 0; x < 10; x++) {
        if(StopShapes[x][y] == 0){
          empty = false;
        }
      }
      if(empty){
        for(int y2 = y-1; y2 > -1; y2--) {
          for(int x = 0; x < 10; x++) {
            StopShapes[x][y2+1] = StopShapes[x][y2];
          }
        }
      }
    }
    Current = new Shapes();
    CurrentPos[0] = 4;
    CurrentPos[1] = 0;
   
    displayCurrent();
    //state=true;
  }
}

沒有留言:

張貼留言