Advertisement
Nichismoke

Untitled

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