日期:2014-05-20 浏览次数:20715 次
//大爱伙可以先把源码copy到机器上试一下,再回贴,这是我上学时写的一直没有公开过(不过还有些bug代码也没重构,后来//没时间放下了 一直没写下去了,不怎么会玩贴多次发送吧),分不多谢谢大家的支持谢谢大家无声的掌声^_^ package game; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Insets; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.Timer; import javax.swing.border.Border; public class QuareGame extends JFrame implements ActionListener { private GamePanel gamePanel ; //菜单工具组件 JMenuBar menuBar = new JMenuBar(); //menuBar.setSize(20, 20); JMenu menuFile = new JMenu("游戏"); JMenuItem newGame = new JMenuItem("新游戏"); JMenuItem pauseGame = new JMenuItem("暂停"); JMenuItem overGame = new JMenuItem("结束"); JMenu menuHelp=new JMenu("帮助"); // JLabel labLevel=new JLabel("选择关卡:"); JTextField txtLevel=new JTextField(); public QuareGame(GamePanel gp) { setBounds(500, 100, 400, 480); setTitle("my small game"); setLayout(new BorderLayout()); JFrame.setDefaultLookAndFeelDecorated(true); //添加菜单条 setJMenuBar(createMenuBar()); this.gamePanel = gp; if(gamePanel!=null){ add(gamePanel); addKeyListener(gamePanel); } labLevel.setBounds(260, 140, 50, 30); txtLevel.setBounds(260, 180, 50, 30); // add(labLevel); // add(txtLevel); setVisible(true); // setResizable(false); } // 菜单条 public JMenuBar createMenuBar() { menuFile.add(newGame); menuFile.add(pauseGame); menuFile.add(overGame); menuBar.add(menuFile); menuBar.add(menuHelp); menuHelp.add(new JMenuItem("版本号")); //添加事件 newGame.addActionListener(this); pauseGame.addActionListener(this); overGame.addActionListener(this); return menuBar; } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); QuareGame game = new QuareGame(new GamePanel(new int[22][12] ,new int[2][2])); } @Override public void actionPerformed(ActionEvent e) { //System.out.println(e.getActionCommand()); if(e.getActionCommand(). equals("新游戏")){ System.out.println("newgame"); if(gamePanel!=null){ remove(gamePanel); removeKeyListener(gamePanel); } this.gamePanel = new GamePanel(new int[22][12] ,new int[2][2]); add(gamePanel); addKeyListener(gamePanel); }else if(e.getActionCommand().equals("暂停")){ pauseGame.setText("继续"); gamePanel.timer.stop(); }else if(e.getActionCommand().equals("继续")){ pauseGame.setText("暂停"); gamePanel.timer.start(); }else if(e.getActionCommand().equals("结束")){ if(gamePanel!=null) this.remove(gamePanel); gamePanel=null; } } } class GamePanel extends JPanel implements KeyListener { /** * */ private static final long serialVersionUID = 1L; Random random=new Random(); JLabel labLevel=new JLabel("选择关卡:"); JTextField txtLevel=new JTextField(); int size = 20;// 显示大小 // 座标 int x = 4, y; int i = 0, j = 0; Timer timer=null; int level=1; int score =0; int steep=1000;//方块下降的速度 TimerAction timerAction; //绘图颜色 Color mapColor; Color moveShapColor; int colors[][]={{255, 222, 173},{47 ,79, 79},{255, 228 ,225},{0 ,255 ,0},{0, 0 ,255},{255, 193, 37} ,{156 ,156 ,156 },{202 ,225, 255},{171, 130, 255},{224 ,102 ,255},{255 ,62 ,150},{255, 0 ,0}}; // squareType类型和 squareState状态 int squareType, squareState; int nextState=0;//每次产生一个新的会自加1并取2的余数赋给自己 int map[][];// = new int; int nextTypeAndState [][]; // 方块的几种形状态和状态 (每个方块由一个4*4的矩阵构成) // I O T Z S L J int shape[][][]; //构造函数 public GamePanel(int map[][],int nextTypeAndState[][]) { this.nextTypeAndState=nextTypeAndState; this.map=map; this.shape=this.initShap(); setBackground(new Color(250, 250, 250)); initMap(); initWall(); createdSquare(); timerAction=new TimerAction(); timer = new Timer(steep, timerAction); timer.start(); score = 0; initTypeAndState(); this.mapColor=createColor(); this.moveShapColor=createColor(); // setLayout(); // // labLevel.setBounds(260, 140, 80, 30); // txtLevel.setBounds(50, 80, 50, 80); // txtLevel.setText("111"); // txtLevel.setSize(10, 10); // add(labLevel,BorderLayout.EAST); // add(txtLevel,BorderLayout.EAST); setSize(400, 480); }