Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. /*
  2. Controls:
  3. LEFT/RIGHT/DOWN to move
  4. UP - flip
  5. SPACE - hard drop (drop immediately)
  6. */
  7.  
  8. import controlP5.*;
  9. import processing.sound.*;
  10.  
  11. SoundFile file;
  12. String audioName = "1.mp3";
  13. String path;
  14. final int CYAN = color(0,255,255);
  15. final int ORANGE = color(255,165,0);
  16. final int YELLOW = color(255,255,0);
  17. final int PURPLE = color(160,32,240);
  18. final int BLUE = color(0,0,255);
  19. final int RED = color(255,0,0);
  20. final int GREEN = color(0,255,0);
  21.  
  22. ControlP5 controlP5;
  23. Grid board, preview;
  24. Tetromino curr;
  25. Shape next;
  26. Shape[] shapes = new Shape[7];
  27. int timer = 20;
  28. int currTime = 0;
  29. int score = 0;
  30. int lines = 0;
  31. int level = 1;
  32. final int SPEED_DECREASE = 2;
  33. boolean game_over = false;
  34.  
  35. void setup() {
  36. path = sketchPath(audioName);
  37. file = new SoundFile(this, path);
  38. file.loop();
  39. size(500, 690, P2D);
  40. textSize(25);
  41. controlP5 = new ControlP5(this);
  42. controlP5.addButton("play", 1, width/2 - 35, height/2, 70, 20).setLabel("play again");
  43. shapes[0] = new Shape(4, new int[] {8,9,10,11}, CYAN); // I
  44. shapes[1] = new Shape(3, new int[] {0,3,4,5}, BLUE); // J
  45. shapes[2] = new Shape(3, new int[] {2,3,4,5}, ORANGE); // L
  46. shapes[3] = new Shape(2, new int[] {0,1,2,3}, YELLOW); // O
  47. shapes[4] = new Shape(4, new int[] {5,6,8,9}, GREEN); // S
  48. shapes[5] = new Shape(3, new int[] {1,3,4,5,}, PURPLE); // T
  49. shapes[6] = new Shape(4, new int[] {4,5,9,10}, RED); // Z
  50. board = new Grid(20, 20, 321, 642, 20, 10);
  51. preview = new Grid(355, 20, 116, 58, 2, 4);
  52. next = shapes[(int)random(7)];
  53. loadNext();
  54. }
  55.  
  56. void draw() {
  57. background(0);
  58. if (game_over) {
  59. text("GAME OVER\nSCORE: " + score, width/2 - 70, height/2 - 50);
  60. controlP5.show(); // show the play again button
  61. return;
  62. }
  63. else {
  64. controlP5.hide();
  65.  
  66. }
  67. currTime++;
  68. if (currTime >= timer && board.animateCount == -1)
  69. curr.stepDown();
  70. preview.draw();
  71. board.draw();
  72. if (curr != null)
  73. curr.draw();
  74. next.preview();
  75. fill(255);
  76. text("LEVEL\n" + level, width - 150, 120);
  77. text("LINES\n" + lines, width - 150, 200);
  78. text("SCORE\n" + score, width - 150, 280);
  79. }
  80.  
  81. void loadNext() {
  82. curr = new Tetromino(next);
  83. next = shapes[(int)random(7)];
  84. currTime = 0;
  85. }
  86.  
  87. void keyPressed() {
  88. if (curr == null || game_over)
  89. return;
  90. switch(keyCode) {
  91. case LEFT : curr.left(); break;
  92. case RIGHT : curr.right(); break;
  93. case UP : curr.rotate(); break;
  94. case DOWN : curr.down(); break;
  95. case ' ' : curr.hardDown(); break;
  96. }
  97. }
  98.  
  99. void play(int value) {
  100. board.clear();
  101. loadNext();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement