Java version of the snake (more complete version)

Source: Internet
Author: User

Very seriously write a Java version of the Snake game, graphical interface, support menu operation, keyboard monitoring, can accelerate, slow down, statistical score, set the speed of movement, set the game background color! There should be no bug, because I have changed the whole. Ha ha.

The following are the hierarchical relationships of the project packages and classes:


The main operating interface of the game is as follows:



The following is part of the code, detailed source see this link: http://pan.baidu.com/s/1bnubnzh

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 public static final int for direction = 1;public static final int down = -1;public static final int left = 2;public static final int right = -2;//Listener Group Private set<snakelistener> listeners = New hashset<snakelistener> ();//The list structure of the storage snake private linkedlist<point> BODY = new linkedlist<point> (); Private Boolean life;//whether the private Boolean pause;//is alive or not to pause the game private int olddirection,newdirection;//New, The introduction of the old direction (avoids the invalid direction within a single move time) private point oldtail;//the old tail (used when eating food) private int foodcount = 0;//The amount 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 of emptying the snake's node (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;} The state to change the pause constant public void changepause () {pause =!pause;} The way the snake died public void die () {life = false;} The snake moves the method public void Move () {if (! ( Olddirection + newdirection==0) {olddirection = newdirection;} Go tail 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);//Gathou 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 eaten public int getfoodcount () {return foodcount;} Whether the snake has eaten 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;} Gets the node that represents the head of the snake Public Point gethead () {return Body.getfirst ();} Show yourself public void Drawme (Graphics g) {if (bodycolor==null) {G.setcolor (new Color (0X3333FF));} else {G.setcolor ( BodyColor);} for (point P:body) {g.fillroundrect (p.x*global.cell_size, P.y*global.cell_size,global.cell_size, Global.CELL_SIZE, 15,12);} Drawhead (g);} Draw the Serpent Head 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, 15,12);} The thread inner class that controls the movement of the snake is private classes Snakedriver implements Runnable {public void run () {and (life) {if (Pause==false) {move (); (Snakelistener l:listeners) l.snakemoved (snake.this);} try {thread.sleep (sleeptime);} catch (Interruptedexception e) {e.printstacktrace ();}}}} method to start the thread public void begin () {new Snakedriver ()). Start (); Add Listener public void Addsnakelistener (Snakelistener l) {if (l! = null) {Listeners.add (L);}} Accelerated public void SpeedUp () {if (sleeptime>50) {sleeptime-=20;}} Deceleration public void Speeddown () {if (sleeptime<700) {sleeptime+=20;}}}

Food Category:

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 Food color;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];p rivate int maptype = 1;public int Getmaptype () {return maptype;} public void Setmaptype (int maptype) {this.maptype = Maptype;} Initialize Ground (empty stone) public void Clear () {for (int x = 0; x < global.width; + +) for (int y = 0; y < global.height; y++) Rock S[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;} Whether the snake eats 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);}}}} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java version of the snake (more complete version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.