2023年1月8日 星期日

7ru week18

 float [] cardX = new float[48];//float cardX[10];

float [] cardY = new float[48];

int selectI=-1;//-1沒有選

import processing.sound.*;

void setup() {

  SoundFile file =new SoundFile(this, "music.mp3");

  file.loop();

  size(600, 600);

  for (int i=0; i<48; i++) {

    cardX[i] = random(600-100);

    cardY[i] = random(300, 600-120);

  }

  PFont font = createFont("微軟正黑體", 40);

  textFont(font);

  show1=shows[0];

  show2=shows[1];

  show3=shows[2];

  show4=shows[3];

}


String[] shows={"黑桃A", "紅心A", "方塊A", "梅花A", };

String show1, show2, show3, show4;

String face;

String[] faces={

  "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K",

  "紅心2", "紅心3", "紅心4", "紅心5", "紅心6", "紅心7", "紅心8", "紅心9", "紅心10", "紅心J", "紅心Q", "紅心K",

  "方塊2", "方塊3", "方塊4", "方塊5", "方塊6", "方塊7", "方塊8", "方塊9", "方塊10", "方塊J", "方塊Q", "方塊K",

  "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K",

};

void draw() {

  background(#029823);

  A(70, 20, show1);

  A(170, 20, show2);

  A(270, 20, show3);

  A(370, 20, show4);


  for (int i=0; i<48; i++) {

    if (selectI == i) fill(255, 128, 128);

    else fill(255);

    drawCard( cardX[i], cardY[i], faces[i]);

  }

  for (int a=0; a<a; a++) {

    for (int top=0; top<4; top++) {

      for (int y=20; y<=dy[top]; y+=20) {

        drawCard( dx[top], y, faces[a]);

      }

    }

  }

}

void A(float x, float y, String show) {

  int W=20;

  fill(255);

  rect(x-W/2, y-W/2, 80, 120, 10);

  if (show.indexOf("黑桃")==-1 && show.indexOf("梅花")==-1)fill(#FF0000);

  else fill(0);

  textSize(20);

  text(show, x+5, y+20);

}


void drawCard(float x, float y, String face) {

  fill(255);

  rect(x, y, 80, 120, 10);

  if (face.indexOf("黑桃")==-1 && face.indexOf("梅花")==-1)fill(#FF0000);

  else fill(0);

  textSize(20);

  text(face, x+5, y+20);

}

void mousePressed() {

  for (int i=0; i<48; i++) {

    if (cardX[i]<mouseX && mouseX<cardX[i]+100 && cardY[i]<mouseY && mouseY<cardY[i]+120) {

      selectI = i;

    }//mouseX, mouseY 決定 i是誰

  }

}

void mouseDragged() {

  int i = selectI;

  if (i==-1) return;

  cardX[i] += (mouseX-pmouseX);

  cardY[i] += (mouseY-pmouseY);

}

void mouseReleased() {

  int i = selectI;

  if (i==-1) return;

  decide_top_or_nothing();

  selectI = -1;

}

void decide_top_or_nothing() {

  int i = selectI;

  if (cardY[i]>300) return; //nothing


  int top = 0;

  if (cardX[i]<100) top = 0;

  else if (cardX[i]<200) top = 1;

  else if (cardX[i]<300) top = 2;

  else top = 3;


  dy[top]+=20;

  cardY[i] = dy[top];

  cardX[i] = dx[top];

}

float [] dy = {0, 0, 0, 0};

float [] dx = {60, 160, 260, 360};

沒有留言:

張貼留言