Guest User

Processing 3 - Menu en 10 minutes

a guest
Apr 21st, 2016
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.60 KB | None | 0 0
  1. int background[][] = new int[13][25];
  2.  
  3. int Grid[][] = new int[10][20];
  4. Tetromino current = new Tetromino();
  5. int currentPos[] = new int[2];
  6. int timeBuff = 0;
  7. int score = 0;
  8.  
  9. int best_score = 0;
  10.  
  11. boolean game = false;
  12.  
  13. void setup() {
  14.   size(200, 375);
  15.   String sc[] = loadStrings("score.txt");
  16.   best_score = Integer.parseInt(sc[0]);
  17.   textAlign(CENTER, CENTER);
  18.   current = new Tetromino();
  19.   currentPos[0] = 4;
  20.   currentPos[1] = 0;
  21.   for (int x = 0; x < 13; x++) {
  22.     for (int y = 0; y < 25; y++) {
  23.       if (random(100) < 40) background[x][y] = int(random(1, 8));
  24.     }
  25.   }
  26. }
  27.  
  28. void keyPressed() {
  29.   if (game) {
  30.     if (keyCode == 37) {
  31.       boolean stop = false;
  32.       for (int i = 0; i < 4; i++) {
  33.         if ((current.getShapeX(i)+currentPos[0]) > 0) {
  34.           if ((current.getShapeX(i)+currentPos[0]) < 9 && (current.getShapeY(i)+currentPos[1]) < 19 && (current.getShapeY(i)+currentPos[1]) > 0) {
  35.             if (Grid[current.getShapeX(i)+currentPos[0]-1][current.getShapeY(i)+currentPos[1]] != 0) stop = true;
  36.           }
  37.         } else stop = true;
  38.       }
  39.       if (!stop) currentPos[0]--;
  40.     }
  41.     if (keyCode == 38) {
  42.       current.rot();
  43.       if (current.getMinX()+currentPos[0] < 0) currentPos[0]-=current.getMinX()+currentPos[0];
  44.       if (current.getMaxX()+currentPos[0] > 9) currentPos[0]-=current.getMaxX()+currentPos[0]-9;
  45.       if (current.getMinY()+currentPos[1] < 0) currentPos[1]-=current.getMinY()+currentPos[1];
  46.       if (current.getMaxY()+currentPos[1] > 19) currentPos[1]-=current.getMaxY()+currentPos[1]-19;
  47.     }
  48.     if (keyCode == 39) {
  49.       boolean stop = false;
  50.       for (int i = 0; i < 4; i++) {
  51.         if ((current.getShapeX(i)+currentPos[0]) < 9) {
  52.           if ((current.getShapeX(i)+currentPos[0]) > 0 && (current.getShapeY(i)+currentPos[1]) < 19 && (current.getShapeY(i)+currentPos[1]) > 0) {
  53.             if (Grid[current.getShapeX(i)+currentPos[0]+1][current.getShapeY(i)+currentPos[1]] != 0) stop = true;
  54.           }
  55.         } else stop = true;
  56.       }
  57.       if (!stop) currentPos[0]++;
  58.     }
  59.     if (keyCode == 40) {
  60.       score++;
  61.       currentPos[1]++;
  62.       verifyGrid();
  63.     }
  64.   }
  65. }
  66.  
  67. void draw() {
  68.   textFont(createFont("Courier", 20));
  69.   background(0);
  70.   noStroke();
  71.   if (game) {
  72.     for (int x = 0; x < 10; x++) {
  73.       for (int y = 0; y < 20; y++) {
  74.         if (Grid[x][y] == 0) fill(100);
  75.         else if (Grid[x][y] == 1) fill(50, 150, 255);
  76.         else if (Grid[x][y] == 2) fill(0, 0, 255);
  77.         else if (Grid[x][y] == 3) fill(255, 150, 0);
  78.         else if (Grid[x][y] == 4) fill(255, 255, 0);
  79.         else if (Grid[x][y] == 5) fill(100, 255, 0);
  80.         else if (Grid[x][y] == 6) fill(150, 0, 150);
  81.         else fill(255, 0, 0);
  82.         rect(x*15+25, y*15+25, 13, 13);
  83.       }
  84.     }
  85.  
  86.     displayCurrent();
  87.  
  88.     timeBuff++;
  89.     if (timeBuff > 20) {
  90.       timeBuff = 0;
  91.       currentPos[1]++;
  92.     }
  93.  
  94.     verifyGrid();
  95.  
  96.     text("Score : "+score, 100, 345);
  97.   } else {
  98.     for (int x = 0; x < 13; x++) {
  99.       for (int y = 0; y < 25; y++) {
  100.         if (background[x][y] == 0) fill(100);
  101.         else if (background[x][y] == 1) fill(50, 150, 255);
  102.         else if (background[x][y] == 2) fill(0, 0, 255);
  103.         else if (background[x][y] == 3) fill(255, 150, 0);
  104.         else if (background[x][y] == 4) fill(255, 255, 0);
  105.         else if (background[x][y] == 5) fill(100, 255, 0);
  106.         else if (background[x][y] == 6) fill(150, 0, 150);
  107.         else fill(255, 0, 0);
  108.         rect(x*15+3.5, y*15, 13, 13);
  109.       }
  110.     }
  111.     fill(0, 200);
  112.     rect(0, 0, width, height);
  113.     textFont(createFont("Courier", 40));
  114.     fill(255);
  115.     text("TETRIS", width/2, 40);
  116.     textFont(createFont("Courier", 30));
  117.     text("Jouer", width/2, 140);
  118.     text("Quitter", width/2, 210);
  119.     textFont(createFont("Courier", 20));
  120.     text("Meilleur : "+best_score, width/2, 300);
  121.   }
  122. }
  123.  
  124. void mousePressed() {
  125.   if (!game) {
  126.     if (mouseX > 20 && mouseX < width-20 && mouseY > 110 && mouseY < 170) {
  127.       Grid = new int[10][20];
  128.       current = new Tetromino();
  129.       timeBuff = 0;
  130.       score = 0;
  131.       currentPos[0] = 4;
  132.       currentPos[1] = 0;
  133.       for (int x = 0; x < 13; x++) {
  134.         for (int y = 0; y < 25; y++) {
  135.           if (random(100) < 40) background[x][y] = int(random(1, 8));
  136.         }
  137.       }
  138.       game = true;
  139.     }
  140.     if (mouseX > 20 && mouseX < width-20 && mouseY > 180 && mouseY < 240) exit();
  141.   }
  142. }
  143.  
  144. public void verifyGrid() {
  145.   boolean stop = false;
  146.  
  147.   for (int i = 0; i < 4; i++) {
  148.     if (current.getShapeY(i)+currentPos[1] < 19) {
  149.       if (Grid[current.getShapeX(i)+currentPos[0]][current.getShapeY(i)+currentPos[1]+1] != 0) {
  150.         stop = true;
  151.       }
  152.     }
  153.   }
  154.  
  155.   if (current.getMaxY()+currentPos[1] == 19 || stop) {
  156.     for (int i = 0; i < 4; i++) {
  157.       if ((current.getShapeY(i)+currentPos[1]) < 0) {
  158.         lost();
  159.       } else Grid[(current.getShapeX(i)+currentPos[0])][(current.getShapeY(i)+currentPos[1])] = current.getColor();
  160.     }
  161.     int count = 0;
  162.     for (int y = 0; y < 20; y++) {
  163.       boolean destroy = true;
  164.       for (int x = 0; x < 10; x++) {
  165.         if (Grid[x][y] == 0) destroy = false;
  166.       }
  167.       if (destroy) {
  168.         count++;
  169.         for (int y2 = y-1; y2 > -1; y2--) {
  170.           for (int x = 0; x < 10; x++) {
  171.             Grid[x][y2+1] = Grid[x][y2];
  172.           }
  173.         }
  174.       }
  175.     }
  176.     if (count > 0) {
  177.       if (count == 1) score+=40;
  178.       else if (count == 2) score+=100;
  179.       else if (count == 3) score+=300;
  180.       else score+=1200;
  181.     }
  182.  
  183.     current = new Tetromino();
  184.     currentPos[0] = 4;
  185.     currentPos[1] = 0;
  186.  
  187.     displayCurrent();
  188.  
  189.     for (int i = 0; i < 4; i++) {
  190.       if (current.getShapeY(i)+currentPos[1] < 19) {
  191.         if (Grid[current.getShapeX(i)+currentPos[0]][current.getShapeY(i)+currentPos[1]+1] != 0) {
  192.           lost();
  193.         }
  194.       }
  195.     }
  196.   }
  197. }
  198.  
  199. void lost() {
  200.   String sc[] = loadStrings("score.txt");
  201.   best_score = Integer.parseInt(sc[0]);
  202.   if (score > best_score) {
  203.     sc[0] = Integer.toString(score);
  204.     saveStrings("score.txt", sc);
  205.     best_score = score;
  206.   }
  207.   game = false;
  208. }
  209.  
  210. void displayCurrent() {
  211.   if (current.getColor() == 0) fill(100);
  212.   else if (current.getColor() == 1) fill(50, 150, 255);
  213.   else if (current.getColor() == 2) fill(0, 0, 255);
  214.   else if (current.getColor() == 3) fill(255, 150, 0);
  215.   else if (current.getColor() == 4) fill(255, 255, 0);
  216.   else if (current.getColor() == 5) fill(100, 255, 0);
  217.   else if (current.getColor() == 6) fill(150, 0, 150);
  218.   else fill(255, 0, 0);
  219.  
  220.   for (int i = 0; i < 4; i++) {
  221.     if ((current.getShapeY(i)+currentPos[1]) >= 0) {
  222.       rect((current.getShapeX(i)+currentPos[0])*15+25, (current.getShapeY(i)+currentPos[1])*15+25, 13, 13);
  223.     }
  224.   }
  225. }
  226.  
  227. class Tetromino {
  228.   int shape[][] = new int[4][2];
  229.   int col = 0;
  230.   public Tetromino() {
  231.     col = int(random(1, 8));
  232.     if (col == 1) {
  233.       shape[1][0] = 1;
  234.       shape[2][0] = -1;
  235.       shape[3][0] = -2;
  236.     } else if (col == 2) {
  237.       shape[1][0] = 1;
  238.       shape[2][0] = -1;
  239.       shape[3][0] = -1;
  240.       shape[3][1] = -1;
  241.     } else if (col == 3) {
  242.       shape[1][0] = 1;
  243.       shape[2][0] = -1;
  244.       shape[3][0] = 1;
  245.       shape[3][1] = -1;
  246.     } else if (col == 4) {
  247.       shape[1][0] = 1;
  248.       shape[2][1] = 1;
  249.       shape[3][0] = 1;
  250.       shape[3][1] = 1;
  251.     } else if (col == 5) {
  252.       shape[1][0] = -1;
  253.       shape[2][1] = -1;
  254.       shape[3][0] = 1;
  255.       shape[3][1] = -1;
  256.     } else if (col == 6) {
  257.       shape[1][0] = 1;
  258.       shape[2][0] = -1;
  259.       shape[3][1] = -1;
  260.     } else {
  261.       shape[1][0] = 1;
  262.       shape[2][1] = -1;
  263.       shape[3][0] = -1;
  264.       shape[3][1] = -1;
  265.     }
  266.   }
  267.   public int getShapeX(int i) {
  268.     return shape[i][0];
  269.   }
  270.   public int getShapeY(int i) {
  271.     return shape[i][1];
  272.   }
  273.   public int getMaxX() {
  274.     int i = 0;
  275.     for (int j = 0; j < 4; j++) {
  276.       if (shape[j][0] > shape[i][0]) i = j;
  277.     }
  278.     return shape[i][0];
  279.   }
  280.   public int getMaxY() {
  281.     int i = 0;
  282.     for (int j = 0; j < 4; j++) {
  283.       if (shape[j][1] > shape[i][1]) i = j;
  284.     }
  285.     return shape[i][1];
  286.   }
  287.   public int getMinX() {
  288.     int i = 0;
  289.     for (int j = 0; j < 4; j++) {
  290.       if (shape[j][0] < shape[i][0]) i = j;
  291.     }
  292.     return shape[i][0];
  293.   }
  294.   public int getMinY() {
  295.     int i = 0;
  296.     for (int j = 0; j < 4; j++) {
  297.       if (shape[j][1] < shape[i][1]) i = j;
  298.     }
  299.     return shape[i][1];
  300.   }
  301.   public int getColor() {
  302.     return col;
  303.   }
  304.   public void rot() {
  305.     for (int i = 0; i < 4; i++) {
  306.       int buf = shape[i][0];
  307.       shape[i][0] = shape[i][1];
  308.       shape[i][1] = -buf;
  309.     }
  310.   }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment