Regional Yard
Package com;
//public class Yard {
Span style="font - size: 14 px;"> Import the Java. The awt. Color;
Import the Java. The awt. The Font;
Import the Java. The awt. Frame;
Import the Java. The awt. Graphics;
Import the Java. The awt. Image;
Import the Java. The awt. Event. KeyAdapter;
Import the Java. The awt. Event. KeyEvent;
Import the Java. The awt. Event. KeyListener;
Import the Java. The awt. Event. WindowAdapter;
Import the Java. The awt. Event. WindowEvent;
Public class Yard extends Frame {
//activity area
Private static final ints ROWS=30;//the number of rows
Private static final ints COLS=30;//the number of columns
Private static final ints BLOCK_SIZE=15;//grid size
Private Font fontGameOver=new Font (" tahoma ", the Font, BOLD, 50);//font
Private int score=0;//score
Image offScreenImage=null;//create a bitmap, prevent flashing
Snake Snake=new Snake (this);//create a snake
An Egg e=new Egg ();//random create an egg;
PaintThread PaintThread=new PaintThread ();
Private Boolean gameOver=false;//whether the game end
Public static int getROWS () {
Return ROWS.
}
Public static int getCOLS () {
Return the COLS.
}
Public static int getBLOCK_XIZE () {
Return BLOCK_SIZE;
}
Public int getScore () {
Return score;
}
Public void launch () {//set the form
Enclosing setLocation (300, 300);//window location
Enclosing setSize (COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);//window size
//add close events
Enclosing addWindowListener (new WindowAdapter () {
@ Override
Public void windowClosing (WindowEvent e) {
System. The exit (0);
}
});
Enclosing setVisible (true);//window is displayed
Enclosing addKeyListener (new KeyMonitor ());//to monitor keys
New Thread (paintThread.) start ();
///start a thread
}
Public static void main (String [] args) {
//TODO Auto - generated method stub
New Yard (). The launch ();
}
Public void setScore (int score) {//set the score
This. Score=score;
}
@ Override
Public void paint (Graphics g) {//picture
//set the background color in
Color=c g.g etColor ();
G.s etColor (Color. GRAY);
G.f illRect (0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
The color of the//set the line
G.s etColor (Color. DARK_GRAY);
Draw a line//
For (int I=1; I & lt; ROWS; I++) {
G.d rawLine (0, I * BLOCK_SIZE, COLS * BLOCK_SIZE, BLOCK_SIZE * I);
}
For (int I=1; I & lt; ROWS; I++) {
G.d rawLine (I * BLOCK_SIZE, 0, BLOCK_SIZE * I, ROWS * BLOCK_SIZE);
}
G.s etColor (Color YELLOW);
G.d rawString (score: "" + score, 10, 60).//draw score
If (gameOver) {
G.s etFont (fontGameOver);
G.d rawString (" game over ", 120, 180);
PaintThread. Pause ();
}
G.s etColor (c);
The snake eat (e);
The snake. The draw (g);//draw snakes
E.d raw (g);//egg
}
@ Override
Public void update (Graphics g) {
If (offScreenImage==null) {
BLOCK_SIZE offScreenImage=this. CreateImage (COLS * and ROWS * BLOCK_SIZE);
}
Graphics gOff=offScreenImage. GetGraphics ();
Paint (gOff);
G.d rawImage (offScreenImage, 0, 0, null);
}
Public void the stop () {
GameOver=true;//the game
}
Private class PaintThread implements Runnable {
Private Boolean running=true;
Private Boolean pause=false;
Public void the run () {
While (running) {
If (pause) continue;
The else repaint ();
Try {
Thread.sleep (200);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
Public void pause () {
This. Pause=true;
}
Public void reStart () {
This. Pause=false;
The snake=new snake (Yard. This);
GameOver=false;
}
Public void gameOver () {
Running=false;
}
}
Private class KeyMonitor extends KeyAdapter {
//to monitor keys
@ Override
Public void keyPressed KeyEvent (e) {
Int key=um participant etKeyCode ();
If (key==KeyEvent. VK_F2) {
PaintThread. ReStart ();
}
The snake. KeyPressed (e);
}
}
}
Snake snake
Package com;
Public class snake {
Import the Java. The awt. Color;
Import the Java. The awt. Graphics;
Import the Java. The awt. A Rectangle;
Import the Java. The awt. Event. KeyEvent;
Public class Snake {
Private Node head=null;//head node
Private Node tail=null;//end node
Private int size=0;//node number
Private Node Node=new Node (20, 30, Direction, LEFT);//the initial node (snake)
Private Yard y;//in which within the scope of the
Public Snake (Yard) y {
The head=node;
Tail=node;
Size=1;
This. Y=y;
}
Public void addToTail () {//node is added to the tail
The Node Node=null;
The switch (tail. Dir) {
Case LEFT:
The node=new node (tail. Row, tail. Col + 1, tail. Dir);//to the new node
break;
Case RIGHT:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull