Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = 31;
- static final int numColumns = 61;
- 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 time;
- boolean game;
- boolean snakeSpeed;
- boolean twoPlayer;
- double fps = 60.0;
- double sps;
- double dps = 1;
- int score;
- int highscore;
- int R;
- int G;
- int B;
- final int EMPTY = 0;
- final int SNAKE = 1;
- final int DEATH = 2;
- final int SNAKE2 = 3;
- int board[][];
- int currentRow;
- int currentColumn;
- int rowDir;
- int columnDir;
- int currentRow2;
- int currentColumn2;
- int rowDir2;
- int columnDir2;
- boolean gameOver;
- 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 (e.VK_W == e.getKeyCode()) {
- game = true;
- rowDir = -1;
- columnDir = 0;
- } else if (e.VK_S == e.getKeyCode()) {
- game = true;
- rowDir = 1;
- columnDir = 0;
- } else if (e.VK_A == e.getKeyCode()) {
- game = true;
- columnDir = -1;
- rowDir = 0;
- } else if (e.VK_D == e.getKeyCode()) {
- game = true;
- columnDir = 1;
- rowDir = 0;
- }
- else if (e.VK_UP == e.getKeyCode()) {
- game = true;
- rowDir2 = -1;
- columnDir2 = 0;
- } else if (e.VK_DOWN == e.getKeyCode()) {
- game = true;
- columnDir2 = 0;
- rowDir2 = 1;
- } else if (e.VK_RIGHT == e.getKeyCode()) {
- game = true;
- columnDir2 = 1;
- rowDir2 = 0;
- }
- else if (e.VK_LEFT == e.getKeyCode()) {
- game = true;
- columnDir2 = -1;
- rowDir2 = 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.gray);
- 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
- g.setColor(Color.black);
- g.fillPolygon(x, y, 4);
- // draw border
- g.setColor(Color.black);
- g.drawPolyline(x, y, 5);
- if (animateFirstTime) {
- gOld.drawImage(image, 0, 0, null);
- return;
- }
- g.setColor(Color.black);
- //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
- Color RGB = new Color(R,G,B);
- for (int zrow=0;zrow<numRows;zrow++)
- {
- for (int zcolumn=0;zcolumn<numColumns;zcolumn++)
- {
- if (board[zrow][zcolumn] == SNAKE)
- {
- g.setColor(Color.green);
- g.fillRect(getX(0)+zcolumn*getWidth2()/numColumns,
- getY(0)+zrow*getHeight2()/numRows,
- getWidth2()/numColumns,
- getHeight2()/numRows);
- }
- else if (board[zrow][zcolumn] == DEATH)
- {
- g.setColor(Color.red);
- g.fillRect(getX(0)+zcolumn*getWidth2()/numColumns,
- getY(0)+zrow*getHeight2()/numRows,
- getWidth2()/numColumns,
- getHeight2()/numRows);
- }
- else if (board[zrow][zcolumn] == SNAKE2)
- {
- g.setColor(Color.yellow);
- g.fillRect(getX(0)+zcolumn*getWidth2()/numColumns,
- getY(0)+zrow*getHeight2()/numRows,
- getWidth2()/numColumns,
- getHeight2()/numRows);
- }
- }
- }
- if(gameOver){
- g.setFont(new Font("Arial Black",Font.PLAIN,92));
- g.setColor(Color.green);
- g.drawString("GAME OVER",getX(getWidth2()/3-10),getYNormal(getHeight2()/2));
- }
- g.setFont(new Font("Arial",Font.PLAIN,12));
- g.setColor(Color.black);
- g.drawString("Score: " + score,getX(0),getYNormal(-15));
- g.drawString("Highscore: " + highscore,getX(100),getYNormal(-15));
- gOld.drawImage(image, 0, 0, null);
- }
- ////////////////////////////////////////////////////////////////////////////
- // needed for implement runnable
- public void run() {
- while (true) {
- animate();
- repaint();
- double seconds = 1/fps; //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][numColumns];
- //Initialize the board to be empty.
- for (int zrow = 0;zrow < numRows;zrow++)
- {
- for (int zcolumn = 0;zcolumn < numColumns;zcolumn++)
- board[zrow][zcolumn] = EMPTY;
- }
- time = 0;
- game = false;
- score = 0;
- snakeSpeed = false;
- twoPlayer = false;
- R=0;
- G=0;
- B=0;
- if(twoPlayer){
- currentRow = numRows/2;
- currentColumn = numColumns/2-1;
- }
- else
- currentRow = numRows/2;
- currentColumn = numColumns/2;
- currentRow2 = numRows/2;
- currentColumn2 = numColumns/2+1;
- board[currentRow][currentColumn] = SNAKE;
- if(twoPlayer)
- board[currentRow2][currentColumn2] = SNAKE2;
- rowDir = -1;
- columnDir = 0;
- rowDir2 = -1;
- columnDir2 = 0;
- if(snakeSpeed)
- sps = 5;
- else
- sps = 10;
- gameOver = false;
- }
- /////////////////////////////////////////////////////////////////////////
- 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(game && time % (int)(fps/sps) == (int)(fps/sps)-1){
- currentColumn += columnDir;
- currentRow += rowDir;
- if(twoPlayer){
- currentColumn2 += columnDir2;
- currentRow2 += rowDir2;
- }
- if(currentColumn == numColumns)
- currentColumn = 0;
- else if(currentColumn == -1)
- currentColumn = numColumns-1;
- else if(currentRow == numRows)
- currentRow = 0;
- else if(currentRow == -1)
- currentRow = numRows-1;
- if(twoPlayer){
- if(currentColumn2 == numColumns)
- currentColumn2 = 0;
- else if(currentColumn2 == -1)
- currentColumn2 = numColumns-1;
- else if(currentRow2 == numRows)
- currentRow2 = 0;
- else if(currentRow2 == -1)
- currentRow2 = numRows-1;
- }
- if(board[currentRow][currentColumn] == SNAKE){
- gameOver = true;
- if(score > highscore)
- highscore = score;
- }
- else if(board[currentRow][currentColumn] == DEATH){
- gameOver = true;
- if(score > highscore)
- highscore = score;
- }
- else if(board[currentRow][currentColumn] == SNAKE2){
- gameOver = true;
- if(score > highscore)
- highscore = score;
- }
- else{
- board[currentRow][currentColumn] = SNAKE;
- score++;
- }
- if(twoPlayer){
- if(board[currentRow2][currentColumn2] == SNAKE){
- gameOver = true;
- if(score > highscore)
- highscore = score;
- }
- else if(board[currentRow2][currentColumn2] == DEATH){
- gameOver = true;
- if(score > highscore)
- highscore = score;
- }
- else if(board[currentRow2][currentColumn2] == SNAKE2){
- gameOver = true;
- if(score > highscore)
- highscore = score;
- }
- else{
- board[currentRow2][currentColumn2] = SNAKE2;
- score++;
- }
- }
- }
- if(game && time % (int)(fps/dps) == (int)(fps/dps)-1)
- {
- death();
- }
- if(snakeSpeed && time % 600 == 599 && sps<12)
- sps+=1;
- time++;
- }
- ////////////////////////////////////////////////////////////////////////////
- public void death(){
- if(gameOver)
- return;
- int randomRow = (int)(Math.random()*numRows);
- int randomColumn = (int)(Math.random()*numColumns);
- if(board[randomRow][randomColumn] == EMPTY)
- board[randomRow][randomColumn] = DEATH;
- else
- death();
- }
- ////////////////////////////////////////////////////////////////////////////
- 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