学校要交个JAVA大作业,不含数据库的。有的话请把源代码发到我邮箱上,小妹不胜感激啊~~~~~
------解决方案--------------------
http://hi.baidu.com/freish/blog/item/21a604f7c0271b28730eec61.html
------解决方案--------------------
http://blog.csdn.net/youyigong 到这里,这里有个小项目
------解决方案--------------------
- Java code
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, 338, 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); } public int[][][] initShap(){ return new int[][][]{ { { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } }, // s { { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } }, // z { { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } }, // j { { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // o { { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // l { { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // t { { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } }; } // 初始化地图 public void initMap() { for (i = 0; i < map.length; i++) { for (j = 0; j < map[0].length; j++) { map[i][j] = 0; } } } // 初始化围墙 public void initWall() { for (i = 0; i < map.length; i++) { map[i][0] = 2; // 两边为2 (1.表示普通2 表示围墙) map[i][11] = 2; } // 底部 for (j = 0; j < 12; j++) { map[map.length - 1][j] = 2; } } // 产生新方块方法 public void createdSquare() { x = 4; y = 0; nextState=(nextState+1)%2; getTypeAndState(nextState); } //获取当前方块数据,并计算下一个方块数据 public void getTypeAndState(int index){ squareType= nextTypeAndState[0][index]; nextTypeAndState[0][index]=(int) (Math.random() * 100) % 7; squareState=nextTypeAndState[1][index]; nextTypeAndState[1][index]=(int) (Math.random() * 100) % 4; } //初始化当前方块和下一个方块 public void initTypeAndState(){ for (int i = 0; i < nextTypeAndState[0].length; i++) { nextTypeAndState[0][i]=(int) (Math.random() * 100) % 7; } for (int i = 0; i < nextTypeAndState[1].length; i++) { nextTypeAndState[1][i]=(int) (Math.random() * 100) % 4; } } //随机产生颜色方法 public Color createColor(){ int temp[]=colors[random.nextInt(colors.length)];//此处不用减1 return new Color(temp[0],temp[1],temp[2]); } // 是否超出边界 public int isOverstep(int x, int y, int squareType, int squareState) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (shape[squareType][squareState][i * 4 + j] == 1) { if (map[y + i][j + x] > 0) { return 0; } } } } // 0 1 0 0 0 1 0 0 // 1 1 1 0 0 1 0 0 // 0 0 0 0 0 1 0 0 // 0 0 0 0 0 1 0 0 return 1; }