Node.java
package gyt;
class Node
{
public static final int W = 20 ;
public static final int H = 20 ;
int x;
int y;
public Node ( int x, int y)
{
this . x = x;
this . y = y;
}
}
Snake.java
package gyt;
import java. util. ArrayList;
class Snake
{
boolean isRun;
ArrayList< Node> body;
Node food;
int direction;
int score;
int status;
int speed;
public static final int SLOW = 500 ;
public static final int MID = 300 ;
public static final int FAST = 100 ;
public static final int RUNNING = 1 ;
public static final int PAUSED = 2 ;
public static final int GAMEOVER = 3 ;
public static final int LEFT = 1 ;
public static final int UP = 2 ;
public static final int RIGHT = 3 ;
public static final int DOWN = 4 ;
public Snake ( )
{
speed = Snake. SLOW;
score = 0 ;
isRun = false ;
status = Snake. PAUSED;
direction = Snake. RIGHT;
body = new ArrayList < Node> ( ) ;
body. add ( new Node ( 60 , 20 ) ) ;
body. add ( new Node ( 40 , 20 ) ) ;
body. add ( new Node ( 20 , 20 ) ) ;
makeFood ( ) ;
}
private boolean isEaten ( )
{
Node head = body. get ( 0 ) ;
if ( direction == Snake. RIGHT && ( head. x + Node. W) == food. x
&& head. y == food. y)
return true ;
else if ( direction == Snake. LEFT && ( head. x - Node. W) == food. x
&& head. y == food. y)
return true ;
else if ( direction == Snake. UP && ( head. y - Node. H) == food. y
&& head. x == food. x)
return true ;
else if ( direction == Snake. DOWN && ( head. y + Node. H) == food. y
&& head. x == food. x)
return true ;
else
return false ;
}
public boolean isCollsion ( )
{
Node head = body. get ( 0 ) ;
if ( direction == Snake. RIGHT && head. x == 280 )
return true ;
else if ( direction == Snake. UP && head. y == 0 )
return true ;
else if ( direction == Snake. LEFT && head. x == 0 )
return true ;
else if ( direction == Snake. DOWN && head. y == 380 )
return true ;
Node temp = null;
int i;
for ( i = 3 ; i < body. size ( ) ; i++ )
{
temp = body. get ( i) ;
if ( temp. x == head. x && temp. y == head. y)
return true ;
}
if ( i < body. size ( ) )
return true ;
return false ;
}
public void makeFood ( )
{
boolean isInBody = true ;
int ax = 0 , ay = 0 ;
int X = 0 , Y = 0 ;
while ( isInBody)
{
isInBody = false ;
ax = ( int ) ( Math. random ( ) * 15 ) ;
ay = ( int ) ( Math. random ( ) * 20 ) ;
X = ax* Node. W;
Y = ax* Node. H;
for ( Node temp : body)
{
if ( X== temp. x && Y == temp. y)
isInBody = true ;
}
}
food = new Node ( X, Y) ;
}
public void changeDerection ( int newDer)
{
if ( direction % 2 != newDer % 2 )
{
direction = newDer;
}
}
public void move ( )
{
if ( isEaten ( ) )
{
body. add ( 0 , food) ;
score += 10 ;
makeFood ( ) ;
}
else if ( isCollsion ( ) )
{
isRun = false ;
status = Snake. GAMEOVER;
}
else if ( isRun)
{
Node head = body. get ( 0 ) ;
int X = head. x;
int Y = head. y;
switch ( direction)
{
case RIGHT:
X += Node. W;
break ;
case LEFT:
X -= Node. W;
break ;
case UP:
Y -= Node. H;
break ;
case DOWN:
Y += Node. H;
break ;
}
body. add ( 0 , new Node ( X, Y) ) ;
body. remove ( body. size ( ) - 1 ) ;
}
}
}
SnakeFrame.java
package gyt;
import java. awt. Color;
import java. awt. Component;
import java. awt. event. ActionListener;
import java. awt. event. KeyAdapter;
import java. awt. event. ActionEvent;
import java. awt. event. KeyEvent;
import java. util. ArrayList;
import javax. swing. BorderFactory;
import javax. swing. JFrame;
import javax. swing. JLabel;
import javax. swing. JMenu;
import javax. swing. JMenuBar;
import javax. swing. JMenuItem;
import javax. swing. JPanel;
public class SnakeFrame extends JFrame
{
private JLabel statusLabel;
private JLabel speedLabel;
private JLabel scoreLabel;
private JPanel snakePanel;
private Snake snake;
private JMenuBar bar;
private JMenu gameMenu;
private JMenu helpMenu;
private JMenu speedMenu;
private JMenuItem newItem;
private JMenuItem pauseItem;
private JMenuItem beginItem;
private JMenuItem aboutItem;
private JMenuItem slowItem;
private JMenuItem midItem;
private JMenuItem fastItem;
public SnakeFrame ( )
{
init ( ) ;
ActionListener actionListener = event - >
{
if ( event. getSource ( ) == pauseItem)
snake. isRun = false ;
else if ( event. getSource ( ) == beginItem)
snake. isRun = true ;
else if ( event. getSource ( ) == newItem)
newGame ( ) ;
else if ( event. getSource ( ) == slowItem)
{
snake. speed = Snake. SLOW;
speedLabel. setText ( "slow" ) ;
}
else if ( event. getSource ( ) == midItem)
{
snake. speed = Snake. MID;
speedLabel. setText ( "mid" ) ;
}
else if ( event. getSource ( ) == fastItem)
{
snake. speed = Snake. FAST;
speedLabel. setText ( "fast" ) ;
}
} ;
pauseItem. addActionListener ( actionListener) ;
beginItem. addActionListener ( actionListener) ;
newItem. addActionListener ( actionListener) ;
slowItem. addActionListener ( actionListener) ;
midItem. addActionListener ( actionListener) ;
fastItem. addActionListener ( actionListener) ;
aboutItem. addActionListener ( actionListener) ;
addKeyListener ( new KeyAdapter ( )
{
public void keyPressed ( KeyEvent event)
{
switch ( event. getKeyCode ( ) )
{
case KeyEvent. VK_DOWN:
snake. changeDerection ( Snake. DOWN) ;
break ;
case KeyEvent. VK_UP:
snake. changeDerection ( Snake. UP) ;
break ;
case KeyEvent. VK_LEFT:
snake. changeDerection ( Snake. LEFT) ;
break ;
case KeyEvent. VK_RIGHT:
snake. changeDerection ( Snake. RIGHT) ;
break ;
case KeyEvent. VK_SPACE:
if ( snake. isRun == true )
{
snake. isRun = false ;
snake. status = Snake. PAUSED;
break ;
}
else
{
snake. isRun = true ;
snake. status = Snake. RUNNING;
break ;
}
}
}
} ) ;
}
private void init ( )
{
snake = new Snake ( ) ;
setSize ( 380 , 460 ) ;
setLayout ( null) ;
this . setResizable ( false ) ;
bar = new JMenuBar ( ) ;
gameMenu = new JMenu ( "Game" ) ;
gameMenu. add ( ( newItem = new JMenuItem ( "New Game" ) ) ) ;
gameMenu. add ( ( pauseItem = new JMenuItem ( "Pause" ) ) ) ;
gameMenu. add ( ( beginItem = new JMenuItem ( "continue" ) ) ) ;
helpMenu = new JMenu ( "help" ) ;
helpMenu. add ( ( aboutItem = new JMenuItem ( "about" ) ) ) ;
speedMenu = new JMenu ( "speed" ) ;
speedMenu. add ( ( slowItem = new JMenuItem ( "slow" ) ) ) ;
speedMenu. add ( ( midItem = new JMenuItem ( "mid" ) ) ) ;
speedMenu. add ( ( fastItem = new JMenuItem ( "fast" ) ) ) ;
bar. add ( gameMenu) ;
bar. add ( helpMenu) ;
bar. add ( speedMenu) ;
setJMenuBar ( bar) ;
statusLabel = new JLabel ( ) ;
speedLabel = new JLabel ( ) ;
scoreLabel = new JLabel ( ) ;
snakePanel = new JPanel ( ) ;
snakePanel. setBounds ( 0 , 0 , 300 , 400 ) ;
snakePanel. setBorder ( BorderFactory. createLineBorder ( Color. DARK_GRAY) ) ;
add ( snakePanel) ;
statusLabel. setBounds ( 310 , 25 , 60 , 20 ) ;
add ( statusLabel) ;
speedLabel. setBounds ( 310 , 75 , 60 , 20 ) ;
add ( speedLabel) ;
scoreLabel. setBounds ( 310 , 125 , 60 , 20 ) ;
add ( scoreLabel) ;
JLabel temp = new JLabel ( "状态" ) ;
temp. setBounds ( 310 , 5 , 60 , 20 ) ;
add ( temp) ;
temp = new JLabel ( "速度" ) ;
temp. setBounds ( 310 , 55 , 60 , 20 ) ;
add ( temp) ;
temp = new JLabel ( "分数" ) ;
temp. setBounds ( 310 , 105 , 60 , 20 ) ;
add ( temp) ;
}
private void newGame ( )
{
this . remove ( snakePanel) ;
this . remove ( statusLabel) ;
this . remove ( scoreLabel) ;
speedLabel. setText ( "slow" ) ;
statusLabel = new JLabel ( ) ;
scoreLabel = new JLabel ( ) ;
snakePanel = new SnakePanel ( snake) ;
snakePanel. setBounds ( 0 , 0 , 300 , 400 ) ;
snakePanel. setBorder ( BorderFactory. createLineBorder ( Color. DARK_GRAY) ) ;
Runnable r1 = new SnakeRunnable ( snake, snakePanel) ;
Runnable r2 = new StatusRunnable ( snake, statusLabel, scoreLabel) ;
Thread t1 = new Thread ( r1) ;
Thread t2 = new Thread ( r2) ;
t1. start ( ) ;
t2. start ( ) ;
add ( snakePanel) ;
statusLabel. setBounds ( 310 , 25 , 60 , 20 ) ;
add ( statusLabel) ;
scoreLabel. setBounds ( 310 , 125 , 60 , 20 ) ;
add ( scoreLabel) ;
}
}
SnakeGame.java
package gyt;
import java. awt. EventQueue;
import javax. swing. JFrame;
public class SnakeGame
{
public static void main ( String[ ] args)
{
EventQueue. invokeLater ( ( ) - >
{
SnakeFrame frame = new SnakeFrame ( ) ;
frame. setTitle ( "̰贪吃蛇" ) ;
frame. setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
frame. setVisible ( true ) ;
} ) ;
}
}
SnakePanel.java
package gyt;
import java. awt. EventQueue;
import javax. swing. JFrame;
public class SnakeGame
{
public static void main ( String[ ] args)
{
EventQueue. invokeLater ( ( ) - >
{
SnakeFrame frame = new SnakeFrame ( ) ;
frame. setTitle ( "̰贪吃蛇" ) ;
frame. setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
frame. setVisible ( true ) ;
} ) ;
}
}
SnakeRunnable.java
package gyt;
import java. awt. EventQueue;
import javax. swing. JFrame;
public class SnakeGame
{
public static void main ( String[ ] args)
{
EventQueue. invokeLater ( ( ) - >
{
SnakeFrame frame = new SnakeFrame ( ) ;
frame. setTitle ( "̰贪吃蛇" ) ;
frame. setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
frame. setVisible ( true ) ;
} ) ;
}
}
StatusRunnable.java
package gyt;
import javax. swing. JLabel;
class StatusRunnable implements Runnable
{
private JLabel statusLabel;
private JLabel scoreLabel;
private Snake snake;
public StatusRunnable ( Snake snake, JLabel statusLabel, JLabel scoreLabel)
{
this . statusLabel = statusLabel;
this . scoreLabel = scoreLabel;
this . snake = snake;
}
public void run ( )
{
String sta = "" ;
String spe = "" ;
while ( true )
{
switch ( snake. status)
{
case Snake. RUNNING:
sta = "running" ;
break ;
case Snake. PAUSED:
sta = "Paused" ;
break ;
case Snake. GAMEOVER:
sta = "GameOver" ;
break ;
}
statusLabel. setText ( sta) ;
scoreLabel. setText ( "" + snake. score) ;
try {
Thread. sleep ( 100 ) ;
} catch ( Exception e) { }
}
}
}