java游戏代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

java游戏代码IT技术2009-04-1808:08:35阅读631评论6字号:大中小订阅importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassJTetrixextendsJFrameimplementsRunnable{privateImageIconiconLogo;privateJPanelnextPanel,scorePanel,opPanel,gamePanel,hidedOpPanel;privateJLabellabLevel,labLine,labScore;privatefinalJTextAreakeyfocus;//键盘的操作焦点,隐藏于游戏画面之后privateJButtonbtnNew,btnPause,btnQuit;privateshowNextPiecenextPieceArea;//预览下一方块privatefinalgameOpBoardgameOpArea;//游戏画面privateContainerc;privateintLevel,Score,LineRemoved;//等级、得分、移除总行数privatedoubleinterval;//暂停时间,用于控制速度privatebooleanpause;//是否暂停privateThreadloopThread;//游戏回圈执行绪staticfinallongserialVersionUID=1;publicJTetrix(){c=getContentPane();//iconLogo=newImageIcon(JTetrix.class.getResource(logo.jpg));Level=1;Score=0;LineRemoved=0;//预览下一个方块的面版配置nextPanel=newJPanel();nextPanel.setLayout(null);nextPanel.setBorder(BorderFactory.createTitledBorder(下一个));nextPanel.setLocation(10,10);nextPanel.setSize(150,120);nextPieceArea=newshowNextPiece(5,6,15,15);nextPieceArea.generateNextPiece();//先产生第一片待取nextPieceArea.setLocation(40,20);nextPanel.add(nextPieceArea);//等级、得分面版配置scorePanel=newJPanel();scorePanel.setBorder(BorderFactory.createTitledBorder(等级/得分));scorePanel.setLocation(10,140);scorePanel.setSize(150,160);scorePanel.add(newJLabel(速度等级));scorePanel.add(labLevel=newJLabel(1,SwingConstants.CENTER));scorePanel.add(newJLabel(移除行数));scorePanel.add(labLine=newJLabel(0,SwingConstants.CENTER));scorePanel.add(newJLabel(目前得分));scorePanel.add(labScore=newJLabel(0,SwingConstants.CENTER));//功能面版配置opPanel=newJPanel();opPanel.setBorder(BorderFactory.createTitledBorder());opPanel.setLocation(10,320);opPanel.setSize(150,130);opPanel.add(btnNew=newJButton(开新游戏));opPanel.add(btnPause=newJButton(暂停游戏));opPanel.add(btnQuit=newJButton(关闭游戏));//游戏操作画面面版配置gameOpArea=newgameOpBoard(10,22,18,18);gameOpArea.setLocation(20,20);gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());//先取出一个方块nextPieceArea.generateNextPiece();//预览窗格产生下一个方块预览gamePanel=newJPanel();gamePanel.setLayout(null);gamePanel.setLocation(180,20);gamePanel.setSize(12*18,24*18);gamePanel.setBorder(BorderFactory.createTitledBorder());gamePanel.add(gameOpArea);//隐藏的操作面版配置hidedOpPanel=newJPanel();hidedOpPanel.setLayout(newBorderLayout());hidedOpPanel.setLocation(200,40);hidedOpPanel.setSize(10*18,22*18);keyfocus=newJTextArea();keyfocus.setEditable(false);hidedOpPanel.add(keyfocus);//加入面版至视窗c.setLayout(null);c.add(nextPanel);c.add(scorePanel);c.add(opPanel);c.add(gamePanel);c.add(hidedOpPanel);//键盘事件处理keyfocus.addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEvente){intkey=e.getKeyCode();if(key==KeyEvent.VK_RIGHT)gameOpArea.moveRight();elseif(key==KeyEvent.VK_LEFT)gameOpArea.moveLeft();elseif(key==KeyEvent.VK_UP)gameOpArea.RotateRL(1);elseif(key==KeyEvent.VK_DOWN)gameOpArea.RotateRL(0);elseif(key==KeyEvent.VK_SPACE){gameOpArea.soonMoveDown();}}});//开新游戏btnNew.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){newGame();keyfocus.requestFocus();}});//暂停游戏btnPause.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){pause=true;JOptionPane.showOptionDialog(null,程式名称:\nJTetrixv0.1\n+程式设计:\nyounganne\n+简介:\n一个用Java写的俄罗斯方块游戏\n,关于JTetrix,JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);pause=false;interval=2000/Level;loopThread.interrupt();keyfocus.requestFocus();}});//关闭游戏btnQuit.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);}});//启动游戏回圈执行绪loopThread=newThread(this);loopThread.start();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle(JTetrixv0.1);setSize(420,500);setVisible(true);}//游戏回圈的执行绪执行对象publicvoidrun(){while(true){//游戏回圈try{//是否暂停if(pause)interval=999999999;elseinterval=2000/Level;if(gameOpArea.isUpdated()){//由游戏画面的阵列是否更新来判断是否取出下一个gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());nextPieceArea.generateNextPiece();//更新等级、得分等资讯Score=gameOpArea.getScore();labScore.setText(+Score);LineRemoved=gameOpArea.getLineRemoved();labLine.setText(+LineRemoved);Level=(int)((Score+100)/100);//每一百分升级一次labLevel.setText(+Level);}Thread.sleep((int)interval);gameOpArea.MoveDown();//不断下移}catch(InterruptedExceptione){}}}//开新游戏publicvoidnewGame(){Level=1;Score=0;LineRemoved=0;labScore.setText(+Score);labLine.setText(+LineRemoved);labLevel.setText(+Level);gameOpArea.setCurrentPiece(nextPieceArea.getNextTetrix());//先取出一个方块gameOpArea.newState();nextPieceArea.generateNextPiece();//预览窗格产生下一个方块预览}publicstaticvoidmain(String[]args){JTetrixfrm=newJTetrix();}}//方块的资料结构与操作方法classTetrixPiece{privateintpieceType;//方块样式privateint[][]coordinates=newint[4][2];//四个方块,记录X与YpublicTetrixPiece(){initialize((int)(Math.random()*7+1));}publicTetrixPiece(inttype){initialize(type);}//向左转动publicvoidrotateLeft(){if(pieceType==5)//不转动正方形方块return;inttmp;for(inti=0;i4;i++){tmp=getXCoord(i);setXCoord(i,getYCoord(i));setYCoord(i,-tmp);}}//向右转动publicvoidrotateRight(){if(pieceType==5)//不转动正方形方块return;inttmp;for(inti=0;i4;i++){tmp=getXCoord(i);setXCoord(i,

1 / 61
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功