java新手
沒法把jar文件附在文章後面
有意見盡管提
///////////////////////////
// 文件
//////////////////////////
import java
import java
import javax
import java
public class GreedSnake implements KeyListener{
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;
SnakeModel snakeModel = null;
public static final int canvasWidth =
public static final int canvasHeight =
public static final int nodeWidth =
public static final int nodeHeight =
public GreedSnake() {
mainFrame = new JFrame(
Container cp = mainFrame
labelScore = new JLabel(
cp
paintCanvas = new Canvas();
paintCanvas
paintCanvas
cp
JPanel panelButtom = new JPanel();
panelButtom
JLabel labelHelp;
labelHelp = new JLabel(
panelButtom
labelHelp = new JLabel(
panelButtom
labelHelp = new JLabel(
panelButtom
cp
mainFrame
mainFrame
mainFrame
mainFrame
mainFrame
begin();
}
public void keyPressed(KeyEvent e){
int keyCode = e
if (snakeModel
switch(keyCode){
case KeyEvent
snakeModel
break;
case KeyEvent
snakeModel
break;
case KeyEvent
snakeModel
break;
case KeyEvent
snakeModel
break;
case KeyEvent
case KeyEvent
snakeModel
break;
case KeyEvent
case KeyEvent
snakeModel
break;
case KeyEvent
case KeyEvent
snakeModel
break;
default:
}
if (keyCode == KeyEvent
keyCode == KeyEvent
keyCode == KeyEvent
snakeModel
begin();
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
void repaint(){
Graphics g = paintCanvas
//draw background
g
g
// draw the snake
g
LinkedList na = snakeModel
Iterator it = erator();
while(it
Node n = (Node)it
drawNode(g
}
// draw the food
g
Node n = snakeModel
drawNode(g
updateScore();
}
private void drawNode(Graphics g
g
n
nodeWidth
nodeHeight
}
public void updateScore(){
String s =
labelScore
}
void begin(){
if (snakeModel == null || !snakeModel
snakeModel = new SnakeModel(this
canvasWidth/nodeWidth
canvasHeight/nodeHeight);
(new Thread(snakeModel))
}
}
public static void main(String[] args){
GreedSnake gs = new GreedSnake();
}
}
///////////////////////////////////////////////////
// 文件
///////////////////////////////////////////////////
import java
import javax
class SnakeModel implements Runnable{
GreedSnake gs;
boolean[][] matrix;
LinkedList nodeArray = new LinkedList();
Node food;
int maxX;
int maxY;
int direction =
boolean running = false;
int timeInterval =
double speedChangeRate =
boolean paused = false;
int score =
int countMove =
// UP and DOWN should be even
// RIGHT and LEFT should be odd
public static final int UP =
public static final int DOWN =
public static final int LEFT =
public static final int RIGHT =
public SnakeModel(GreedSnake gs
this
this
this
// initial matirx
matrix = new boolean[maxX][];
for(int i=
matrix[i] = new boolean[maxY];
Arrays
}
// initial the snake
int initArrayLength = maxX >
for(int i =
int x = maxX/
int y = maxY/
nodeArray
matrix[x][y] = true;
}
food = createFood();
matrix[food
}
public void changeDirection(int newDirection){
if (direction %
direction = newDirection;
}
}
public boolean moveOn(){
Node n = (Node)nodeArray
int x = n
int y = n
switch(direction){
case UP:
y
break;
case DOWN:
y++;
break;
case LEFT:
x
break;
case RIGHT:
x++;
break;
}
if ((
if (matrix[x][y]){
if(x == food
nodeArray
int scoreGet = (
score += scoreGet >
countMove =
food = createFood();
matrix[food
return true;
}
else
return false;
}
else{
nodeArray
matrix[x][y] = true;
n = (Node)nodeArray
matrix[n
countMove++;
return true;
}
}
return false;
}
public void run(){
running = true;
while (running){
try{
Thread
}
catch(Exception e){
break;
}
if(!paused){
if (moveOn()){
gs
}
else{
JOptionPane
null
JOptionPane
break;
}
}
}
running = false;
}
private Node createFood(){
int x =
int y =
do{
Random r = new Random();
x = r
y = r
}while(matrix[x][y]);
return new Node(x
}
public void speedUp()
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27025.html