Advertisement
Guest User

Make redboxes appear 1,2,3,4,5,6....

a guest
Mar 24th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.71 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package snake;
  7.  
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.geom.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13.  
  14. public class Snake extends JFrame implements Runnable {
  15.  
  16.     static final int numRows = 25;
  17.     static final int numColumns = 27;
  18.     static final int XBORDER = 20;
  19.     static final int YBORDER = 20;
  20.     static final int YTITLE = 30;
  21.     static final int WINDOW_BORDER = 8;
  22.     static final int WINDOW_WIDTH = 2 * (WINDOW_BORDER + XBORDER) + numColumns * 30;
  23.     static final int WINDOW_HEIGHT = YTITLE + WINDOW_BORDER + 2 * YBORDER + numRows * 30;
  24.  
  25.     boolean animateFirstTime = true;
  26.     int xsize = -1;
  27.     int ysize = -1;
  28.     Image image;
  29.     Graphics2D g;
  30.  
  31.     int timeCount;
  32.     double frameRate = 144;
  33.     double boxSec;
  34.     boolean sSpeed;
  35.  
  36.     final int EMPTY = 0;
  37.     final int SNAKE = 1;
  38.     final int DEATH = 2;
  39.     int board[][];
  40.  
  41.     int XMovDir;
  42.     int YMovDir;
  43.     int XMov;
  44.     int YMov;
  45.     int XMovDir2;
  46.     int YMovDir2;
  47.     int XMov2;
  48.     int YMov2;
  49.  
  50.     //1 Norm, 2 Night,3 Party
  51.     int mode = 1;
  52.     //Party RGB
  53.     int R;
  54.     int G;
  55.     int B;
  56.     boolean canClip = true;
  57.  
  58.     boolean gameOver;
  59.     boolean inGame;
  60.  
  61.     int Score;
  62.     int hScore;
  63.  
  64.     static Snake frame;
  65.  
  66.     public static void main(String[] args) {
  67.         frame = new Snake();
  68.         frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  69.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.         frame.setVisible(true);
  71.     }
  72.  
  73.     public Snake() {
  74.         addMouseListener(new MouseAdapter() {
  75.             public void mousePressed(MouseEvent e) {
  76.                 if (e.BUTTON1 == e.getButton()) {
  77.                     //left button
  78.  
  79. // location of the cursor.
  80.                     int xpos = e.getX();
  81.                     int ypos = e.getY();
  82.  
  83.                 }
  84.                 if (e.BUTTON3 == e.getButton()) {
  85.                     //right button
  86.                     reset();
  87.                 }
  88.                 repaint();
  89.             }
  90.         });
  91.  
  92.         addMouseMotionListener(new MouseMotionAdapter() {
  93.             public void mouseDragged(MouseEvent e) {
  94.                 repaint();
  95.             }
  96.         });
  97.  
  98.         addMouseMotionListener(new MouseMotionAdapter() {
  99.             public void mouseMoved(MouseEvent e) {
  100.  
  101.                 repaint();
  102.             }
  103.         });
  104.  
  105.         addKeyListener(new KeyAdapter() {
  106.  
  107.             public void keyPressed(KeyEvent e) {
  108.                 if (gameOver) {
  109.                     return;
  110.                 }
  111.                 if (e.VK_UP == e.getKeyCode()) {
  112.                     if (YMovDir != 1) {
  113.                         inGame = true;
  114.                         XMovDir = 0;
  115.                         YMovDir = 2;
  116.                     }
  117.  
  118.                 } else if (e.VK_DOWN == e.getKeyCode()) {
  119.                     if (YMovDir != 2) {
  120.                         inGame = true;
  121.                         XMovDir = 0;
  122.                         YMovDir = 1;
  123.                     }
  124.  
  125.                 } else if (e.VK_LEFT == e.getKeyCode()) {
  126.                     if (XMovDir != 1) {
  127.                         inGame = true;
  128.                         XMovDir = 2;
  129.                         YMovDir = 0;
  130.                     }
  131.                 } else if (e.VK_RIGHT == e.getKeyCode()) {
  132.                     if (XMovDir != 2) {
  133.                         inGame = true;
  134.                         XMovDir = 1;
  135.                         YMovDir = 0;
  136.                     }
  137.                 }
  138.                 repaint();
  139.             }
  140.         });
  141.         init();
  142.         start();
  143.     }
  144.     Thread relaxer;
  145. ////////////////////////////////////////////////////////////////////////////
  146.  
  147.     public void init() {
  148.         requestFocus();
  149.     }
  150. ////////////////////////////////////////////////////////////////////////////
  151.  
  152.     public void destroy() {
  153.     }
  154. ////////////////////////////////////////////////////////////////////////////
  155.  
  156.     public void paint(Graphics gOld) {
  157.         if (image == null || xsize != getSize().width || ysize != getSize().height) {
  158.             xsize = getSize().width;
  159.             ysize = getSize().height;
  160.             image = createImage(xsize, ysize);
  161.             g = (Graphics2D) image.getGraphics();
  162.             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  163.                     RenderingHints.VALUE_ANTIALIAS_ON);
  164.         }
  165. //fill background
  166.         g.setColor(Color.white);
  167.         g.fillRect(0, 0, xsize, ysize);
  168.  
  169.         int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
  170.         int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
  171. //fill border
  172.         if (mode == 1) {
  173.             g.setColor(Color.black);
  174.         } else if (mode == 2) {
  175.             partyModeBG();
  176.         } else {
  177.             g.setColor(Color.white);
  178.         }
  179.  
  180.         g.fillPolygon(x, y, 4);
  181. // draw border
  182.         g.setColor(Color.red);
  183.         g.drawPolyline(x, y, 5);
  184.         if (animateFirstTime) {
  185.             gOld.drawImage(image, 0, 0, null);
  186.             return;
  187.         }
  188.  
  189.         if (mode == 2) {
  190.             partyModeLines();
  191.         } else {
  192.             g.setColor(Color.red);
  193.         }
  194. //horizontal lines
  195.         for (int zi = 1; zi < numRows; zi++) {
  196.             g.drawLine(getX(0), getY(0) + zi * getHeight2() / numRows,
  197.                     getX(getWidth2()), getY(0) + zi * getHeight2() / numRows);
  198.         }
  199. //vertical lines
  200.         for (int zi = 1; zi < numColumns; zi++) {
  201.             g.drawLine(getX(0) + zi * getWidth2() / numColumns, getY(0),
  202.                     getX(0) + zi * getWidth2() / numColumns, getY(getHeight2()));
  203.         }
  204.  
  205. //Display the objects of the board
  206.         for (int zrow = 0; zrow < numRows; zrow++) {
  207.             for (int zcolumn = 0; zcolumn < numColumns; zcolumn++) {
  208.                 if (board[zrow][zcolumn] == SNAKE) {
  209.                     if (mode == 2) {
  210.                         partyModeSnake();
  211.                     } else {
  212.                         g.setColor(Color.green);
  213.                     }
  214.  
  215.                     g.fillRect(getX(0) + zcolumn * getWidth2() / numColumns,
  216.                             getY(0) + zrow * getHeight2() / numRows,
  217.                             getWidth2() / numColumns,
  218.                             getHeight2() / numRows);
  219.                 }
  220.                 if (board[zrow][zcolumn] == DEATH) {
  221.                     if (mode == 2) {
  222.                         partyModeSnake();
  223.                     } else {
  224.                         g.setColor(Color.red);
  225.                     }
  226.  
  227.                     g.fillRect(getX(0) + zcolumn * getWidth2() / numColumns,
  228.                             getY(0) + zrow * getHeight2() / numRows,
  229.                             getWidth2() / numColumns,
  230.                             getHeight2() / numRows);
  231.                 }
  232.             }
  233.         }
  234.  
  235.         handleScore();
  236.  
  237.         if (gameOver) {
  238.             g.setColor(Color.yellow);
  239.             g.setFont(new Font("Arial Narrow", Font.PLAIN, 100));
  240.             g.drawString("GAME OVER", getWidth2() / 2 - 200, getHeight2() / 2);
  241.         }
  242.  
  243.         gOld.drawImage(image, 0, 0, null);
  244.     }
  245. ////////////////////////////////////////////////////////////////////////////
  246.  
  247.     void handleScore() {
  248.         g.setColor(Color.black);
  249.         g.setFont(new Font("Arial Narrow", Font.PLAIN, 20));
  250.         g.drawString("Blocks traveled:" + Score, getX(0), getYNormal(-17));
  251.         g.drawString("Session high score:" + hScore, getX(350), getYNormal(-17));
  252.     }
  253.  
  254.     ///////////////////////////////////////////////////////////////////////
  255.     public static String read() {
  256.         byte[] buffer = new byte[10];
  257.         try {
  258.             int numBytes = System.in.read(buffer);
  259.         } catch (IOException e) {
  260.             System.out.print("Error: " + e);
  261.             System.exit(1);
  262.         }
  263.         String str = new String(buffer);
  264.         int ball = 5;
  265.         return (str);
  266.     }
  267. ////////////////////////////////////////////////////////////////////////////
  268.  
  269.     public static void write(String str) {
  270.         System.out.print(str);
  271.     }
  272.  
  273.     /////////////////////////////////////////////////////////
  274.     void handleBorderClip() {
  275.         if (numColumns / 2 + XMov == numColumns) {
  276.             XMov = -(numColumns / 2);
  277.         }
  278.  
  279.         if (XMov == -(numColumns / 2 + 1)) {
  280.             XMov = numColumns / 2;
  281.         }
  282.  
  283.         if (numRows / 2 + YMov == numRows) {
  284.             YMov = -(numRows / 2);
  285.         }
  286.  
  287.         if (YMov == -(numRows / 2 + 1)) {
  288.             YMov = numRows / 2;
  289.         }
  290.     }
  291.  
  292.     ///////////////////////////////////////////////////////////////////////
  293.     void handleBorderEnd() {
  294.         if (numColumns / 2 + XMov == numColumns || XMov == -(numColumns / 2 + 1) || numRows / 2 + YMov == numRows || YMov == -(numRows / 2) - 1) {
  295.             gameOver = true;
  296.             inGame = false;
  297.         }
  298.     }
  299.  
  300.     ////////////////////////////////////////////////////////////////////////////
  301.     void redBlock() {
  302.         if (timeCount % 50 == 49) {
  303.             for (int i = 0; i > -1; i++) {
  304.                 int redBlockRow = (int) (Math.random() * numRows + 0);
  305.                 int redBlockColumn = (int) (Math.random() * numColumns + 0);
  306.  
  307.                 if (board[redBlockRow][redBlockColumn] == SNAKE) {
  308.                     redBlock();
  309.                 } else if (board[redBlockRow][redBlockColumn] == DEATH) {
  310.                     redBlock();
  311.                 } else {
  312.                     board[redBlockRow][redBlockColumn] = DEATH;
  313.                 }
  314.             }
  315.         }
  316.     }
  317.  
  318.     ////////////////////////////////////////////////////////////////////////////
  319.     void partyModeBG() {
  320.         Color RGB = new Color(R, G, B);
  321.         g.setColor(RGB);
  322.     }
  323.  
  324.     ////////////////////////////////////////////////////////////////////////////
  325.     void partyModeSnake() {
  326.         Color RGB = new Color(B, R, G);
  327.         g.setColor(RGB);
  328.     }
  329.  
  330.     ////////////////////////////////////////////////////////////////////////////
  331.     void partyModeLines() {
  332.         Color RGB = new Color(R, G, B);
  333.         g.setColor(RGB);
  334.     }
  335.  
  336.     ////////////////////////////////////////////////////////////////////////////
  337.     void handleDeath() {
  338.         if (gameOver) {
  339.             return;
  340.         }
  341.  
  342.         if (XMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
  343.             gameOver = true;
  344.             inGame = false;
  345.         }
  346.  
  347.         if (YMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
  348.             gameOver = true;
  349.             inGame = false;
  350.         }
  351.  
  352.         if (XMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
  353.             inGame = false;
  354.             gameOver = true;
  355.         }
  356.  
  357.         if (YMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
  358.             inGame = false;
  359.             gameOver = true;
  360.         }
  361.  
  362.         if (XMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
  363.             gameOver = true;
  364.             inGame = false;
  365.         }
  366.  
  367.         if (YMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
  368.             gameOver = true;
  369.             inGame = false;
  370.         }
  371.  
  372.         if (XMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
  373.             inGame = false;
  374.             gameOver = true;
  375.         }
  376.  
  377.         if (YMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
  378.             inGame = false;
  379.             gameOver = true;
  380.         }
  381.         if (gameOver == false) {
  382.             board[(numRows / 2) + YMov][(numColumns / 2) + XMov] = SNAKE;
  383.             Score++;
  384.         }
  385.     }
  386. /////////////////////////////////////////////////////////////////////////////////////
  387.  
  388.     void handleDir() {
  389.         //YMOV:1 is DOWN XMOV:1 is RIGHT
  390.         if (XMovDir == 1) {
  391.             XMov++;
  392.         }
  393.  
  394.         if (XMovDir == 2) {
  395.             XMov--;
  396.         }
  397.  
  398.         if (YMovDir == 1) {
  399.             YMov++;
  400.         }
  401.  
  402.         if (YMovDir == 2) {
  403.             YMov--;
  404.         }
  405.     }
  406. ////////////////////////////////////////////////////////////////////////////
  407. // needed for     implement runnable
  408.  
  409.     public void run() {
  410.         while (true) {
  411.             animate();
  412.             repaint();
  413.             double seconds = 1 / frameRate;    //time that 1 frame takes.
  414.             int miliseconds = (int) (1000.0 * seconds);
  415.             try {
  416.                 Thread.sleep(miliseconds);
  417.             } catch (InterruptedException e) {
  418.             }
  419.         }
  420.     }
  421. /////////////////////////////////////////////////////////////////////////
  422.  
  423.     public void reset() {
  424. //Allocate memory for the 2D array that represents the board.
  425.         board = new int[numRows + 1][numColumns + 1];
  426. //Initialize the board to be empty.
  427.         for (int zrow = 0; zrow < numRows; zrow++) {
  428.             for (int zcolumn = 0; zcolumn < numColumns; zcolumn++) {
  429.                 board[zrow][zcolumn] = EMPTY;
  430.             }
  431.         }
  432.         board[numRows / 2][numColumns / 2] = SNAKE;
  433.         XMovDir = 1;
  434.         YMovDir = 0;
  435.         XMov = 0;
  436.         YMov = 0;
  437.         gameOver = false;
  438.         inGame = false;
  439.         Score = 0;
  440.         boxSec = 5;
  441.     }
  442. /////////////////////////////////////////////////////////////////////////
  443.  
  444.     public void animate() {
  445.         if (animateFirstTime) {
  446.             animateFirstTime = false;
  447.             if (xsize != getSize().width || ysize != getSize().height) {
  448.                 xsize = getSize().width;
  449.                 ysize = getSize().height;
  450.             }
  451.             reset();
  452.  
  453.         }
  454.         if (gameOver) {
  455.             return;
  456.         }
  457.         if (timeCount % (int) (frameRate / boxSec) == (int) (frameRate / boxSec) - 1 && inGame) {
  458.  
  459.             handleDir();
  460.  
  461.             if (canClip) {
  462.                 handleBorderClip();
  463.             } else {
  464.                 handleBorderEnd();
  465.             }
  466.  
  467.             handleDeath();
  468.             redBlock();
  469.  
  470.             //Scaling Speed
  471.             if (timeCount % 50 == 49 && sSpeed) {
  472.                 boxSec += 1;
  473.             }
  474.         }
  475.  
  476.         //Score Updater
  477.         if (hScore < Score) {
  478.             hScore = Score;
  479.         }
  480.  
  481.         //Time Count
  482.         timeCount++;
  483.  
  484.         //RGB
  485.         if (mode == 2) {
  486.             R += 3;
  487.             G += 4;
  488.             B += 10;
  489.             if (R >= 255) {
  490.                 R = 0;
  491.             }
  492.             if (G >= 255) {
  493.                 G = 0;
  494.             }
  495.             if (B >= 255) {
  496.                 B = 0;
  497.             }
  498.         }
  499.     }
  500.  
  501. ////////////////////////////////////////////////////////////////////////////
  502.     public void start() {
  503.         if (relaxer == null) {
  504.             relaxer = new Thread(this);
  505.             relaxer.start();
  506.         }
  507.     }
  508. ////////////////////////////////////////////////////////////////////////////
  509.  
  510.     public void stop() {
  511.         if (relaxer.isAlive()) {
  512.             relaxer.stop();
  513.         }
  514.         relaxer = null;
  515.     }
  516.  
  517. /////////////////////////////////////////////////////////////////////////
  518.     public int getX(int x) {
  519.         return (x + XBORDER + WINDOW_BORDER);
  520.     }
  521.  
  522.     public int getY(int y) {
  523.         return (y + YBORDER + YTITLE);
  524.     }
  525.  
  526.     public int getYNormal(int y) {
  527.         return (-y + YBORDER + YTITLE + getHeight2());
  528.     }
  529.  
  530.     public int getWidth2() {
  531.         return (xsize - 2 * (XBORDER + WINDOW_BORDER));
  532.     }
  533.  
  534.     public int getHeight2() {
  535.         return (ysize - 2 * YBORDER - WINDOW_BORDER - YTITLE);
  536.     }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement