Java小游戏之扫雷

前几天看到室长在做c#的作业,貌似是写一个扫雷。。。于是当时就想用java也写写看。。。
发现java的可视化真不是一般的难过。。。
不过,磕磕碰碰的还是写出来了,虽然样子比较挫,但是还是可以玩的
9*9 9个雷~
下面上代码~

/* * To change this template, choose Tools | Templates * and open the template in the editor. / /* * * @author Zerob13 / import java.awt.; import java.awt.event.; import javax.swing.; class Mineb extends JButton { private boolean ismine; int x, y; int num; public boolean isMine() { return ismine; } public void setMine(boolean a) { ismine = a; } Mineb(String a, int i, int j) { super.setText(a); x = i; y = j; ismine = false; } } public class Deom extends JFrame implements MouseListener { /** * @param args */ Mineb[][] b; JLabel state; int last; JMenuBar mb; int elas; class node { int x, y; } int[][] dir = { { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 0 }, { 0, -1 } }; private void gen() { int co = 0; elas = 81 - 9; last = 9; int[] hash = new int[81]; for (int i = 0; i = 9) x = 8; if (y >= 9) y = 8; b[x][y].setMine(true); } } for (int i = 0; i = 9 || z >= 9) continue; if (b[w][z].isMine()) { k++; } } return k; } void showall() { int i, j; for (i = 0; i top) { head = queue[top]; top++; for (i = 0; i = 9 || z >= 9) continue; if(b[w][z].getText()!="") continue; if (b[w][z].isMine()) continue; if (hash[w][z]) continue; hash[w][z] = true; if (b[w][z].num 0) { queue[tail] = new node(); queue[tail].x = w; queue[tail].y = z; tail++; if (b[w][z].isEnabled()) { b[w][z].setEnabled(false); elas--; } b[w][z].setText("-"); } } } if (elas 0) { showall(); JOptionPane.showMessageDialog(null, "YOU WIN!", "YOU WIN!", JOptionPane.DEFAULTOPTION); state.setText("WIN"); } } @Override public void mouseClicked(MouseEvent e) { Mineb temp = (Mineb) e.getSource(); if (e.getButton() MouseEvent.BUTTON1) { if (temp.getText() "P" || temp.getText() "?") { } else { if (temp.isMine()) { showall(); state.setText("LOSE"); JOptionPane.showMessageDialog(null, "YOU LOSE!", "YOU LOSE!", JOptionPane.DEFAULTOPTION); } else { if (temp.num 0) { showsame(temp.x, temp.y); temp.setText("-"); } else { temp.setText(String.valueOf(temp.num)); } } if (temp.isEnabled()) { temp.setEnabled(false); elas--; } if (elas 0) { showall(); state.setText("WIN"); JOptionPane.showMessageDialog(null, "YOU WIN!", "YOU WIN!", JOptionPane.DEFAULT_OPTION); } } } else if (e.getButton() MouseEvent.BUTTON3) { if (temp.getText() "P" || this.last 0) { if (temp.getText() "P") this.last++; temp.setText("?"); } else if (temp.getText() "?") { temp.setText(""); } else { temp.setText("P"); this.last--; } state.setText(String.valueOf(this.last)); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }

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(); } } }