The java Snake game box builds a class of snake, food, surrounding stone, panel, control center, listener, and component based on the object-oriented idea. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/134R06311-0.jpg "/> Food. javapublic class Food {
// How does a snake get food?
Public boolean issnkeeatfood (){
System. out. println ("does a snake eat food ");
Return false;
}
// How to draw food
Public void drawFood (){
System. out. println ("draw food ");
}
} Ground. javapublic class Ground {
// Whether the snake has taken the stone
Public boolean issnkeeatrrock (){
System. out. println ("has the snake eaten stone ");
Return false;
}
// Method for drawing surrounding stones
Public void drawRock (){
System. out. println ("Drawing surrounding stones ");
}
} Snake. javapublic class Snake {
// Listen
Private Set <snkelistener> listeners = new HashSet <snkelistener> ();
// Method of snake Movement
Public void move (){
System. out. println ("Snake mobile ");
}
// Method for changing the direction of a snake
Public void changeDirecion (){
System. out. println ("Snake changing direction ");
}
// How does a snake eat food?
Public void eatFood (){
System. out. println ("snake food ");
}
// How can a snake eat it?
Public boolean isEatBody (){
System. out. println ("Whether the snake eats itself ");
Return false;
}
// Method to draw a snake
Public void drawSnake (){
System. out. println ("draw a snake ");
}
// The Thread class that the snake keeps moving after the game starts
Class SnakeDriver implements Runnable {
Public void run (){
While (true ){
Move ();
// Traverse the listener Group
For (snkelistener l: listeners ){
L. snkemoved (Snake. this );
}
Try {
// Move every second
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}
// Thread start Method
Public void start (){
New Thread (new SnakeDriver (). start ();
}
// Method for adding a listener
Public void addSnakelistener (snkelistener l ){
If (l! = Null ){
This. listeners. add (l );
}
}
} GamePanel. java
Public class GamePanel extends JPanel {
Private Snake snake;
Private Food food;
Private Ground;
// Display the image drawn
Public void display (Snake snake, Food food, Ground ground ){
System. out. println ("display drawn image ");
This. snake = snake;
This. food = food;
This. ground = ground;
This. repaint ();
}
// Method for re-displaying the image
Protected void paintComponent (Graphics g ){
// TODO Auto-generated method stub
This. ground. drawRock ();
This. food. drawFood ();
This. snake. drawSnake ();
}
} Snkelistener. java listens to the public interface of the snake mobile interface {
Void snkemoved (Snake snake );
} Controller. java Control Center handles business logic, handles Keyboard Events, and implements the listener interface public class Controller extends KeyAdapter implements snkelistener {
Private Snake snake;
Private Food food;
Private Ground;
Private GamePanel gamepanel;
Public Controller (Snake snake, Food food, Ground ground, GamePanel gamepanel ){
Super ();
This. snake = snake;
This. food = food;
This. ground = ground;
This. gamepanel = gamepanel;
}
// Handle Keyboard Events
@ Override
Public void keyPressed (KeyEvent e ){
// TODO Auto-generated method stub
Switch (e. getKeyCode ()){
Case KeyEvent. VK_UP:
Snake. changeDirecion ();
Break;
Case KeyEvent. VK_DOWN:
Snake. changeDirecion ();
Break;
Case KeyEvent. VK_LEFT:
Snake. changeDirecion ();
Break;
Case KeyEvent. VK_RIGHT:
Snake. changeDirecion ();
Break;
}
}
// Implement the listener Interface
Public void Snake (snake Snake ){
// TODO Auto-generated method stub
Gamepanel. display (snake, food, ground );
}
// Start a new game Method
Public void gameStart (){
Snake. start ();
}
} Game. java build class public class Game {public static void main (String [] args ){
Snake snake = new Snake ();
Food food = new Food ();
Ground ground = new Ground ();
GamePanel gamepanel = new GamePanel ();
Controller controller = new Controller (snake, food, ground, gamepanel );
JFrame frame = new JFrame ();
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setSize (300,300 );
Frame. add (gamepanel, BorderLayout. CENTER );
Gamepanel. addKeyListener (controller );
Snake. addSnakelistener (controller );
Frame. setVisible (true );
Controller. gameStart ();
} After the framework is built, the next step is to implement these methods step by step. The final effect is as follows: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/134R02244-1.jpg "/> 650) this. width = 650;" onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/134R0O64-2.jpg "/> 650) this. width = 650;" onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 1 "src =" ee39aaa1111fee3f63a4dee75dd5233c "width =" 1 "border =" 0 "/>
This article is from the "going forward" blog and will not be reproduced!