日期:2014-05-20 浏览次数:20785 次
package game; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Iterator; import java.util.LinkedList; import javax.swing.*; import javax.swing.border.Border; public class MFrame extends JFrame implements ActionListener, KeyListener, Runnable { private static final int UP=1; private static final int DOWN=2; private static final int LEFT=3; private static final int RIGHT=4; private static final int ROWS=30; private static final int COLS=40; int score =0,speed =500; boolean ispause = false,isend = false; JButton startGame,stopGame; JButton [][] PA = new JButton[ROWS][COLS]; JLabel jl1,jl2; int direction = RIGHT,lastdirection = RIGHT; int x = 0,y = 0; LinkedList<snakeact> list = new LinkedList<snakeact>(); Thread nThread = new Thread(this); public MFrame() { setTitle("~贪吃蛇~刘松立~"); setBounds(200, 200, 435, 390); setVisible(true); startGame = new JButton("开始"); stopGame = new JButton("暂停"); jl2 = new JLabel("分数 : " + score); jl1 = new JLabel("速度 : " + speed); GridLayout grid = new GridLayout(ROWS,COLS); JPanel jPanel=new JPanel(grid); for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) { PA[i][j] = new JButton(); PA[i][j].setBackground(Color.lightGray); PA[i][j].setSize(10, 10); PA[i][j].setVisible(false); jPanel.add(PA[i][j]); } Border border = BorderFactory.createBevelBorder(0, Color.black, Color.black); jPanel.setBorder(border); startGame.addActionListener(this); stopGame.addActionListener(this); addKeyListener(this); JPanel jPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); jPanel2.add(startGame); jPanel2.add(stopGame); jPanel2.add(jl2); jPanel2.add(jl1); getContentPane().add(jPanel2,BorderLayout.NORTH); getContentPane().add(jPanel,BorderLayout.CENTER); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if(!isend&&!ispause){ if(e.getKeyCode() == KeyEvent.VK_UP){ direction = UP; } if(e.getKeyCode() == KeyEvent.VK_DOWN){ direction = DOWN; } if(e.getKeyCode() == KeyEvent.VK_LEFT){ direction = LEFT; } if(e.getKeyCode() == KeyEvent.VK_RIGHT){ direction = RIGHT; } move(); } } public void move() { int x = list.getFirst().getX(); int y = list.getFirst().getY(); switch (direction) { case UP:{ if(lastdirection == DOWN) x = x+1; else { x = x-1; lastdirection = UP; } break; } case DOWN:{ if(lastdirection == UP) x = x-1; else { x = x+1; lastdirection = DOWN; } break; } case LEFT:{ if(lastdirection == RIGHT) y = y+1; else { y = y-1; lastdirection = LEFT; } break; } case RIGHT:{ if(lastdirection == LEFT) y = y-1; else { y = y+1; lastdirection = RIGHT; } break; } default: break; } if (x>=0&&x<ROWS&&y>=0&&y<COLS) { snakeact temp = new snakeact(); temp.setX(x); temp.setY(y); list.addFirst(temp); PA[x][y].setVisible(true); if (!addsnakebody()) { PA[list.getLast().getX()][list.getLast().getY()].setVisible(false); list.removeLast(); } else { food(); score += 100; } } else { isend = true; } jl1.setText("分数 : " + score); jl2.setText("速度 : " + speed); } public boolean addsnakebody() { int x = list.getFirst().getX(); int y = list.getFirst().getY(); if (PA[x][y].getBackground().equals(Color.yellow)) { return true; } return false; } public void food() { int x = (int) (Math.random() * ROWS); int y = (int) (Math.random() * COLS); while(PA[x][y].isVisible()){ x = (int) (Math.random() * ROWS); y = (int) (Math.random() * COLS); } PA[x][y].setBackground(Color.yellow); PA[x][y].setVisible(true); } @Override public void keyReleased(KeyEvent e) {} @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == startGame){ start(); } if(e.getSource() == stopGame ){ stop(); } } public void start(){ for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) { PA[i][j].setBackground(Color.lightGray); PA[i][j].setVisible(false); } startGame.setEnabled(true); list.removeAll(list); for (int i = 0; i < 3; i++) { snakeact tempSnakeact = new snakeact(); tempSnakeact.setX(9); tempSnakeact.setY(10-i); list.add(tempSnakeact); } Iterator<snakeact> iter = list.iterator(); while(iter.hasNext()){ snakeact te = iter.next(); PA[te.getX()][te.getY()].setVisible(true); } x = (int) (Math.random() * ROWS); y = (int) (Math.random() * COLS); while(PA[x][y].isVisible()){ x = (int) (Math.random() * ROWS); y = (int) (Math.random() * COLS); } PA[x][y].setBackground(Color.yellow); PA[x][y].setVisible(true); requestFocus(); nThread.start(); } public void stop(){ if(ispause){ ispause = false; stopGame.setText("暂停"); } else { ispause = true; stopGame.setText("继续"); } requestFocus(); } /** * @param args */ public static void main(String[] args) { new MFrame(); } @Override public void run() { while (true) { try { Thread.sleep(speed); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!isend&&!ispause) { move(); } } } } package game; public class snakeact { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } }