A Java version of Snake game that I wrote very seriously, with a graphical interface, supports menu operation, keyboard monitoring, can accelerate, decelerate, count scores, set movement speed, set game background color, etc.! There should be no bugs, because I have completely modified them.
The following is the hierarchical relationship between the various packages and classes of the project:
The main running interface of the game is shown below:
The following are some codes. See this link for the detailed source code: Download the source code of Greedy Snake
Snake class:
package com.huowolf.entities; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; import com.huowolf.listener.SnakeListener; import com.huowolf.util.Global; public class Snake { //Constant representing direction public static final int UP = 1; public static final int DOWN = -1; public static final int LEFT = 2; public static final int RIGHT = -2; //Liser group private Set<SnakeListener> listeners = new HashSet<SnakeListener>(); //Storage list structure private LinkedList<Point> body = new LinkedList<Point>(); private boolean life; //Is private boolean pause alive; //Does the game private int oldDirection,newDirection; //Introduction of new and old directions (avoid invalid directions within a move time) private Point oldTail; //Old tail (used when eating food) private int foodCount = 0; //The quantity of food eaten private Color headColor; private Color bodyColor; private int sleepTime; public boolean isLife() { return life; } public int getSleepTime() { return sleepTime; } public void setSleepTime(int sleepTime) { this.sleepTime = sleepTime; } public void setHeadColor(Color headColor) { this.headColor = headColor; } public void setBodyColor(Color bodyColor) { this.bodyColor = bodyColor; } public void init() { int x = Global.WIDTH/2; int y = Global.HEIGHT/2; for(int i=0;i<3;i++) { body.addLast(new Point(x--,y)); } oldDirection = newDirection = RIGHT; foodCount = 0; life = true; pause = false; if(sleepTime==0) { sleepTime = 300; } } // Method to clear the snake's nodes (used to start a new game) public void clear() { body.clear(); } public void setLife(boolean life) { this.life = life; } public boolean getPause() { return pause; } public void setPause(boolean pause) { this.pause = pause; } // Method to change the state of pause constants public void changePause() { pause = !pause; } // Method to die for snake public void die() { life = false; } // Method to move public void move() { if(!(oldDirection + newDirection==0)) { oldDirection = newDirection ; } //Detail oldTail = body.removeLast(); int x = body.getFirst().x; int y = body.getFirst().y; switch(oldDirection) { case UP: y--; if(y<0) { y = Global.HEIGHT -1 ; } break; case DOWN: y++; if(y >= Global.HEIGHT) { y = 0; } break; case LEFT: x--; if(x<0) { x = Global.WIDTH-1; } break; case RIGHT: x++; if(x >= Global.WIDTH) { x = 0; } break; } Point newHead = new Point(x, y); // Add head body.addFirst(newHead); } // Change direction public void changeDirection(int direction) { newDirection = direction; } // Eat food public void eatFood() { body.addLast(oldTail); foodCount++; } //Get the amount of food you eat public int getFoodCount() { return foodCount; } //Does the snake eat its own body public boolean isEatBody() { for(int i=1;i<body.size();i++) { if(body.get(i).equals(this.getHead())) return true; } return false; } //Get the node representing the snake's head public Point getHead() { return body.getFirst(); } //Show itself public void drawMe(Graphics g) { if(bodyColor==null) { g.setColor(new Color(0x3333FF)); } else { g.setColor(bodyColor); } for(Point p : body) { g.fillRoundRect(px*Global.CELL_SIZE, py*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, 15,12 ); } drawHead(g); } //Draw snakehead public void drawHead(Graphics g) { if(headColor==null) g.setColor(Color.YELLOW); else { g.setColor(headColor); } g.fillRoundRect(getHead().x * Global.CELL_SIZE, getHead().y* Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, 15,12); } //The internal class private class SnakeDriver implements Runnable { public void run() { while(life) { if(pause==false) { move(); for(SnakeListener l : listeners) l.snakeMoved(Snake.this); } try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } } } } // Method to start thread public void begin() { new Thread(new SnakeDriver()).start(); } //Add listener public void addSnakeListener(SnakeListener l) { if(l != null) { listeners.add(l); } } //Speed up public void speedUp() { if(sleepTime>50) { sleepTime-=20; } } //Slow down public void speedDown() { if(sleepTime<700) { sleepTime+=20; } } }Food class:
package com.huowolf.entities; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import com.huowolf.util.Global; public class Food extends Point{ private static final long serialVersionUID = 1L; private Color foodColor; public void setFoodColor(Color foodColor) { this.foodColor = foodColor; } public Color getFoodColor() { return foodColor; } public void newFood(Point p) { setLocation(p); } public boolean isFoodEated(Snake snake) { return this.equals(snake.getHead()); } public void drawMe(Graphics g) { if(foodColor==null) { g.setColor(Color.RED); }else { g.setColor(foodColor); } g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true); } }Ground class
package com.huowolf.entities; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.Random; import com.huowolf.util.Global; public class Ground { private boolean [][] rocks = new boolean[Global.WIDTH][Global.HEIGHT]; private int mapType = 1; public int getMapType() { return mapType; } public void setMapType(int mapType) { this.mapType = mapType; } //Initialize the ground (clear the stone) public void clear() { for (int x = 0; x < Global.WIDTH; x++) for (int y = 0; y < Global.HEIGHT; y++) rocks[x][y] = false; } /** * Generate stone*/ public void generateRocks1() { for (int x = 0; x < Global.WIDTH; x++) rocks[x][0] = rocks[x][Global.HEIGHT - 1] = true; for (int y = 0; y < Global.HEIGHT; y++) rocks[0][y] = rocks[Global.WIDTH - 1][y] = true; } public void generateRocks2() { for (int y = 0; y < 6; y++) { rocks[0][y] = true; rocks[Global.WIDTH - 1][y] = true; rocks[0][Global.HEIGHT - 1 - y] = true; rocks[Global.WIDTH - 1][Global.HEIGHT - 1 - y] = true; } for (int y = 6; y < Global.HEIGHT - 6; y++) { rocks[6][y] = true; rocks[Global.WIDTH - 7][y] = true; } } public void generateRocks3() { for(int x=4;x<14;x++) rocks[x][5] = true; for(int j=5;j<15;j++) rocks[21][j] = true; for(int y=13;y<20;y++) rocks[14][y] = true; for(int x=2;x<10;x++) rocks[x][17] = true; for(int i=10;i<Global.WIDTH-3;i++) rocks[i][Global.HEIGHT-3] = true; } //Does the snake eat the stone public boolean isGroundEated(Snake snake) { for(int x=0; x<Global.WIDTH;x++) { for(int y=0; y<Global.HEIGHT;y++) { if(rocks[x][y] == true && (x==snake.getHead().x &&y==snake.getHead().y)) { return true; } } return false; } public Point getPoint() { Random random = new Random(); int x=0,y=0; do { x = random.nextInt(Global.WIDTH); y = random.nextInt(Global.HEIGHT); } while (rocks[x][y]==true); return new Point(x,y); } //Display method public void drawMe(Graphics g) { g.setColor(Color.DARK_GRAY); for(int x=0; x<Global.WIDTH;x++) { for(int y=0; y<Global.HEIGHT;y++) { if(rocks[x][y] == true) { g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true); } } } } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.