Home>

1, using mvc (model, view, control) framework mode

2. The tree diagram of the relationship between packages and classes is:

3. Source code:

package com.huai;
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.huai.listener.snakelistener;
import com.huai.util.constant;
public class snake {
 //Related to the snake's listener
 set<snakelistener>snakelistener=new hashset<snakelistener>();
 //Save the snake's body node with a linked list
 linkedlist<point>body=new linkedlist<point>();
 private boolean life=true;
 //The default speed is 400ms
 private int speed=400;
 private point lasttail=null;
 private boolean pause=false;
 //define each 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;
 int newdirection=right;
 int olddirection=newdirection;
 public snake () {
 initial ();
 }
 public void initial () {
 //Empty all nodes first
 body.removeall (body);
 int x=constant.width/2;
 int y=constant.height/2;
 //The default length of the snake is 7
 for (int i=0;i<7;i ++) {
 body.add (new point (x--, y));
 }
 }
 public void setspeed (int speed) {
 this.speed=speed;
 }
 public void setlife (boolean life) {
 this.life=life;
 }
 public boolean getlife () {
 return this.life;
 }
 public boolean getpause () {
 return this.pause;
 }
 public void setpause (boolean pause) {
 this.pause=pause;
 }
 public void move () {
 //The data structure used for the implementation of the snake movement is:a snake tail deletes a node,Add a node to the snake head.
 system.out.println ("snake move");
 if (! (olddirection + newdirection == 0)) {
 olddirection=newdirection;
 }
 lasttail=body.removelast ();
 int x=body.getfirst (). x;
 int y=body.getfirst (). y;
 switch (olddirection) {
 case up:
 y--;
 break;
 case down:
 y ++;
 break;
 case left:
 x--;
 break;
 case right:
 x ++;
 break;
 }
 point point=new point (x, y);
 body.addfirst (point);
 }
 //When eating a food,Add the deleted snaketail node back
 public void eatfood () {
 system.out.println ("snake eat food");
 body.addlast (lasttail);
 }
 public boolean iseatitself () {
 for (int i=2;i<body.size ();i ++) {
 if (body.getfirst (). equals (body.get (i))) {
 return true;
 }
 }
 return false;
 }
 public void drawme (graphics g) {
 system.out.println ("draw snake");
 //Cycle print each body node of the snake
 for (point p:body) {
 if (p.equals (body.getfirst ())) {
 //When it is a snake head,Set the color to orange
 g.setcolor (color.orange);
 } else {
 g.setcolor (color.pink);
 }
 g.fill3drect (p.x * constant.cell_size, p.y * constant.cell_size, constant.cell_size, constant.cell_size, true);
 }
 }
 public void changedirection (int direction) {
 system.out.println ("snake changedirection");
 this.newdirection=direction;
 }
 //Inner class, add a game thread
 public class drivesnake implements runnable {@override
 public void run () {
 while (life) {
 if (! pause) {
 //Pause the game as long as you don't move the snake
 move ();
 }
 //Listen to the snake every time it moves
 for (snakelistener listener:snakelistener) {
 listener.snakemove (snake.this);
 }
 try {
 thread.sleep (speed);
 } catch (interruptedexception e) {
 e.printstacktrace ();
 }
 }
 }
 }
 public void startmove () {
 new thread (new drivesnake ()). start ();
 }
 //Method of adding listener
 public void addsnakelistener (snakelistener listener) {
 if (listener!=null) {
 snakelistener.add (listener);
 }
 }
 //Return the first body node of the snake
 public point gethead () {
 return body.getfirst ();
 }
 //Return all body nodes of the snake
 public linkedlist<point>getsnakebody () {
 return this.body;
 }
 public boolean died () {
 this.life=false;
 return true;
 }
}
package com.huai;
import java.awt.color;
import java.awt.graphics;
import java.awt.point;
import com.huai.util.constant;
public class food {
 private int x=0;
 private int y=0;
 //Set the default color of food to red
 private color color=color.red;
 public void newfood (point point) {
 x=point.x;
 y=point.y;
 }
 //determine if it was eaten
 public boolean isate (snake snake) {
 if (snake.gethead (). equals (new point (x, y))) {
 system.out.println ("food isate");
 return true;
 }
 return false;
 }
 public void setfoodcolor (color color) {
 this.color=color;
 }
 public color getfoodcolor () {
 return this.color;
 }
 public void drawme (graphics g) {
 system.out.println ("draw food");
 g.setcolor (this.getfoodcolor ());
 g.fill3drect (x * constant.cell_size, y * constant.cell_size, constant.cell_size, constant.cell_size, true);
 }
}
package com.huai;
import java.awt.color;
import java.awt.graphics;
import java.awt.point;
import java.util.linkedlist;
import java.util.random;
import com.huai.util.constant;
public class ground {
 //In a two-dimensional array,When 1, it means brick
 private int [] [] rock=new int [constant.width] [constant.height];
 private boolean drawline=true;
 public boolean isdrawline () {
 return drawline;
 }
 public void setdrawline (boolean drawline) {
 this.drawline=drawline;
 } public ground () {
 for (int x=0;x<constant.width;x ++) {
 for (int y=0;y<constant.height;y ++) {
 rock [0] [y]=1;
 rock [x] [0]=1;
 rock [constant.width-1] [y]=1;
 rock [y] [constant.height-1]=1;
 }
 }
 }
 //Print the game's grid
 public void drawgrid (graphics g) {
 for (int x=2;x<constant.width;x ++) {
 g.drawline (x * constant.cell_size, 0, x * constant.cell_size, constant.cell_size * constant.height);
 }
 for (int y=2;y<constant.height;y ++) {
 g.drawline (0, y * constant.cell_size, constant.width * constant.cell_size, y * constant.cell_size);
 }
 }
 public void drawme (graphics g) {
 system.out.println ("draw ground");
 //print the fence
 g.setcolor (color.pink);
 for (int x=0;x<constant.width;x ++) {
 for (int y=0;y<constant.height;y ++) {
 if (rock [x] [y] == 1) {
 g.fill3drect (x * constant.width, y * constant.height, constant.cell_size, constant.cell_size, true);
 }
 }
 }
 if (isdrawline ()) {
 g.setcolor (color.yellow);
 this.drawgrid (g);
 }
 }
 //Get a random point
 public point getrandompoint (snake snake, thorn thorn) {
 random random=new random ();
 boolean isundersnakebody=false;
 boolean isunderthorn=false;
 int x, y=0;
 linkedlist<point>snakebody=snake.getsnakebody ();
 linkedlist<point>thorns=thorn.getthorns ();
 do {
 x=random.nextint (constant.width);
 y=random.nextint (constant.height);
 for (point p:snakebody) {
 if (x == p.x&&y == p.y) {
 isundersnakebody=true;
 }
 }
 for (point point:thorns) {
 if (x == point.x&&y == point.y) {
 isunderthorn=true;
 }
 }
 } while (rock [x] [y] == 1&&! isundersnakebody&&! isunderthorn);
 return new point (x, y);
 }
 public boolean issnakehitrock (snake snake) {
 system.out.println ("snack hit rock");
 for (int i=0;i<constant.width;i ++) {
 for (int j=0;j<constant.height;j ++) {
 if (snake.gethead (). x == i&&
 snake.gethead (). y == j&&rock [i] [j] == 1) {
 return true;
 }
 }
 }
 return false;
 }
}
package com.huai
import java.awt.color;
import java.awt.graphics;
import java.awt.point;
import java.util.linkedlist;
import com.huai.util.constant;
public class thorn {int x, y;
 private linkedlist<point>thorns=new linkedlist<point>();
 //Return a linked list of all thorns
 public linkedlist<point>getthorns () {
 return this.thorns;
 }
 public void newthorn (point point) {
 x=point.x;
 y=point.y;
 thorns.add (new point (x, y));
 }
 public void drawme (graphics g) {
 system.out.println ("draw thorns");
 for (point p:thorns) {
 int [] xb={p.x * constant.cell_size, (p.x * constant.cell_size + constant.cell_size/2), (p.x * constant.cell_size + constant.cell_size), (p.x * constant.cell_size + constant.cell_size/4 * 3), (p.x * constant.cell_size + constant.cell_size), (p.x * constant.cell_size + constant.cell_size/2), p.x * constant.cell_size, (p.x * constant.cell_size + constant.cell_size/4), p.x * constant.cell_size};
 int [] yb={p.y * constant.cell_size, (p.y * constant.cell_size + constant.cell_size/4), p.y * constant.cell_size, (p.y * constant.cell_size + constant.cell_size/2), (p.y * constant.cell_size + constant.cell_size), (int) (p.y * constant.cell_size + constant.cell_size * 0.75), (p.y * constant.cell_size + constant.cell_size), (p.y * constant.cell_size + constant.cell_size/2), p.y * constant.cell_size};
 g.setcolor (color.darkgray);
 g.fillpolygon (xb, yb, 8);
 }
 }
 public boolean issnakehitthorn (snake snake) {
 for (point points:thorns) {
 if (snake.gethead (). equals (points)) {
 system.out.println ("hit thorn");
 return true;
 }
 }
 return false;
 }
 }
package com.huai.listener;
import com.huai.snake;
public interface snakelistener {
 public void snakemove (snake snake);
}
 package com.huai.util;public class constant {
 public static int cell_size=20;
 public static int width=20;
 public static int height=20;
}
package com.huai.view;
import java.awt.color;
import java.awt.graphics;import javax.swing.jpanel;import com.huai.food;
import com.huai.ground;
import com.huai.snake;
import com.huai.thorn;
import com.huai.util.constant;public class gamepanel extends jpanel {
 /**
 *
 * /
 private static final long serialversionuid=1l;
 snake snake;
 food food;
 ground ground;
 thorn thorn;
 public void display (snake snake, food food, ground ground, thorn thorn) {
 this.snake=snake;
 this.food=food;
 this.ground=ground;
 this.thorn=thorn;
 system.out.println ("display gamepanel");
 this.repaint ();
 }
 @override
 protected void paintcomponent (graphics g) {
 if (snake!=null&&food!=null&&ground!=null) {
 g.setcolor (color.lightgray);
 g.fillrect (0, 0, constant.width * constant.cell_size, constant.height * constant.cell_size);
 snake.drawme (g);
 food.drawme (g);
 ground.drawme (g);
 thorn.drawme (g);
 } else {
 system.out.println ("snake=null || food=null || ground=null");
 }
 }
}
package com.huai.controller;
import java.awt.event.keyadapter;
import java.awt.event.keyevent;import com.huai.food;
import com.huai.ground;
import com.huai.snake;
import com.huai.thorn;
import com.huai.listener.snakelistener;
import com.huai.view.gamepanel;public class controller extends keyadapter implements snakelistener {
/**
 * Controls for the entire game,Belongs to controller in mvc design framework
 * This includes controlling the game's listening events and game logic
 * /
 snake snake;
 food food;
 ground ground;
 gamepanel gamepanel;
 thorn thorn;
 public controller () {}
 //Use the constructor to initialize the object
 public controller (snake snake, food food, ground ground, gamepanel gamepanel, thorn thorn) {
 super ();
 this.snake=snake;
 this.food=food;
 this.ground=ground;
 this.gamepanel=gamepanel;
 this.thorn=thorn;
 }
 @override
 public void keypressed (keyevent e) {
 switch (e.getkeycode ()) {
 case keyevent.vk_up:
 snake.changedirection (snake.up);
 snake.setspeed (150);
 break;
 case keyevent.vk_down:
 snake.changedirection (snake.down);
 snake.setspeed (150);
 break;
 case keyevent.vk_left:
 snake.changedirection (snake.left);
 snake.setspeed (150);
 break;
 case keyevent.vk_right:
 snake.changedirection (snake.right);
 snake.setspeed (150);
 break;
 case keyevent.vk_enter:
 if (! snake.getpause ()&&snake.getlife ()) {
 //Pause the game
 snake.setpause (true) ;;
 } else if (! snake.getlife ()) {
 //restart the game
 snake.setlife (true);
 snake.initial ();
 snake.changedirection (snake.up);
 thorn.getthorns (). removeall (thorn.getthorns ());
 this.startgame ();
 } else {
 snake.setpause (false);
 }
 break;
 case keyevent.vk_l:
 //Whether to display the game grid when the l button is pressed
 if (ground.isdrawline ()) {
 ground.setdrawline (false);
 } else {
 ground.setdrawline (true);
 }
 break;
 }
 }
 @override
 public void keyreleased (keyevent e) {
 switch (e.getkeycode ()) {
 case keyevent.vk_up:
 snake.setspeed (400);
 break;
 case keyevent.vk_down:
 snake.setspeed (400);
 break;
 case keyevent.vk_left:
 snake.setspeed (400);
 break;
 case keyevent.vk_right:
 snake.setspeed (400);
 break;
 }
 }
 //This is the method to implement the snakelistener listener override
 @override
 public void snakemove (snake snake) {
 //Show snake, food, ground, and thorn
 gamepanel.display (snake, food, ground, thorn);
 if (ground.issnakehitrock (snake)) {
 snake.died ();
 }
 if (snake.iseatitself ()) {
 snake.died ();
 }
 if (food.isate (snake)) {
 snake.eatfood ();
 food.newfood (ground.getrandompoint (snake, thorn));
 thorn.newthorn (ground.getrandompoint (snake, thorn));
 }
 if (thorn.issnakehitthorn (snake)) {
 snake.died ();
 }
 }
 public void startgame () {
 snake.startmove ();//This will start a new thread
 food.newfood (ground.getrandompoint (snake, thorn));
 thorn.newthorn (ground.getrandompoint (snake, thorn));
 }
}
package com.huai.game;
import java.awt.borderlayout;
import java.awt.color;import javax.swing.jframe;
import javax.swing.jlabel;import com.huai.food;
import com.huai.ground;
import com.huai.snake;
import com.huai.thorn;
import com.huai.controller.controller;
import com.huai.util.constant;
import com.huai.view.gamepanel;public class game {
 public static void main (string args []) {
 snake snake=new snake ();
 food food=new food ();
 gamepanel gamepanel=new gamepanel ();
 ground ground=new ground ();
 thorn thorn=new thorn ();
 controller controller=new controller (snake, food, ground, gamepanel, thorn);
 jframe frame=new jframe ("Little Brother's Little Snake Game");
 frame.add (gamepanel);
 frame.setbackground (color.magenta);
 frame.setsize (constant.width * constant.cell_size + 6, constant.height * constant.cell_size + 40);
 gamepanel.setsize (constant.width * constant.cell_size, constant.height * constant.cell_size);
 frame.add (gamepanel);
 frame.setresizable (false);//Can't change the window size
 frame.setdefaultcloseoperation (jframe.exit_on_close);
 frame.setvisible (true);
 gamepanel.addkeylistener (controller);
 snake.addsnakelistener (controller);
 frame.addkeylistener (controller);
 jlabel label1=new jlabel ();
 label1.setbounds (0, 400, 400, 100);
 label1.settext ("enter =>>pause or again;"
 + "l =>drawgrid");
 label1.setforeground (color.black);
 frame.add (label1, borderlayout.south);
 controller.startgame ();
 }
}
  • Previous Vue save user login status example code
  • Next PHP session implements shopping cart function
  • Trends