Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package snake;
- import java.io.*;
- import java.awt.*;
- import java.awt.geom.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Snake extends JFrame implements Runnable {
- static final int numRows = 25;
- static final int numColumns = 27;
- static final int XBORDER = 20;
- static final int YBORDER = 20;
- static final int YTITLE = 30;
- static final int WINDOW_BORDER = 8;
- static final int WINDOW_WIDTH = 2 * (WINDOW_BORDER + XBORDER) + numColumns * 30;
- static final int WINDOW_HEIGHT = YTITLE + WINDOW_BORDER + 2 * YBORDER + numRows * 30;
- boolean animateFirstTime = true;
- int xsize = -1;
- int ysize = -1;
- Image image;
- Graphics2D g;
- int timeCount;
- double frameRate = 144;
- double boxSec;
- boolean sSpeed;
- final int EMPTY = 0;
- final int SNAKE = 1;
- final int DEATH = 2;
- int board[][];
- int XMovDir;
- int YMovDir;
- int XMov;
- int YMov;
- int XMovDir2;
- int YMovDir2;
- int XMov2;
- int YMov2;
- //1 Norm, 2 Night,3 Party
- int mode = 1;
- //Party RGB
- int R;
- int G;
- int B;
- boolean canClip = true;
- boolean gameOver;
- boolean inGame;
- int Score;
- int hScore;
- static Snake frame;
- public static void main(String[] args) {
- frame = new Snake();
- frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- public Snake() {
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- if (e.BUTTON1 == e.getButton()) {
- //left button
- // location of the cursor.
- int xpos = e.getX();
- int ypos = e.getY();
- }
- if (e.BUTTON3 == e.getButton()) {
- //right button
- reset();
- }
- repaint();
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseDragged(MouseEvent e) {
- repaint();
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent e) {
- repaint();
- }
- });
- addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- if (gameOver) {
- return;
- }
- if (e.VK_UP == e.getKeyCode()) {
- if (YMovDir != 1) {
- inGame = true;
- XMovDir = 0;
- YMovDir = 2;
- }
- } else if (e.VK_DOWN == e.getKeyCode()) {
- if (YMovDir != 2) {
- inGame = true;
- XMovDir = 0;
- YMovDir = 1;
- }
- } else if (e.VK_LEFT == e.getKeyCode()) {
- if (XMovDir != 1) {
- inGame = true;
- XMovDir = 2;
- YMovDir = 0;
- }
- } else if (e.VK_RIGHT == e.getKeyCode()) {
- if (XMovDir != 2) {
- inGame = true;
- XMovDir = 1;
- YMovDir = 0;
- }
- }
- repaint();
- }
- });
- init();
- start();
- }
- Thread relaxer;
- ////////////////////////////////////////////////////////////////////////////
- public void init() {
- requestFocus();
- }
- ////////////////////////////////////////////////////////////////////////////
- public void destroy() {
- }
- ////////////////////////////////////////////////////////////////////////////
- public void paint(Graphics gOld) {
- if (image == null || xsize != getSize().width || ysize != getSize().height) {
- xsize = getSize().width;
- ysize = getSize().height;
- image = createImage(xsize, ysize);
- g = (Graphics2D) image.getGraphics();
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- }
- //fill background
- g.setColor(Color.white);
- g.fillRect(0, 0, xsize, ysize);
- int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
- int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
- //fill border
- if (mode == 1) {
- g.setColor(Color.black);
- } else if (mode == 2) {
- partyModeBG();
- } else {
- g.setColor(Color.white);
- }
- g.fillPolygon(x, y, 4);
- // draw border
- g.setColor(Color.red);
- g.drawPolyline(x, y, 5);
- if (animateFirstTime) {
- gOld.drawImage(image, 0, 0, null);
- return;
- }
- if (mode == 2) {
- partyModeLines();
- } else {
- g.setColor(Color.red);
- }
- //horizontal lines
- for (int zi = 1; zi < numRows; zi++) {
- g.drawLine(getX(0), getY(0) + zi * getHeight2() / numRows,
- getX(getWidth2()), getY(0) + zi * getHeight2() / numRows);
- }
- //vertical lines
- for (int zi = 1; zi < numColumns; zi++) {
- g.drawLine(getX(0) + zi * getWidth2() / numColumns, getY(0),
- getX(0) + zi * getWidth2() / numColumns, getY(getHeight2()));
- }
- //Display the objects of the board
- for (int zrow = 0; zrow < numRows; zrow++) {
- for (int zcolumn = 0; zcolumn < numColumns; zcolumn++) {
- if (board[zrow][zcolumn] == SNAKE) {
- if (mode == 2) {
- partyModeSnake();
- } else {
- g.setColor(Color.green);
- }
- g.fillRect(getX(0) + zcolumn * getWidth2() / numColumns,
- getY(0) + zrow * getHeight2() / numRows,
- getWidth2() / numColumns,
- getHeight2() / numRows);
- }
- if (board[zrow][zcolumn] == DEATH) {
- if (mode == 2) {
- partyModeSnake();
- } else {
- g.setColor(Color.red);
- }
- g.fillRect(getX(0) + zcolumn * getWidth2() / numColumns,
- getY(0) + zrow * getHeight2() / numRows,
- getWidth2() / numColumns,
- getHeight2() / numRows);
- }
- }
- }
- handleScore();
- if (gameOver) {
- g.setColor(Color.yellow);
- g.setFont(new Font("Arial Narrow", Font.PLAIN, 100));
- g.drawString("GAME OVER", getWidth2() / 2 - 200, getHeight2() / 2);
- }
- gOld.drawImage(image, 0, 0, null);
- }
- ////////////////////////////////////////////////////////////////////////////
- void handleScore() {
- g.setColor(Color.black);
- g.setFont(new Font("Arial Narrow", Font.PLAIN, 20));
- g.drawString("Blocks traveled:" + Score, getX(0), getYNormal(-17));
- g.drawString("Session high score:" + hScore, getX(350), getYNormal(-17));
- }
- ///////////////////////////////////////////////////////////////////////
- public static String read() {
- byte[] buffer = new byte[10];
- try {
- int numBytes = System.in.read(buffer);
- } catch (IOException e) {
- System.out.print("Error: " + e);
- System.exit(1);
- }
- String str = new String(buffer);
- int ball = 5;
- return (str);
- }
- ////////////////////////////////////////////////////////////////////////////
- public static void write(String str) {
- System.out.print(str);
- }
- /////////////////////////////////////////////////////////
- void handleBorderClip() {
- if (numColumns / 2 + XMov == numColumns) {
- XMov = -(numColumns / 2);
- }
- if (XMov == -(numColumns / 2 + 1)) {
- XMov = numColumns / 2;
- }
- if (numRows / 2 + YMov == numRows) {
- YMov = -(numRows / 2);
- }
- if (YMov == -(numRows / 2 + 1)) {
- YMov = numRows / 2;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- void handleBorderEnd() {
- if (numColumns / 2 + XMov == numColumns || XMov == -(numColumns / 2 + 1) || numRows / 2 + YMov == numRows || YMov == -(numRows / 2) - 1) {
- gameOver = true;
- inGame = false;
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- void redBlock() {
- if (timeCount % 50 == 49) {
- for (int i = 0; i > -1; i++) {
- int redBlockRow = (int) (Math.random() * numRows + 0);
- int redBlockColumn = (int) (Math.random() * numColumns + 0);
- if (board[redBlockRow][redBlockColumn] == SNAKE) {
- redBlock();
- } else if (board[redBlockRow][redBlockColumn] == DEATH) {
- redBlock();
- } else {
- board[redBlockRow][redBlockColumn] = DEATH;
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- void partyModeBG() {
- Color RGB = new Color(R, G, B);
- g.setColor(RGB);
- }
- ////////////////////////////////////////////////////////////////////////////
- void partyModeSnake() {
- Color RGB = new Color(B, R, G);
- g.setColor(RGB);
- }
- ////////////////////////////////////////////////////////////////////////////
- void partyModeLines() {
- Color RGB = new Color(R, G, B);
- g.setColor(RGB);
- }
- ////////////////////////////////////////////////////////////////////////////
- void handleDeath() {
- if (gameOver) {
- return;
- }
- if (XMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
- gameOver = true;
- inGame = false;
- }
- if (YMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
- gameOver = true;
- inGame = false;
- }
- if (XMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
- inGame = false;
- gameOver = true;
- }
- if (YMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == SNAKE) {
- inGame = false;
- gameOver = true;
- }
- if (XMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
- gameOver = true;
- inGame = false;
- }
- if (YMovDir == 1 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
- gameOver = true;
- inGame = false;
- }
- if (XMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
- inGame = false;
- gameOver = true;
- }
- if (YMovDir == 2 && board[(numRows / 2) + YMov][(numRows / 2) + XMov + 1] == DEATH) {
- inGame = false;
- gameOver = true;
- }
- if (gameOver == false) {
- board[(numRows / 2) + YMov][(numColumns / 2) + XMov] = SNAKE;
- Score++;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////
- void handleDir() {
- //YMOV:1 is DOWN XMOV:1 is RIGHT
- if (XMovDir == 1) {
- XMov++;
- }
- if (XMovDir == 2) {
- XMov--;
- }
- if (YMovDir == 1) {
- YMov++;
- }
- if (YMovDir == 2) {
- YMov--;
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- // needed for implement runnable
- public void run() {
- while (true) {
- animate();
- repaint();
- double seconds = 1 / frameRate; //time that 1 frame takes.
- int miliseconds = (int) (1000.0 * seconds);
- try {
- Thread.sleep(miliseconds);
- } catch (InterruptedException e) {
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////
- public void reset() {
- //Allocate memory for the 2D array that represents the board.
- board = new int[numRows + 1][numColumns + 1];
- //Initialize the board to be empty.
- for (int zrow = 0; zrow < numRows; zrow++) {
- for (int zcolumn = 0; zcolumn < numColumns; zcolumn++) {
- board[zrow][zcolumn] = EMPTY;
- }
- }
- board[numRows / 2][numColumns / 2] = SNAKE;
- XMovDir = 1;
- YMovDir = 0;
- XMov = 0;
- YMov = 0;
- gameOver = false;
- inGame = false;
- Score = 0;
- boxSec = 5;
- }
- /////////////////////////////////////////////////////////////////////////
- public void animate() {
- if (animateFirstTime) {
- animateFirstTime = false;
- if (xsize != getSize().width || ysize != getSize().height) {
- xsize = getSize().width;
- ysize = getSize().height;
- }
- reset();
- }
- if (gameOver) {
- return;
- }
- if (timeCount % (int) (frameRate / boxSec) == (int) (frameRate / boxSec) - 1 && inGame) {
- handleDir();
- if (canClip) {
- handleBorderClip();
- } else {
- handleBorderEnd();
- }
- handleDeath();
- redBlock();
- //Scaling Speed
- if (timeCount % 50 == 49 && sSpeed) {
- boxSec += 1;
- }
- }
- //Score Updater
- if (hScore < Score) {
- hScore = Score;
- }
- //Time Count
- timeCount++;
- //RGB
- if (mode == 2) {
- R += 3;
- G += 4;
- B += 10;
- if (R >= 255) {
- R = 0;
- }
- if (G >= 255) {
- G = 0;
- }
- if (B >= 255) {
- B = 0;
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- public void start() {
- if (relaxer == null) {
- relaxer = new Thread(this);
- relaxer.start();
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- public void stop() {
- if (relaxer.isAlive()) {
- relaxer.stop();
- }
- relaxer = null;
- }
- /////////////////////////////////////////////////////////////////////////
- public int getX(int x) {
- return (x + XBORDER + WINDOW_BORDER);
- }
- public int getY(int y) {
- return (y + YBORDER + YTITLE);
- }
- public int getYNormal(int y) {
- return (-y + YBORDER + YTITLE + getHeight2());
- }
- public int getWidth2() {
- return (xsize - 2 * (XBORDER + WINDOW_BORDER));
- }
- public int getHeight2() {
- return (ysize - 2 * YBORDER - WINDOW_BORDER - YTITLE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement