Ê×Ò³ Ê«´Ê ×Öµä °å±¨ ¾ä×Ó ÃûÑÔ ÓÑ´ð ÀøÖ¾ ѧУ ÍøÕ¾µØͼ
µ±Ç°Î»ÖÃ: Ê×Ò³ > ½Ì³ÌƵµÀ > JAVA > Eclipse¿ª·¢ >

Ϊʲô˵СµÜÎÒSnakeÎÞ·¨½âÎö

2013-10-31 
Ϊʲô˵ÎÒSnakeÎÞ·¨½âÎö£¿import javax.swing.*import java.awt.*import java.awt.event.*import java.

Ϊʲô˵ÎÒSnakeÎÞ·¨½âÎö£¿

import javax.swing.*;



import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Game {
public static void main(String args[]) {
GameSnake gs = new GameSnake();

}
}

class  GameSnake  implements KeyListener {
         
         JLabel jlabel;
         JPanel jpanel;
         JFrame jframe; 
        
       
static int panelWidth = 294;

static int panelHeight = 450;

int rectX = 15;

int rectY = 15;

Snake snake; 

Node n;

public GameSnake() {
               jframe=new JFrame("Ì°³ÔÉßDEMO!");
               jframe.setLayout(new BorderLayout());
               Container cp=jframe.getContentPane();
               jlabel=new JLabel("welcome");
               jlabel.setText("Welcome my friend! Enjoy your self!");
  
               cp.add(jlabel,BorderLayout.NORTH);
               jpanel=new JPanel();
               jpanel.setLayout(new BorderLayout());     
               cp.add(jpanel,BorderLayout.CENTER);
               


                jframe.pack();
snake = new Snake(this, panelWidth / rectX, panelHeight / rectY);
setBackground(Color.WHITE);
setSize(panelWidth, panelHeight);
setFocusable(true);
addKeyListener(this);
                
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
LinkedList list = Snake.snakeList;
Iterator it = list.iterator();
g2.setColor(Color.black);
while (it.hasNext()) {
n = (Node) it.next();
drawNode(g2, n);
}
g2.setColor(Color.ORANGE);
Node f = snake.food;
drawNode(g2, f);
snake.drawMap(g2);// »æÖƵØͼ
}

public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_ENTER) {
begin();
} else if (keycode == KeyEvent.VK_UP) {
snake.changeDirection(Snake.up);
} else if (keycode == KeyEvent.VK_DOWN) {
snake.changeDirection(Snake.down);
} else if (keycode == KeyEvent.VK_LEFT) {
snake.changeDirection(Snake.left);
} else if (keycode == KeyEvent.VK_RIGHT) {
snake.changeDirection(Snake.right);
} else if (keycode == KeyEvent.VK_S) {
Snake.run = false;
}
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

public void drawNode(Graphics2D g, Node n) {
g.fillRect(n.x * rectX, n.y * rectY, rectX - 2, rectY - 2);
}

public void begin() {
Snake.run = true;
SnakeThread thread = new SnakeThread(snake);
thread.start();
}

        public void repaint(){
           int score=snake.getScore();
           jlabel.setText("ÄúµÄµÃ·ÖÊÇ£º"+score);
  
         }


}

class Node {
int x;

int y;

public Node(int x, int y) {
this.x = x;
this.y = y;
}
}

class SnakeThread extends Thread {
Snake snake;

public SnakeThread(Snake s) {
snake = s;
}

public void run() {
                
        int interval=200;
        


        boolean pause=false;

        double speedRate=0.5;

Snake.run = true;

GameSnake gs;

while (Snake.run) {
try {
snake.move();
sleep(interval);

                 catch (InterruptedException e) {
                           e.printStackTrace();
}
                 if(!pause){
                     if(move()){
                      gs.repaint();
                     }
                else{
                JOptionPane.showMessageDialog(null, "sorry myboy,GAME OVER!", "message", JOptionPane.INFORMATION_MESSAGE);
                break;
               } 
             }
        }
Snake.run = false;
}


class Snake {
GameSnake gs;

Node food;

boolean[][] all;

public static boolean run;

int maxX;

int maxY;

public static int left = 1;

public static int up = 2;

public static int right = 3;

public static int down = 4;
          
        int interval=200;
        
        boolean pause=false;

        double speedRate=0.5;



int direction = 4;
        
        int score=0;

LinkedList snakeList = new LinkedList();

public Snake(GameSnake p, int maxX, int maxY) {
gs = p;
this.maxX = maxX;
this.maxY = maxY;
all = new boolean[maxX][maxY];
for (int i = 0; i < maxX; i++) {
for (int j = 0; j < maxY; j++) {
all[i][j] = false;
}
}
int arrayLength = maxX > 20 ? 10 : maxX / 2;
for (int i = 0; i < arrayLength; i++) {
int x = maxX / 10 + i;
int y = maxY / 10;
snakeList.addFirst(new Node(x, y));
all[x][y] = true;
}
food = createFood();
all[food.x][food.y] = true;
}

// ÉßÒƶ¯µÄ·½·¨
public void move() {
Node n = (Node) snakeList.getFirst();
int x = n.x;
int y = n.y;

if (direction == 3) {
x++;
} else if (direction == 4) {
y++;
} else if (direction == 1) {
x--;
} else if (direction == 2) {
y--;
}
// ÊµÏÖ¶ÔÉßײµ½×ÔÉíµÄ¼ì²â
if ((0 <= x && x <= GameSnake.panelWidth / 15 - 1)
&& (0 <= y && y <= GameSnake.panelHeight / 15 - 1)) {
if (all[x][y]) {
if (x == food.x && y == food.y) {
snakeList.addFirst(food);
food = createFood();
all[food.x][food.y] = true;
} else {
JOptionPane.showMessageDialog(null, "Äãײµ½×Ô¼ºÁË");
System.exit(0);
}
} else {
snakeList.addFirst(new Node(x, y));
all[x][y] = true;
Node l = (Node) snakeList.getLast();
snakeList.removeLast();
all[l.x][l.y] = false;
}
} else {
JOptionPane.showMessageDialog(null, "Ô½½çÁË,ÓÎÏ·½áÊø");
System.exit(0);
}
gs.repaint();
}

public Node createFood() {
int x = 0;
int y = 0;
do {
Random r = new Random();
x = r.nextInt(maxX - 10);
y = r.nextInt(maxY - 10);



} while (all[x][y]);
return new Node(x, y);
}
   
  //»ñÈ¡µ±Ç°ÓÎÏ·µÃ·Ö
  public int getScore(){
  return this.score;
  }
 
 //¼ÓËÙ
 public void speedUp(){
  interval*=speedRate;
 }
 //¼õËÙ
 public void speedDown(){
  interval/=speedRate;
 }
 //ÉèÖÃÔÝÍ£
 public void chagePause(){
  pause=!pause;
 }
 //ÉèÖ÷½Ïò
 public void chageDirection(int newdirection){
  if(direction % 2 != newdirection % 2){
   direction=newdirection;
  }
 }




//ÉèÖÃÉß²»ÄÜ»ØÍ·
public void changeDirection(int newDirection) {
if (direction % 2 != newDirection % 2) {
direction = newDirection;
}
}

public void drawMap(Graphics2D g) {
for (int i = 0; i < maxX; i++) {
for (int j = 0; j < maxY; j++) {
if (all[i][j] == true) {
g.setColor(Color.red);
g.fillRect(i, j, 4, 4);
}
}
}
}
}


ÈȵãÅÅÐÐ