java俄罗斯方块小游戏代码

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

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

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

资源描述

packagekeshe;importjava.awt.Color;//颜色importjava.awt.Font;//字体importjava.awt.Graphics;//绘图importjava.awt.event.ActionEvent;//动作importjava.awt.event.ActionListener;//事件处理importjava.awt.event.KeyAdapter;//接受键盘信息importjava.awt.event.KeyEvent;//键盘信息处理importjavax.swing.JFrame;//窗体importjavax.swing.JMenu;//菜单importjavax.swing.JMenuBar;//菜单条importjavax.swing.JMenuItem;//菜单项importjavax.swing.JOptionPane;//消息提示框importjavax.swing.JPanel;//中间容器importjavax.swing.Timer;//时间publicclassdddddddextendsJFrame{publicstaticvoidmain(String[]args){dddddddte=newddddddd();te.setVisible(true);}privateTetrisPaneltp;JMenuItemitemPause;JMenuItemitemContinue;JMenuItemitemgnd;JMenuItemitemdnd;publicddddddd(){this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLocation(500,200);this.setSize(220,275);this.setResizable(false);tp=newTetrisPanel();this.getContentPane().add(tp);//添加菜单JMenuBarmenubar=newJMenuBar();this.setJMenuBar(menubar);JMenumenuGame=newJMenu(游戏);menubar.add(menuGame);JMenuItemitemNew=newJMenuItem(新游戏);itemNew.setActionCommand(new);itemgnd=newJMenuItem(提高难度);itemgnd.setActionCommand(gnd);itemdnd=newJMenuItem(降低难度);itemdnd.setActionCommand(dnd);itemPause=newJMenuItem(暂停);itemPause.setActionCommand(pause);itemContinue=newJMenuItem(继续);itemContinue.setActionCommand(continue);itemContinue.setEnabled(false);itemdnd.setEnabled(false);menuGame.add(itemNew);menuGame.add(itemPause);menuGame.add(itemContinue);menuGame.add(itemgnd);menuGame.add(itemdnd);MenuListenermenuListener=newMenuListener();itemNew.addActionListener(menuListener);itemPause.addActionListener(menuListener);itemContinue.addActionListener(menuListener);itemgnd.addActionListener(menuListener);itemdnd.addActionListener(menuListener);//让整个JFrame添加键盘监听this.addKeyListener(tp.listener);}classMenuListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){//玩新游戏if(e.getActionCommand().equals(new)){tp.newGame();}if(e.getActionCommand().equals(pause)){timer.stop();itemContinue.setEnabled(true);itemPause.setEnabled(false);}if(e.getActionCommand().equals(continue)){timer.restart();itemContinue.setEnabled(false);itemPause.setEnabled(true);}if(tp.nandu==1){itemdnd.setEnabled(false);}elseitemdnd.setEnabled(true);if(tp.nandu==9)itemgnd.setEnabled(false);elseitemgnd.setEnabled(true);if(e.getActionCommand().equals(gnd)){tp.delay/=1.3;tp.nandu+=1;timer.setDelay(tp.delay);}if(e.getActionCommand().equals(dnd)){tp.delay*=1.3;tp.nandu-=1;timer.setDelay(tp.delay);}}}privateTimertimer;classTetrisPanelextendsJPanel{//方块的形状://第一维代表方块类型(包括7种:S、Z、L、J、I、O、T)//第二维代表旋转次数//第三四维代表方块矩阵//shapes[type][turnState][i]i--block[i/4][i%4]intshapes[][][]=newint[][][]{//I(※把版本1中的横条从第1行换到第2行){{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,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}},//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}}};privateintblockType;//方块类型privateintturnState;//旋转状态privateintdierkuai;privateintdierState;privateintx;//方块的位置x--列的位置--列号privateinty;//方块的位置y--行的位置--行号privateintmap[][]=newint[13][23];//地图:12列22行。为防止越界,数组开成:13列23行privateintdelay=1000;privateintnandu=1;publicTimerKeyListerlistener=newTimerKeyLister();privateintscore=0;//分数publicTetrisPanel(){delay=1000;timer=newTimer(delay,listener);timer.start();newGame();nextBlock();//timer=newTimer(delay,listener);//timer.start();}publicvoidnewGame(){blockType=(int)(Math.random()*1000)%7;turnState=(int)(Math.random()*1000)%4;dierkuai=(int)(Math.random()*1000)%7;dierState=(int)(Math.random()*1000)%4;x=4;y=0;for(inti=0;i12;i++){//走列for(intj=0;j21;j++){//走行if(i==0||i==11){//3为界面边框的格map[i][j]=3;}else{map[i][j]=0;}}map[i][21]=3;//3为界面边框的格}delay=1000;time();nandu=1;score=0;}privatevoidnextBlock(){blockType=dierkuai;dierkuai=(int)(Math.random()*1000)%7;turnState=dierState;dierState=(int)(Math.random()*1000)%4;x=4;y=0;//gameOverif(crash(x,y,blockType,turnState)==0){timer.stop();intoption=JOptionPane.showConfirmDialog(this,GameOver!!,还敢来吗...);if(option==JOptionPane.OK_OPTION){newGame();}elseif(option==JOptionPane.NO_OPTION){System.exit(0);}}}privatevoiddown(){if(crash(x,y+1,blockType,turnState)==0){add(x,y,blockType,turnState);//把当前方块的信息保存到地图nextBlock();}else{y++;}repaint();}privatevoidleft(){if(x=0){x-=crash(x-1,

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

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

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

×
保存成功