Package CN. itcast. Snake. Controller;
Import java. AWT. event. keyadapter;
Import java. AWT. event. keyevent;
Import CN. itcast. Snake. Entities. Food;
Import CN. itcast. Snake. Entities. gamepanel;
Import CN. itcast. Snake. Entities. ground;
Import CN. itcast. Snake. Entities. Snake;
Import CN. itcast. Snake. listener. snkelistener;
Public class controller extends keyadapter implements snkelistener {
Private Snake snake;
Private food;
Private ground;
Private gamepanel;
Public controller (Snake snake, Food food, ground, gamepanel ){
Super ();
This. Snake = snake;
This. Food = food;
This. Ground = ground;
This. gamepanel = gamepanel;
}
Public void keypressed (keyevent e ){
Switch (E. getkeycode ()){
Case keyevent. vk_up:
Snake. changedirection (snake. Up );
Break;
Case keyevent. vk_down:
Snake. changedirection (snake. Down );
Break;
Case keyevent. vk_left:
Snake. changedirection (snake. Left );
Break;
Case keyevent. vk_right:
Snake. changedirection (snake. Right );
Break;
}
}
Public void snake (Snake snake ){
// Todo automatically generates method stubs
If (food. issnkeeatfood (snake )){
Snake. eatfood ();
Food. newfood (ground. getpoint ());
}
If (ground. issnkeeatrock (snake )){
Snake. Die ();
}
If (snake. iseatbody ()){
Snake. Die ();
}
Gamepanel. Display (snake, food, ground );
}
Public void newgame (){
Snake. Start ();
Food. newfood (ground. getpoint ());
}
}
Package CN. itcast. Snake. entities;
Import java. AWT. graphics;
Import java. AWT. Point;
Import CN. itcast. Snake. util. Global;
Public class food extends point {
Public void newfood (point P ){
This. setlocation (P );
}
Public Boolean issnkeeatfood (Snake snake ){
System. Out. println ("issnkeeatfood ");
Return this. Equals (snake. gethead ());
}
Public void drawme (Graphics g ){
System. Out. println ("fooddrawme ");
G. fill3drect (x * Global. cell_size, y * Global. cell_size,
Global. cell_size, global. cell_size, true );
}
}
Package CN. itcast. Snake. entities;
Import java. AWT. color;
Import java. AWT. graphics;
Import javax. Swing. jpanel;
Import CN. itcast. Snake. util. Global;
Public class gamepanel extends jpanel {
Private Snake snake;
Private ground;
Private food;
Public void display (Snake snake, Food food, ground ){
System. Out. println ("gamepanel display ");
This. Snake = snake;
This. Food = food;
This. Ground = ground;
This. repaint ();
}
@ Override
Protected void paintcomponent (Graphics g ){
G. setcolor (color. Gray );
G. fillrect (0, 0, global. Width * Global. cell_size, global. Height
* Global. cell_size );
If (ground! = NULL & snake! = NULL & food! = NULL ){
This. Snake. drawme (g );
This. Food. drawme (g );
This. Ground. drawme (g );
}
}
}
,
Package CN. itcast. Snake. entities;
Import java. AWT. color;
Import java. AWT. graphics;
Import java. AWT. Point;
Import java. util. Random;
Import CN. itcast. Snake. util. Global;
Public class ground {
Private int [] [] rocks = new int [Global. Width] [global. Height];
Public ground (){
For (INT x = 0; x <global. width; X ++ ){
Rocks [x] [0] = 1;
Rocks [x] [global. Height-1] = 1;
}
}
Public Boolean issnkeeatrock (Snake snake ){
System. Out. println ("ground issnkeeatrock ");
For (INT x = 0; x <global. width; X ++ ){
For (INT y = 0; y <global. height; y ++ ){
If (rocks [x] [Y] = 1
& (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] = 1 );
Return new point (x, y );
}
Public void drawme (Graphics g ){
System. Out. println ("ground drawme ");
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] = 1 ){
G. fill3drect (x * Global. cell_size, y * Global. cell_size,
Global. cell_size, global. cell_size, true );
}
}
}
}
}
Package CN. itcast. Snake. entities;
Import java. AWT. color;
Import java. AWT. graphics;
Import java. AWT. Point;
Import java. util. hashset;
Import java. util. Collections list;
Import java. util. Set;
Import CN. itcast. Snake. listener. snkelistener;
Import CN. itcast. Snake. util. Global;
Public class snake {
Public static final int up =-1;
Public static final int down = 1;
Public static final int left = 2;
Public static final int right =-2;
Private int olddirection, newdirection;
Private point oldtail;
Private partition list <point> body = new partition list <point> ();
Private set <snkelistener> listeners = new hashset <snkelistener> ();
Private Boolean life;
Public snake (){
Init ();
}
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;
Life = true;
}
Public void die (){
Life = false;
}
Public void move (){
System. Out. println ("move ");
If (! (Olddirection + newdirection = 0 )){
Olddirection = newdirection;
}
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 );
Body. addfirst (newhead );
}
Public void changedirection (INT direction ){
System. Out. println ("changedirection ");
Newdirection = direction;
}
Public void eatfood (){
System. Out. println ("");
Body. addlast (oldtail );
}
Public Boolean iseatbody (){
System. Out. println ("");
For (INT I = 1; I <body. Size (); I ++ ){
If (body. Get (I). Equals (this. gethead ())){
Return true;
}
}
Return false;
}
Public void drawme (Graphics g ){
System. Out. println ("");
G. setcolor (new color (0 xcfcfcf ));
For (point P: Body ){
G. fill3drect (p. x * Global. cell_size, P. y * Global. cell_size,
Global. cell_size, global. cell_size, true );
}
}
Public point gethead (){
Return body. getfirst ();
}
Private class snkedriver implements runnable {
Public void run (){
While (LIFE ){
Move ();
For (snkelistener L: listeners ){
L. snkemoved (snake. This );
}
Try {
Thread. Sleep (300 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
}
Public void start (){
New thread (New snakedriver (). Start ();
}
Public void addsnakelistener (snkelistener L ){
If (L! = NULL ){
This. listeners. Add (L );
}
}
}
Package CN. itcast. Snake. game;
Import java. AWT. borderlayout;
Import javax. Swing. jframe;
Import CN. itcast. Snake. Controller. Controller;
Import CN. itcast. Snake. Entities. Food;
Import CN. itcast. Snake. Entities. gamepanel;
Import CN. itcast. Snake. Entities. ground;
Import CN. itcast. Snake. Entities. Snake;
Import CN. itcast. Snake. util. Global;
Public class game {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo automatically generates method stubs
Snake snake = new snake ();
Food food = new food ();
Ground ground = new ground ();
Gamepanel = new gamepanel ();
Controller controller = new controller (snake, food, ground, gamepanel );
Javax. Swing. jframe frame = new jframe ();
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Gamepanel. setsize (Global. Width * Global. cell_size, global. Height
* Global. cell_size );
Frame. setsize (Global. Width * Global. cell_size + 10, global. Height
* Global. cell_size + 35 );
Frame. Add (gamepanel, borderlayout. center );
Gamepanel. addkeylistener (Controller );
Snake. addsnakelistener (Controller );
Frame. addkeylistener (Controller );
Frame. setvisible (true );
Controller. newgame ();
}
}
Package CN. itcast. Snake. listener;
Import CN. itcast. Snake. Entities. Snake;
Public interface snkelistener {
Void snkemoved (Snake snake );
}
Package CN. itcast. Snake. util;
Public class global {
Public static final int cell_size = 20;
Public static final int width = 15;
Public static final int Height = 15;
}