superluig164

Untitled

Nov 13th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. /* Written by Tommy Sebestyen
  2.  * Purpose: Play Not Really Tetris
  3.  * Not Really Tetris is derived from Tetris, but isn't Tetris.
  4.  * Blocks cascade downward rather than stopping in midair,
  5.  * allowing for nasty combos.  That is, if it wasn't so buggy.
  6.  * Ah well, I can only blame myself.  At least it's commented
  7.  * to oblivion.
  8.  */
  9. // Standard Tetris board is 10w 20h
  10. // ArrayList<Object> name = new ArrayList<Object>();
  11.  
  12. float gameWidth = 10; // Width of the game field block wise
  13. float gameHeight = 20; // Height of the game field block wise
  14. float blockSize = 23; // Size of each block, square
  15. boolean log = false; // if true, logging will be verbose.  Only for debugging.
  16. int updateInterval = 15; // How often the game should update, in frames.
  17. int upTime = 0; // How long the game has been running, in updates
  18. int score = 0; // How many lines the player has cleared
  19. Field game = new Field(gameWidth, gameHeight, blockSize);
  20. String state = "title"; // The state of the game
  21. PImage bg; // Background image
  22.  
  23. void setup() {
  24.   // Log
  25.   println("Initializing Tetris...");
  26.   size(800, 600); // Size of the window
  27.   rectMode(CENTER);
  28.   textAlign(CENTER, CENTER);
  29.   // Log
  30.   println("OK");
  31.   bg = loadImage("bg.jpg"); // Background image
  32.   noStroke();
  33. }
  34.  
  35. void draw() {
  36.   if (state.equals("title")) drawTitleScreen(); // If we're on the title screen, display the title screen
  37.   if (state.equals("game")) drawGame(); // If we are in the game, display the game
  38.   if (state.equals("pause")) drawPauseScreen(); // If we are paused, display the pause screen
  39.   // NOTE: This doesn't work.  I don't know why.  If you start this game,
  40.   // you'd better be committed, because you can't pause.
  41. }
  42.  
  43. void keyPressed() {
  44.   // If on the title screen
  45.   if (state.equals("title")) {
  46.     // Start the game when the ENTER/RETURN key is pressed.
  47.     if (keyCode == ENTER || keyCode==RETURN) state="game";
  48.   }
  49.   // If in the game
  50.   if (state.equals("game")) {
  51.     // Move the shape over when LEFT is pressed
  52.     if (keyCode == LEFT) game.moveShape(-1);
  53.    
  54.     // Move the shape over when RIGHT is pressed
  55.     if (keyCode == RIGHT) game.moveShape(1);
  56.    
  57.     // Pause when P is pressed (doesn't work)
  58.     if (key == 'p' || key == 'P') state="pause";
  59.   }
  60.   // If we are paused (which we never will be)
  61.   if (state.equals("pause")) {
  62.     // Resume the game when P is pressed
  63.     if (key == 'p' || key == 'P') state="game";
  64.   }
  65. }
  66.  
  67. void mousePressed() {
  68.   // If on the title screen
  69.   if (state.equals("title")) {
  70.     // Start the game when the mouse is clicked within this box
  71.     if (mouseX > (width/2)-100 &&
  72.       mouseX < (width/2)+100 &&
  73.       mouseY > (height/2)-50 &&
  74.       mouseY < (height/2)+50)
  75.       state="game";
  76.   }
  77. }
  78.  
  79. // Draws the background. (I am not 100% sure why I needed a method for this)
  80. void drawBG(color BG) {
  81.   background(BG);
  82. }
  83.  
  84. // Draw the title screen.
  85. void drawTitleScreen() {
  86.   // Display the background image
  87.   imageMode(CENTER);
  88.   tint(255, 255, 255, 50);
  89.   image(bg, width/2, height/2);
  90.  
  91.   // Display the title
  92.   textAlign(CENTER, TOP);
  93.   textSize(30);
  94.   fill(255, 255, 255);
  95.   text("Not Really Tetris", width/2, height/6);
  96.  
  97.   // Draw the button
  98.   rectMode(CENTER);
  99.   fill(200, 200, 200);
  100.   rect(width/2, height/2, 200, 100, 5);
  101.   rectMode(CORNER);
  102.  
  103.   // Draw the text in the button
  104.   textSize(18);
  105.   fill(0, 0, 0);
  106.   text("Start [Enter]", width/2, (height/2)-15);
  107.  
  108.   // Draw the controls
  109.   fill(255, 255, 255);
  110.   text("Use the arrow keys to move left and right", width/2, height-100);
  111. }
  112.  
  113. // Draw the pause screen.  (this doesn't work)
  114. void drawPauseScreen() {
  115.   // Draw a gray rectangle
  116.   fill(128, 128, 128, 128);
  117.   rect(0, 0, width, height);
  118.  
  119.   // Write PAUSE in the middle
  120.   fill(255, 255, 255);
  121.   textSize(18);
  122.   text("PAUSE", width/2, height/2);
  123. }
  124.  
  125. // Draw/update the game.  This is the meaty part.
  126. void drawGame() {
  127.   // Make the game update faster if DOWN is held.  
  128.   if (keyCode == DOWN && keyPressed) updateInterval=5;
  129.   else updateInterval=15;
  130.  
  131.   // Draw the background image.
  132.   drawBG(color(100, 100, 100));
  133.   imageMode(CENTER);
  134.   tint(255, 255, 255, 50);
  135.   image(bg, width/2, height/2);
  136.  
  137.   // If we are on a multiple of the update interval
  138.   if (frameCount % updateInterval == 0) {
  139.     game.update(); // Update the game
  140.     upTime++; // Log the uptime
  141.     score=game.linesCleared; // Set the score to be displayed
  142.   }
  143.   game.display(); // Draw the game
  144.  
  145.   // Draw the score counter in the top left
  146.   fill(255, 255, 255);
  147.   textSize(18);
  148.   textAlign(LEFT, TOP);
  149.   text("SCORE: "+score, 10, 10);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment