Java游戏俄罗斯方块的实现实例
Java游戏俄罗斯方块的实现实例
java小游戏主要理解应用javaSwing,awt等基础组件的知识,通过本例应当掌握面向对象的知识。
实现代码:
packagecn.hncu.games; 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; publicclassTetrisextendsJFrame{ privateTetrisPaneltp; publicTetris(){ //添加菜单,这里只是意思一下,留待大家自己做更详细的 //菜单条 JMenuBarmenubar=newJMenuBar(); setJMenuBar(menubar); //菜单 JMenumenuGame=newJMenu("游戏"); menubar.add(menuGame); //菜单项 JMenuItemmi1=newJMenuItem("新游戏"); mi1.setActionCommand("new"); JMenuItemmi2=newJMenuItem("暂停"); mi2.setActionCommand("pause"); JMenuItemmi3=newJMenuItem("继续"); mi3.setActionCommand("continue"); JMenuItemmi4=newJMenuItem("退出"); mi4.setActionCommand("exit"); menuGame.add(mi1); menuGame.add(mi2); menuGame.add(mi3); menuGame.add(mi4); //菜单项监听 MenuListenermenuListener=newMenuListener(); mi1.addActionListener(menuListener); mi2.addActionListener(menuListener); mi3.addActionListener(menuListener); mi4.addActionListener(menuListener); //版本菜单 JMenumenuHelp=newJMenu("帮助"); menubar.add(menuHelp); menuHelp.add("版本所有@湖南城院QQ:666688888"); setLocation(700,200); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(220,275); setResizable(false); tp=newTetrisPanel(); getContentPane().add(tp); //让整个画布添加键盘监听 //tp.addKeyListener(tp.listener);//不行,画布不方便获得键盘焦点 this.addKeyListener(tp.listener);//让框架来监听键盘 } publicstaticvoidmain(String[]args){ Tetriste=newTetris(); te.setVisible(true); } classMenuListenerimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ if(e.getActionCommand().equalsIgnoreCase("new")){ getContentPane().remove(tp); tp=newTetrisPanel(); getContentPane().add(tp); getContentPane().validate();//校验当前容器,有刷新功能 }elseif(e.getActionCommand().equalsIgnoreCase("pause")){ tp.getTimer().stop(); }elseif(e.getActionCommand().equalsIgnoreCase("continue")){ tp.getTimer().restart(); }elseif(e.getActionCommand().equalsIgnoreCase("exit")){ System.exit(0); } } } } classTetrisPanelextendsJPanel{ privateintmap[][]=newint[13][23];//map[列号][行号]。真正的方块区是:21行*10列。边框(2列,1行) //方块的形状: //第一维代表方块类型(包括7种:S、Z、L、J、I、O、T) //第二维代表旋转次数 //第三四维代表方块矩阵 //shapes[type][turnState][i]i-->block[i/4][i%4] intshapes[][][]=newint[][][]{ /* *模板{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0, *0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0, *0,0,0,0}} */ //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第3行:shapes[2][2][] {{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}}}; privateinttype; privateintturnState; privateintx,y;//当前块的位置---左上角的坐标 privateintscore=0; privateTimertimer=null; privateintdelay=1000; TimerListenerlistener=null; publicTetrisPanel(){ newGame(); nextBlock(); listener=newTimerListener(); timer=newTimer(delay,listener); timer.start(); } privatevoidnewGame(){ //初始化游戏地图 for(inti=0;i<12;i++){ for(intj=0;j<21;j++){ if(i==0||i==11){//边框 map[i][j]=3; }else{ map[i][j]=0; } } map[i][21]=3; } score=0; } privatevoidnextBlock(){ type=(int)(Math.random()*1000)%7;//type=5; turnState=(int)(Math.random()*1000)%4;//turnState=3; x=4; y=0; if(crash(x,y,type,turnState)==0){ timer.stop(); intop=JOptionPane.showConfirmDialog(null, "GameOver!....笨蛋,敢再来一局吗?!"); if(op==JOptionPane.YES_OPTION){ newGame(); }elseif(op==JOptionPane.NO_OPTION){ System.exit(0); } } } privatevoiddown(){ if(crash(x,y+1,type,turnState)==0){//判断当前块往下落一格后是否和地图存在填充块完全重合---注意实参:y+1 add(x,y,type,turnState);//把该块加到地图---形成堆积块 nextBlock(); }else{ y++; } repaint(); } privatevoidleft(){ if(x>=0){ x-=crash(x-1,y,type,turnState); } repaint(); } privatevoidright(){ if(x<8){ x+=crash(x+1,y,type,turnState); } repaint(); } privatevoidturn(){ if(crash(x,y,type,(turnState+1)%4)==1){ turnState=(turnState+1)%4; } repaint(); } //让一个块堆积,其实是把当前块中的填充块信息记录到map[][]中 privatevoidadd(intx,inty,inttype,intturnState){ for(inta=0;a<4;a++){ for(intb=0;b<4;b++){ if(shapes[type][turnState][a*4+b]==1){ map[x+b+1][y+a]=1; } } } tryDelLine(); } //消块 privatevoidtryDelLine(){ //从上往下,一行行依次遍历,如果某一行的map[i][j]值全是1,则把这一行消掉---上一行往下落 for(intb=0;b<21;b++){ intc=1; for(inta=0;a<12;a++){ c&=map[a][b]; } if(c==1){//全是1--下落一行 score+=10; for(intd=b;d>0;d--){ for(inte=0;e<11;e++){ map[e][d]=map[e][d-1]; } } //更改游戏的难度(加快下落速度) delay/=2; timer.setDelay(delay); } } } privateintcrash(intx,inty,intblockType,intturnState){ for(inta=0;a<4;a++){ for(intb=0;b<4;b++){ if((shapes[blockType][turnState][a*4+b]&map[x+b+1][y +a])==1){//和填充块或框架重合,都算碰撞 return0;//碰撞了---方块的填充块和地图中的填充块完全重合 } } } return1;//没有碰撞 } //表现层 @Override publicvoidpaint(Graphicsg){ super.paint(g);//清除残影 //画当前块 for(intj=0;j<16;j++){ if(shapes[type][turnState][j]==1){ g.setColor(Color.green); g.fillRect((j%4+x+1)*10,(j/4+y)*10,10,10); } } /* *for(inta=0;a<4;a++){for(intb=0;b<4;b++){ *if(shapes[type][turnState][a*4+b]==1){g.fillRect((b+x+1)*10, *(a+y)*10,10,10);}}} */ //画地图(整个游戏的方块区和边框) for(inti=0;i<12;i++){ for(intj=0;j<22;j++){ if(map[i][j]==1){ g.setColor(Color.red); g.fillRect(i*10,j*10,10,10);//填充 g.setColor(Color.yellow); g.drawRect(i*10,j*10,10,10);//格线 }elseif(map[i][j]==3){ g.setColor(Color.red); g.drawRect(i*10,j*10,10,10); } } } //显示分数,同时为版面美观,在界面上再加点东西 //画方块区右侧部分 g.setColor(Color.blue); g.setFont(newFont("aa",Font.BOLD,18)); g.drawString("score="+score,130,20); g.setFont(newFont("aa",Font.PLAIN,13)); g.drawString("拒绝盗版游戏",130,70); g.drawString("注意自我保护",130,90); g.drawString("谨防受骗上当。",125,110); g.drawString("适度游戏益脑,",125,130); g.drawString("沉迷游戏伤身。",125,150); g.drawString("合理安排时间,",125,170); g.drawString("享受健康生活。",125,190); } classTimerListenerextendsKeyAdapterimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ down(); } @Override publicvoidkeyPressed(KeyEvente){ //System.out.println("aaaaa"); switch(e.getKeyCode()){ caseKeyEvent.VK_DOWN: down(); break; caseKeyEvent.VK_LEFT: left(); break; caseKeyEvent.VK_RIGHT: right(); break; caseKeyEvent.VK_UP: turn(); } } } publicTimergetTimer(){ returntimer; } }
以上就是Java俄罗斯方块的详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!