|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
[ 作者: 加入时间:2006-11-12 11:30:14 来自:Linux联盟收集
] | |
|
import java.awt.*;HSVLinux联盟 import java.awt.event.*;HSVLinux联盟 import javax.swing.*;HSVLinux联盟 import java.util.*;HSVLinux联盟 HSVLinux联盟 public class GreedSnake implements KeyListener{HSVLinux联盟 JFrame mainFrame;HSVLinux联盟 Canvas paintCanvas;HSVLinux联盟 JLabel labelScore;HSVLinux联盟 SnakeModel snakeModel = null;HSVLinux联盟 HSVLinux联盟 public static final int canvasWidth = 200;HSVLinux联盟 public static final int canvasHeight = 300;HSVLinux联盟 HSVLinux联盟 public static final int nodeWidth = 10;HSVLinux联盟 public static final int nodeHeight = 10;HSVLinux联盟 HSVLinux联盟 public GreedSnake() {HSVLinux联盟 mainFrame = new JFrame("GreedSnake");HSVLinux联盟 HSVLinux联盟 Container cp = mainFrame.getContentPane();HSVLinux联盟 HSVLinux联盟 labelScore = new JLabel("Score:");HSVLinux联盟 cp.add(labelScore, BorderLayout.NORTH);HSVLinux联盟 HSVLinux联盟 paintCanvas = new Canvas();HSVLinux联盟 paintCanvas.setSize(canvasWidth+1,canvasHeight+1);HSVLinux联盟 paintCanvas.addKeyListener(this);HSVLinux联盟 cp.add(paintCanvas, BorderLayout.CENTER);HSVLinux联盟 HSVLinux联盟 JPanel panelButtom = new JPanel();HSVLinux联盟 panelButtom.setLayout(new BorderLayout());HSVLinux联盟 JLabel labelHelp;HSVLinux联盟 labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);HSVLinux联盟 panelButtom.add(labelHelp, BorderLayout.NORTH);HSVLinux联盟 labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);HSVLinux联盟 panelButtom.add(labelHelp, BorderLayout.CENTER);HSVLinux联盟 labelHelp = new JLabel("SPACE or P for pause",JLabel.CENTER);HSVLinux联盟 panelButtom.add(labelHelp, BorderLayout.SOUTH);HSVLinux联盟 cp.add(panelButtom,BorderLayout.SOUTH);HSVLinux联盟 HSVLinux联盟 mainFrame.addKeyListener(this);HSVLinux联盟 mainFrame.pack();HSVLinux联盟 mainFrame.setResizable(false);HSVLinux联盟 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);HSVLinux联盟 mainFrame.setVisible(true);HSVLinux联盟 begin();HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public void keyPressed(KeyEvent e){HSVLinux联盟 int keyCode = e.getKeyCode();HSVLinux联盟 if (snakeModel.running)HSVLinux联盟 switch(keyCode){HSVLinux联盟 case KeyEvent.VK_UP:HSVLinux联盟 snakeModel.changeDirection(SnakeModel.UP);HSVLinux联盟 break;HSVLinux联盟 case KeyEvent.VK_DOWN:HSVLinux联盟 snakeModel.changeDirection(SnakeModel.DOWN);HSVLinux联盟 break;HSVLinux联盟 case KeyEvent.VK_LEFT:HSVLinux联盟 snakeModel.changeDirection(SnakeModel.LEFT);HSVLinux联盟 break;HSVLinux联盟 case KeyEvent.VK_RIGHT:HSVLinux联盟 snakeModel.changeDirection(SnakeModel.RIGHT);HSVLinux联盟 break;HSVLinux联盟 case KeyEvent.VK_ADD:HSVLinux联盟 case KeyEvent.VK_PAGE_UP:HSVLinux联盟 snakeModel.speedUp();HSVLinux联盟 break;HSVLinux联盟 case KeyEvent.VK_SUBTRACT:HSVLinux联盟 case KeyEvent.VK_PAGE_DOWN:HSVLinux联盟 snakeModel.speedDown();HSVLinux联盟 break;HSVLinux联盟 case KeyEvent.VK_SPACE:HSVLinux联盟 case KeyEvent.VK_P:HSVLinux联盟 snakeModel.changePauseState();HSVLinux联盟 break;HSVLinux联盟 default:HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 if (keyCode == KeyEvent.VK_R ||HSVLinux联盟 keyCode == KeyEvent.VK_S ||HSVLinux联盟 keyCode == KeyEvent.VK_ENTER){HSVLinux联盟 snakeModel.running = false;HSVLinux联盟 begin();HSVLinux联盟 }HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public void keyReleased(KeyEvent e){HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public void keyTyped(KeyEvent e){HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 HSVLinux联盟 void repaint(){HSVLinux联盟 Graphics g = paintCanvas.getGraphics();HSVLinux联盟 HSVLinux联盟 //draw backgroundHSVLinux联盟 g.setColor(Color.WHITE);HSVLinux联盟 g.fillRect(0,0,canvasWidth,canvasHeight);HSVLinux联盟 HSVLinux联盟 // draw the snakeHSVLinux联盟 g.setColor(Color.BLACK);HSVLinux联盟 LinkedList na = snakeModel.nodeArray;HSVLinux联盟 Iterator it = na.iterator();HSVLinux联盟 while(it.hasNext()){HSVLinux联盟 Node n = (Node)it.next();HSVLinux联盟 drawNode(g,n);HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 // draw the foodHSVLinux联盟 g.setColor(Color.RED);HSVLinux联盟 Node n = snakeModel.food;HSVLinux联盟 drawNode(g,n);HSVLinux联盟 HSVLinux联盟 updateScore();HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 private void drawNode(Graphics g, Node n){HSVLinux联盟 g.fillRect(n.x*nodeWidth,HSVLinux联盟 n.y*nodeHeight,HSVLinux联盟 nodeWidth-1,HSVLinux联盟 nodeHeight-1);HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public void updateScore(){HSVLinux联盟 String s = "Score: " + snakeModel.score;HSVLinux联盟 labelScore.setText(s);HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 void begin(){HSVLinux联盟 if (snakeModel == null || !snakeModel.running){HSVLinux联盟 snakeModel = new SnakeModel(this,HSVLinux联盟 canvasWidth/nodeWidth,HSVLinux联盟 canvasHeight/nodeHeight);HSVLinux联盟 (new Thread(snakeModel)).start();HSVLinux联盟 }HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public static void main(String[] args){HSVLinux联盟 GreedSnake gs = new GreedSnake();HSVLinux联盟 }HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 ///////////////////////////////////////////////////HSVLinux联盟 // 文件2HSVLinux联盟 ///////////////////////////////////////////////////HSVLinux联盟 HSVLinux联盟 import java.util.*;HSVLinux联盟 import javax.swing.*;HSVLinux联盟 HSVLinux联盟 class SnakeModel implements Runnable{HSVLinux联盟 GreedSnake gs;HSVLinux联盟 boolean[][] matrix;HSVLinux联盟 LinkedList nodeArray = new LinkedList();HSVLinux联盟 Node food;HSVLinux联盟 int maxX;HSVLinux联盟 int maxY;HSVLinux联盟 int direction = 2;HSVLinux联盟 boolean running = false;HSVLinux联盟 HSVLinux联盟 int timeInterval = 200;HSVLinux联盟 double speedChangeRate = 0.75;HSVLinux联盟 boolean paused = false;HSVLinux联盟 HSVLinux联盟 int score = 0;HSVLinux联盟 int countMove = 0;HSVLinux联盟 HSVLinux联盟 // UP and DOWN should be evenHSVLinux联盟 // RIGHT and LEFT should be oddHSVLinux联盟 public static final int UP = 2;HSVLinux联盟 public static final int DOWN = 4;HSVLinux联盟 public static final int LEFT = 1;HSVLinux联盟 public static final int RIGHT = 3;HSVLinux联盟 HSVLinux联盟 public SnakeModel(GreedSnake gs, int maxX, int maxY){HSVLinux联盟 this.gs = gs;HSVLinux联盟 this.maxX = maxX;HSVLinux联盟 this.maxY = maxY;HSVLinux联盟 HSVLinux联盟 // initial matirxHSVLinux联盟 matrix = new boolean[maxX][];HSVLinux联盟 for(int i=0; i<maxX; ++i){HSVLinux联盟 matrix[i] = new boolean[maxY];HSVLinux联盟 Arrays.fill(matrix[i],false);HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 // initial the snakeHSVLinux联盟 int initArrayLength = maxX > 20 ? 10 : maxX/2;HSVLinux联盟 for(int i = 0; i < initArrayLength; ++i){HSVLinux联盟 int x = maxX/2+i;HSVLinux联盟 int y = maxY/2;HSVLinux联盟 nodeArray.addLast(new Node(x, y));HSVLinux联盟 matrix[x][y] = true;HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 food = createFood();HSVLinux联盟 matrix[food.x][food.y] = true;HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public void changeDirection(int newDirection){HSVLinux联盟 if (direction % 2 != newDirection % 2){HSVLinux联盟 direction = newDirection;HSVLinux联盟 }HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 public boolean moveOn(){HSVLinux联盟 Node n = (Node)nodeArray.getFirst();HSVLinux联盟 int x = n.x;HSVLinux联盟 int y = n.y;HSVLinux联盟 HSVLinux联盟 switch(direction){HSVLinux联盟 case UP:HSVLinux联盟 y--;HSVLinux联盟 break;HSVLinux联盟 case DOWN:HSVLinux联盟 y++;HSVLinux联盟 break;HSVLinux联盟 case LEFT:HSVLinux联盟 x--;HSVLinux联盟 break;HSVLinux联盟 case RIGHT:HSVLinux联盟 x++;HSVLinux联盟 break;HSVLinux联盟 }HSVLinux联盟 HSVLinux联盟 if ((0 <= x && x < maxX) && (0 <= y && y < maxY)){HSVLinux联盟 if (matrix[x][y]){HSVLinux联盟 if(x == food.x && y == food.y){HSVLinux联盟 nodeArray.addFirst(food);HSVLinux联盟 HSVLinux联盟 int scoreGet = (10000 - 200 * countMove) / timeInterval;HSVLinux联盟 score += scoreGet > 0? scoreGet : 10;HSVLinux联盟 countMove = 0;HSVLinux联盟 HSVLinux联盟 food = createFood();HSVLinux联盟 matrix[food.x][food.y] = true;HSVLinux联盟 return true;HSVLinux联盟 }HSVLinux联盟 elseHSVLinux联盟 return false;HSVLinux联盟 }HSVLinux联盟 else{HSVLinux联盟 nodeArray.addFirst(new Node(x,y));HSVLinux联盟 matrix[x][y] = true;HSVLinux联盟 n = (Node)nodeArray.removeLast();HSVLinux联盟 matrix[n.x][n.y] = false;HSVLinux联盟 countMove++;HSVLinux联盟 return true;HSVLinux联盟 HSVLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|