2023年12月21日 星期四

week15咻消失

 Week15
讓選取的牌可以在最上層
float[] cardX = new float[52];
float[] cardY = new float[52];
int[] cardN = new int[52];
int[] cardC = new int[52];
int N = 0;
int pressedI = -1;
float oldX, oldY;
void setup() {
  size(1000, 800);
  for (int i = 0; i < 52; i++) {// 初始化卡片數字和花色
    cardN[i] = i % 13 + 1;  // 1 到 13 的數字
    cardC[i] = i / 13;      // 0 到 3 的花色
  }
  for (int shuffleCount = 0; shuffleCount < 100; shuffleCount++) {// 洗牌100次
    int a = int(random(52)), b = int(random(52));// 交換 number
    int temp = cardN[a];
    cardN[a] = cardN[b];
    cardN[b] = temp;
    temp = cardC[a];// 交換 color
    cardC[a] = cardC[b];
    cardC[b] = temp;
  }
  int now = 0;
  for (int L = 1; L <= 7; L++) {//排成階梯狀
    for (int k = 0; k < L; k++) {
      cardX[now] = 160 + L * 90;
      cardY[now] = 180 + k * 50;
      now++;
    }
  }
  N = now;
}
void draw() {
  background(#FCF2EA);
  for (int i = 0; i < 4; i++) {
    drawCardContainer(520 + 90 * i, 10);
  }
  for (int i = 0; i < N; i++) {
    if (i == pressedI) continue; //fill(255, 0, 0);
    else fill(255);
    drawCard(i);
  }
  if(pressedI != -1){ //讓牌移動時在最上方
    fill(255,255,0);
    drawCard(pressedI);
  }
  fill(255);
  rect(40, 10, 83, 109, 10);
}
void drawCard(int i) {
  rect(cardX[i], cardY[i], 83, 109, 10);
  int num = cardN[i], c = cardC[i];
  textSize(20);
  textAlign(CENTER);
  if (c == 1 || c == 2) fill(255, 0, 0);
  else fill(0);
  String word = "" + num;
  if (num == 1) word = "A";
  if (num == 11) word = "J";
  if (num == 12) word = "Q";
  if (num == 13) word = "K";
  text(word, cardX[i] + 20, cardY[i] + 34);
}
void drawCardContainer(int x, int y) {
  stroke(0);
  fill(255);
  rect(x, y, 83, 109, 7);
}
void mousePressed() {
  for (int i = 0; i < 52; i++) {
    if (cardX[i]<mouseX && mouseX<cardX[i] + 83 && cardY[i]<mouseY && mouseY<cardY[i] + 109) {
      pressedI = i;
      oldX = cardX[i];
      oldY = cardY[i];
    }
  }
  if (40<mouseX && mouseX<40 + 83 && 10<mouseY && mouseY<10 + 109) {
    cardX[N] = 150;
    cardY[N] = 10 + s;
    N++;
  }
  s+=30;
}
int s = 0;
void mouseDragged() {
  if (pressedI != -1) {
    cardX[pressedI] += mouseX - pmouseX;
    cardY[pressedI] += mouseY - pmouseY;
  }
}
void mouseReleased() {
  if (pressedI != -1) {
    if (isInsideAnyCardContainer(mouseX, mouseY)) { // 檢查卡片是否在盒子的範圍內
      for (int i = 0; i < 4; i++) { // 根據滑鼠放開的位置放置卡片在對應的盒子內
        float boxX = 520 + 90 * i;
        float boxY = 10;
        if (isInsideCardContainer(mouseX, mouseY, boxX, boxY)) {
          cardX[pressedI] = boxX;
          cardY[pressedI] = boxY;
          break;  // 跳出迴圈,只處理一個盒子
        }
      }
    } else {// 如果不在盒子內,還原卡片位置
      cardX[pressedI] = oldX;
      cardY[pressedI] = oldY;
    }
  }
  pressedI = -1;
}

boolean isInsideCardContainer(float x, float y, float boxX, float boxY) {
  // 假設放置卡片的盒子的範圍是在 (boxX, boxY) 到 (boxX + 83, boxY + 109) 之間
  return (x > boxX && x < boxX + 83 && y > boxY && y < boxY + 109);
}
boolean isInsideAnyCardContainer(float x, float y) {
  // 檢查滑鼠放開的位置是否在任一盒子的範圍內
  for (int i = 0; i < 4; i++) {
    float boxX = 520 + 90 * i;
    float boxY = 10;
    if (isInsideCardContainer(x, y, boxX, boxY)) {
      return true;
    }
  }
  return false;
}
只能選取最後一張牌
float[] cardX = new float[52];
float[] cardY = new float[52];
int[] cardN = new int[52];
int[] cardC = new int[52];
int N = 0;
int pressedI = -1;
float oldX, oldY;
void setup() {
  size(1000, 800);
  for (int i = 0; i < 52; i++) {// 初始化卡片數字和花色
    cardN[i] = i % 13 + 1;  // 1 到 13 的數字
    cardC[i] = i / 13;      // 0 到 3 的花色
  }
  for (int shuffleCount = 0; shuffleCount < 100; shuffleCount++) {// 洗牌100次
    int a = int(random(52)), b = int(random(52));// 交換 number
    int temp = cardN[a];
    cardN[a] = cardN[b];
    cardN[b] = temp;
    temp = cardC[a];// 交換 color
    cardC[a] = cardC[b];
    cardC[b] = temp;
  }
  int now = 0;
  for (int L = 1; L <= 7; L++) {//排成階梯狀
    for (int k = 0; k < L; k++) {
      cardX[now] = 160 + L * 90;
      cardY[now] = 180 + k * 50;
      now++;
    }
  }
  N = now;
}
int []topCard ={0,2,5,9,14,20,27}; //只能點最下面卡片
void draw() {
  background(#FCF2EA);
  for (int i = 0; i < 4; i++) {
    drawCardContainer(520 + 90 * i, 10);
  }
  for (int i = 0; i < N; i++) {
    if (i == pressedI) continue; //fill(255, 0, 0);
    else fill(255);
    drawCard(i);
  }
  if(pressedI != -1){ //讓牌移動時在最上方
    fill(255,255,0);
    drawCard(pressedI);
  }
  fill(255);
  rect(40, 10, 83, 109, 10);
}
void drawCard(int i) {
  rect(cardX[i], cardY[i], 83, 109, 10);
  int num = cardN[i], c = cardC[i];
  textSize(20);
  textAlign(CENTER);
  if (c == 1 || c == 2) fill(255, 0, 0);
  else fill(0);
  String word = "" + num;
  if (num == 1) word = "A";
  if (num == 11) word = "J";
  if (num == 12) word = "Q";
  if (num == 13) word = "K";
  text(word, cardX[i] + 20, cardY[i] + 34);
}
void drawCardContainer(int x, int y) {
  stroke(0);
  fill(255);
  rect(x, y, 83, 109, 7);
}
void mousePressed() {
  for (int i = 0; i < 52; i++) {
    if (cardX[i]<mouseX && mouseX<cardX[i] + 83 && cardY[i]<mouseY && mouseY<cardY[i] + 109) {
      for(int t=0; t<topCard.length;t++){//只能點最下面卡片
        if(i==topCard[t]){
          pressedI = i;
          oldX = cardX[i];
          oldY = cardY[i];
        }
      }
    }
  }
  if (40<mouseX && mouseX<40 + 83 && 10<mouseY && mouseY<10 + 109) {
    cardX[N] = 150;
    cardY[N] = 10 + s;
    N++;
  }
  s+=30;
}
int s = 0;
void mouseDragged() {
  if (pressedI != -1) {
    cardX[pressedI] += mouseX - pmouseX;
    cardY[pressedI] += mouseY - pmouseY;
  }
}
void mouseReleased() {
  if (pressedI != -1) {
    if (isInsideAnyCardContainer(mouseX, mouseY)) { // 檢查卡片是否在盒子的範圍內
      for (int i = 0; i < 4; i++) { // 根據滑鼠放開的位置放置卡片在對應的盒子內
        float boxX = 520 + 90 * i;
        float boxY = 10;
        if (isInsideCardContainer(mouseX, mouseY, boxX, boxY)) {
          cardX[pressedI] = boxX;
          cardY[pressedI] = boxY;
          break;  // 跳出迴圈,只處理一個盒子
        }
      }
    } else {// 如果不在盒子內,還原卡片位置
      cardX[pressedI] = oldX;
      cardY[pressedI] = oldY;
    }
  }
  pressedI = -1;
}

boolean isInsideCardContainer(float x, float y, float boxX, float boxY) {
  // 假設放置卡片的盒子的範圍是在 (boxX, boxY) 到 (boxX + 83, boxY + 109) 之間
  return (x > boxX && x < boxX + 83 && y > boxY && y < boxY + 109);
}
boolean isInsideAnyCardContainer(float x, float y) {
  // 檢查滑鼠放開的位置是否在任一盒子的範圍內
  for (int i = 0; i < 4; i++) {
    float boxX = 520 + 90 * i;
    float boxY = 10;
    if (isInsideCardContainer(x, y, boxX, boxY)) {
      return true;
    }
  }
  return false;
}

沒有留言:

張貼留言