java贪吃蛇,参考网上代码

参考了网上的一些代码,写了一个小小的贪吃蛇,比较简单,但是学到很多。比如线程,比如绘图,还有对类的理解加深了。
两个文件的
第一个是snake的模型

/* * @author zerob13 * */ import java.util.; import javax.swing.; class Node{ int x,y; Node(int x,int y) { this.x=x; this.y=y; } } class Snake implements Runnable{ Greedsnake gs;//界面 boolean[][] mmap;//地图 LinkedList nodeArray =new LinkedList();//蛇身体 Node food;//食物 int maxX; int maxY;//界面大小 int dir=2;//方向 boolean run=false;//运行状态 int time=200;//速度 boolean pause=false; int score=0;//分数 int countMove=0; public static final int UP = 2; public static final int DOWN = 4; public static final int LEFT = 1; public static final int RIGHT = 3;//方向常量 /* * * @param g 界面输入 * @param maxX 最大x边长 * @param maxY 最大y边长 / public Snake(Greedsnake g,int maxX,int maxY)//初始化 { this.gs=g; this.maxX=maxX; this.maxY=maxY; mmap=new boolean[maxX][]; for(int i=0;i20?10:maxX/2; for(int i=0;i0?scoreget:10; countMove=0; food=creatfood(); mmap[food.x][food.y]=true; return true; }else { return false; } }else{//正常移动 nodeArray.addFirst(new Node(x,y));//加头 mmap[x][y]=true; n=(Node)nodeArray.removeLast();//去尾巴 mmap[n.x][n.y]=false; countMove++; return true; } } return false;//over } /* * 食物创造函数 * @return node / private Node creatfood() { // TODO Auto-generated method stub int x=0,y=0; do{ Random r=new Random(); x=r.nextInt(maxX); y=r.nextInt(maxY); }while(mmap[x][y]); return new Node(x,y); } @Override /* * 运行线程 / public void run() { // TODO Auto-generated method stub run=true; while(run){ try{ Thread.sleep(time); }catch(Exception e){ break; } if(!pause){ if(MoveOn()){ gs.repaint(); }else{ JOptionPane.showMessageDialog(null, "GAME OVER", "Game Over", JOptionPane.INFORMATION_MESSAGE); break; } } } run=false; } /* * 暂停函数 */ public void changePauseState() { pause = !pause; } }

然后是主界面的绘画和调用蛇模型

/* * * @author zerob13 * */ import java.awt.; import java.awt.event.; import javax.swing.; import java.util.*; public class Greedsnake implements KeyListener { JFrame mainFrame; Canvas paintCanvas; JLabel labelScore; Snake snake=null; public static final int canvasWidth = 200; public static final int canvasHeight = 300; public static final int nodeWidth = 10; public static final int nodeHeight = 10; public Greedsnake(){ mainFrame=new JFrame("贪吃蛇"); Container cp=mainFrame.getContentPane(); labelScore=new JLabel("积分:"); cp.add(labelScore,BorderLayout.NORTH); paintCanvas=new Canvas(); paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1); paintCanvas.addKeyListener(this); cp.add(paintCanvas, BorderLayout.CENTER); JPanel panelButtom=new JPanel(); panelButtom.setLayout(new BorderLayout()); mainFrame.addKeyListener(this); mainFrame.pack(); mainFrame.setResizable(false); mainFrame.setDefaultCloseOperation(JFrame.EXITONCLOSE); mainFrame.setVisible(true); begin(); } public static void main(String[] args) { // TODO Auto-generated method stub Greedsnake gs = new Greedsnake(); } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub int keyCode=e.getKeyCode(); if(snake.run){ switch(keyCode){ case KeyEvent.VKUP: snake.changeDirection(snake.UP); break; case KeyEvent.VKDOWN: snake.changeDirection(snake.DOWN); break; case KeyEvent.VKLEFT: snake.changeDirection(snake.LEFT); break; case KeyEvent.VKRIGHT: snake.changeDirection(snake.RIGHT); break; case KeyEvent.VKP: snake.changePauseState();// 暂停或继续 break; default: } } if (keyCode KeyEvent.VKR || keyCode KeyEvent.VKS || keyCode KeyEvent.VKENTER) { snake.run = false; begin(); } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } void repaint(){ Graphics g=paintCanvas.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0,0,canvasWidth,canvasHeight); g.setColor(Color.black); LinkedList na=snake.nodeArray; Iterator it=na.iterator(); while(it.hasNext()){ Node n=(Node)it.next(); drawNode(g,n); } g.setColor(Color.red); Node n=snake.food; drawNode(g,n); updateScore(); } private void drawNode(Graphics g,Node n){ g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1, nodeHeight - 1); } public void updateScore(){ String s = "积分: " + snake.score; labelScore.setText(s); } void begin() { if (snake null || !snake.run) { snake = new Snake(this, canvasWidth / nodeWidth, this.canvasHeight / nodeHeight); (new Thread(snake)).start(); } } }