Java怎样实现经典俄罗斯方块游戏(java,开发技术)

时间:2024-05-09 12:55:20 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

前言

俄罗斯方块是一个最初由阿列克谢帕吉特诺夫在苏联设计和编程的益智类视频游戏。

《俄罗斯方块》的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。

用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想。

主要需求

由小方块组成的不同形状的板块陆续从屏幕上方落下来,玩家通过调整板块的位置和方向,使它们在屏幕底部拼出完整的一条或几条。这些完整的横条会随即消失,给新落下来的板块腾出空间,与此同时,玩家得到分数奖励。没有被消除掉的方块不断堆积起来,一旦堆到屏幕顶端,玩家便告输,游戏结束。

主要设计

1、用键盘操作,"←"左移一格;"→"右移一格;"↑"旋转方块;↓ 方块丢下(方块下落到底)

2、一旦堆到屏幕顶端,游戏结束

3、设计不同的方块

4、设计方块下落的速度

5、设计分数系统

功能截图

游戏启动页

Java怎样实现经典俄罗斯方块游戏

开始游戏

Java怎样实现经典俄罗斯方块游戏

Java怎样实现经典俄罗斯方块游戏

游戏结束

Java怎样实现经典俄罗斯方块游戏

代码实现

窗口设计类

publicclassWindowsextendsJFrame{ privateToolkittook=null; privatestaticintwidth=470;//宽 privatestaticintheight=680;//高 privatestaticintwidth_g=10;//游戏区域 privatestaticintheight_g=22;// privatestaticintsize=30;//方块大小 privatestaticintspace=10;//坐标距边界间隔 Mapmap=newMap(width_g,height_g);//地图坐标 ArrayList<Diamonds>ds=newArrayList<Diamonds>();//方块数组 privatebooleangame=false;//游戏开始 privateintflag=0;//游戏状态【暂停:0,继续:1】 privateJLabeljl2; privateJButtonjb1; privateintspeed=500;//速度 privateintscore=0;//分数 privateint[]scores=newint[4];//排名 publicstaticvoidmain(String[]args){ Windowswin=newWindows(); win.run(); } publicvoidrun(){ init(); try{ while(true){ if(game&&flag==1){ ds.get(ds.size()-2).movement(map); if(!ds.get(ds.size()-2).isStatus()){ //判断游戏是否失败 if(ds.get((ds.size()-2)).getY()<=3){ game=false; //重置游戏参数 ds=newArrayList<Diamonds>(); map=newMap(width_g,height_g); JOptionPane.showMessageDialog(newJFrame().getContentPane(),"游戏结束!\n您获得【"+score+"】点分数"); score=0; jl2.setText("分数:"+score); jb1.setEnabled(true); jb1.setText("重新开始"); }else{ //消除判断 score=map.dispel(score); jl2.setText("分数:"+score); //生成新方块 Diamondsdiamonds=newDiamonds(width_g); ds.add(diamonds); } } } repaint(); Thread.sleep(speed); } }catch(InterruptedExceptione){ e.printStackTrace(); } } //窗口加载 publicvoidinit(){ //界面设置 this.setTitle("俄罗斯方块");//标题 this.setSize(width,height);//界面大小 took=Toolkit.getDefaultToolkit(); Dimensiondm=took.getScreenSize(); intswidth=(dm.width-width)/2; intsheight=(dm.height-height)/2; this.setLocation(swidth,sheight); //容器 JPanelp1=newJPanel();//地图 JPanelp2=newJPanel();//俄罗斯方块控制界面 JPanelp3=newJPanel();//按钮 JPanelp4=newJPanel();//说明 //图形绘制容器 JPanelcontentPane=newPaintPanel(); setContentPane(contentPane); //标签 JLabeljl1=newJLabel("俄罗斯方块控制界面"); jl2=newJLabel("分数:"+score); JLabeljl3=newJLabel("游戏说明:"); //按钮 jb1=newJButton("游戏开始"); JButtonjb2=newJButton("难度选择"); JButtonjb3=newJButton("继续/暂停"); JButtonjb4=newJButton("游戏退出"); JButtonjb5=newJButton("高级"); JButtonjb6=newJButton("中级"); JButtonjb7=newJButton("低级"); JButtonjb8=newJButton("显示排名"); //文本 JTextAreajta=newJTextArea("1.点击【游戏开始】按钮开始游戏。" +"\n2.游戏中可随时暂停,使用方向键可继续游戏" +"\n3.用键盘操作,\"←\"左移一格;\"→\"右移一格;\"↑\"旋转方块;↓方块丢下(方块下落到底)",50,9); jta.setSelectionColor(Color.RED); jta.setEditable(false); jta.setLineWrap(true); //布局 this.setLayout(newBorderLayout(5,5)); p2.setLayout(newGridLayout(2,1,5,5)); p3.setLayout(newGridLayout(10,1,5,5)); p4.setLayout(newBorderLayout(5,5)); //设置边界 p2.setBorder(BorderFactory.createEmptyBorder(20,20,15,15)); //背景颜色 p1.setBackground(newColor(255,255,255,0)); p2.setBackground(newColor(255,255,255,0)); p3.setBackground(newColor(255,255,255,0)); p4.setBackground(newColor(255,255,255,0)); jta.setBackground(Color.WHITE); //添加容器/组件 p3.add(jl1); p3.add(jl2); p3.add(jb1); //p3.add(jb2); p3.add(jb3); p3.add(jb4); p3.add(jb8); p4.add(jl3,BorderLayout.NORTH); p4.add(jta,BorderLayout.CENTER); p2.add(p3); p2.add(p4); //主容器 this.add(p1,BorderLayout.CENTER); this.add(p2,BorderLayout.EAST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setUndecorated(true); this.setVisible(true); this.addKeyListener(newKeyAdapter(){ publicvoidkeyPressed(KeyEvente){ //游戏开始时按键有效 if(ds.size()!=0){ inttype=ds.get(ds.size()-2).getType(); intx=ds.get(ds.size()-2).getX(); if(e.getKeyCode()==KeyEvent.VK_LEFT){ ds.get(ds.size()-2).left(map); } if(e.getKeyCode()==KeyEvent.VK_RIGHT){ ds.get(ds.size()-2).right(map); } if(e.getKeyCode()==KeyEvent.VK_UP){ if(type<=1){ //可能在转换过程发生越界的解决办法 if(type==1){ if(x>=width_g-3){ ds.get(ds.size()-2).setX(width_g-4); } } ds.get(ds.size()-2).setType(type==0?1:0); }elseif(type<=5){ if(type==3||type==5){ if(x==width_g-2){ ds.get(ds.size()-2).setX(width_g-3); } } ds.get(ds.size()-2).setType(type==5?2:++type); }elseif(type<=9){ ds.get(ds.size()-2).setType(type==9?6:++type); }elseif(type<=10){ ds.get(ds.size()-2).setType(10); }elseif(type<=14){ if(type==12||type==14){ if(x==width_g-2){ ds.get(ds.size()-2).setX(width_g-3); } } ds.get(ds.size()-2).setType(type==14?11:++type); }elseif(type<=18){ if(type==16||type==18){ if(x==width_g-2){ ds.get(ds.size()-2).setX(width_g-3); } } ds.get(ds.size()-2).setType(type==18?15:++type); } } } if(e.getKeyCode()==KeyEvent.VK_DOWN){ speed=100; } } publicvoidkeyReleased(KeyEvente){ if(e.getKeyCode()==KeyEvent.VK_DOWN){ speed=500; } } }); //游戏开始 jb1.addMouseListener(newMouseAdapter(){ @Override publicvoidmouseClicked(MouseEvente){ requestFocus(); //生成第一个方块 Diamondsdiamonds=newDiamonds(width_g); ds.add(diamonds); //生成提示方块 Diamondspoint=newDiamonds(width_g); ds.add(point); //游戏开始 game=true; flag=1; //游戏开始后禁止使用 jb1.setEnabled(false); } }); //退出 jb4.addMouseListener(newMouseAdapter(){ @Override publicvoidmouseClicked(MouseEvente){ System.exit(0); } }); //暂停/继续 jb3.addMouseListener(newMouseAdapter(){ @Override publicvoidmouseClicked(MouseEvente){ if(flag==0){ flag=1; }else{ flag=0; } requestFocus(); } }); } //重写paintComponent(图形绘制) privateclassPaintPanelextendsJPanel{ @Override protectedvoidpaintComponent(Graphicsg){ //还原size的值 size=30; //绘制边界 g.setColor(Color.GRAY); g.fillRect(0,0,width-149,height-1); g.setColor(Color.PINK); g.fillRect(width-149,0,148,height-1); g.setColor(Color.BLACK); g.drawLine(width-149,0,width-149,height-1); g.drawRect(0,0,width-1,height-1); g.setColor(Color.WHITE); g.fillRect(space,space,width_g*size,height_g*size); g.setColor(Color.BLACK); g.drawRect(space,space,width_g*size,height_g*size); g.drawLine(space,space+3*size,space+width_g*size,space+3*size); //提示框 g.setColor(Color.WHITE); g.fillRect(width-135,222,4*size,4*size); g.setColor(Color.BLACK); g.drawRect(width-135,222,4*size,4*size); if(game){ Color[][]color_xy=map.getColor(); int[][]map_xy=map.getMap(); //绘制地图 for(inti=0;i<width_g;i++){ for(intj=0;j<height_g;j++){ //绘制网格线,可注释 g.drawRect(i*size+space,j*size+space,size,size); if(map_xy[i][j]==1){ //g.setColor(color_xy[i][j]); g.setColor(Color.BLUE); g.fillRect(i*size+space,j*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(i*size+space,j*size+space,size,size); } } } //绘制可移动的方块 inttype=ds.get(ds.size()-2).getType(); intx=ds.get(ds.size()-2).getX(); inty=ds.get(ds.size()-2).getY(); Colorcolor=ds.get(ds.size()-2).getColor(); //绘制提示方块 inttypeO=ds.get(ds.size()-1).getType(); intxO=width-100; intyO=260; ColorcolorO=ds.get(ds.size()-1).getColor(); //绘制图形 //图形一,两种形态 if(type==0){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+2)*size+space,y*size+space,size,size); g.fillRect((x+3)*size+space,y*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+2)*size+space,y*size+space,size,size); g.drawRect((x+3)*size+space,y*size+space,size,size); } if(type==1){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect(x*size+space,(y+2)*size+space,size,size); g.fillRect(x*size+space,(y+3)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect(x*size+space,(y+2)*size+space,size,size); g.drawRect(x*size+space,(y+3)*size+space,size,size); } //图形二,四种形态 if(type==2){ g.setColor(color); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect((x+2)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect((x+2)*size+space,(y+1)*size+space,size,size); } if(type==3){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect(x*size+space,(y+2)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect(x*size+space,(y+2)*size+space,size,size); } if(type==4){ g.setColor(color); g.fillRect((x)*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+2)*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+2)*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); } if(type==5){ g.setColor(color); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+2)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+2)*size+space,size,size); } //图形三,四种形态 if(type==6){ g.setColor(color); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); } if(type==7){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); } if(type==8){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); } if(type==9){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); } //图形四,一种形态 if(type==10){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); } //图形五,三种形态 if(type==11){ g.setColor(color); g.fillRect((x+2)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect((x+2)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect((x+2)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect((x+2)*size+space,(y+1)*size+space,size,size); } if(type==12){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+2)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+2)*size+space,size,size); } if(type==13){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+2)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+2)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); } if(type==14){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect(x*size+space,(y+2)*size+space,size,size); g.fillRect((x+1)*size+space,(y+2)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect(x*size+space,(y+2)*size+space,size,size); g.drawRect((x+1)*size+space,(y+2)*size+space,size,size); } //图形六,三种形态 if(type==15){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect((x+2)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect((x+2)*size+space,(y+1)*size+space,size,size); } if(type==16){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect(x*size+space,(y+1)*size+space,size,size); g.fillRect(x*size+space,(y+2)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect(x*size+space,(y+1)*size+space,size,size); g.drawRect(x*size+space,(y+2)*size+space,size,size); } if(type==17){ g.setColor(color); g.fillRect(x*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+2)*size+space,y*size+space,size,size); g.fillRect((x+2)*size+space,(y+1)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect(x*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+2)*size+space,y*size+space,size,size); g.drawRect((x+2)*size+space,(y+1)*size+space,size,size); } if(type==18){ g.setColor(color); g.fillRect((x+1)*size+space,y*size+space,size,size); g.fillRect((x+1)*size+space,(y+1)*size+space,size,size); g.fillRect(x*size+space,(y+2)*size+space,size,size); g.fillRect((x+1)*size+space,(y+2)*size+space,size,size); g.setColor(Color.BLACK); g.drawRect((x+1)*size+space,y*size+space,size,size); g.drawRect((x+1)*size+space,(y+1)*size+space,size,size); g.drawRect(x*size+space,(y+2)*size+space,size,size); g.drawRect((x+1)*size+space,(y+2)*size+space,size,size); } //绘制提示图形 size=(int)(size/1.5); if(typeO==0){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(xO+size,yO,size,size); g.fillRect(xO+size*2,yO,size,size); g.fillRect(xO+size*3,yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(xO+size,yO,size,size); g.drawRect(xO+size*2,yO,size,size); g.drawRect(xO+size*3,yO,size,size); } if(typeO==1){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(xO,2*size+yO,size,size); g.fillRect(xO,3*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(xO,2*size+yO,size,size); g.drawRect(xO,3*size+yO,size,size); } //图形二,四种形态 if(typeO==2){ g.setColor(colorO); g.fillRect(size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(2*size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(2*size+xO,size+yO,size,size); } if(typeO==3){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(xO,2*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(xO,2*size+yO,size,size); } if(typeO==4){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(2*size+xO,yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(2*size+xO,yO,size,size); g.drawRect(size+xO,size+yO,size,size); } if(typeO==5){ g.setColor(colorO); g.fillRect(size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(size+xO,2*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(size+xO,2*size+yO,size,size); } //图形三,四种形态 if(typeO==6){ g.setColor(colorO); g.fillRect(size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); } if(typeO==7){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); } if(typeO==8){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); } if(typeO==9){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(size+xO,size+yO,size,size); } //图形四,一种形态 if(typeO==10){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); } //图形五,三种形态 if(typeO==11){ g.setColor(colorO); g.fillRect(2*size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(2*size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(2*size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(2*size+xO,size+yO,size,size); } if(typeO==12){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(size+xO,2*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(size+xO,2*size+yO,size,size); } if(typeO==13){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(2*size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(2*size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); } if(typeO==14){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(xO,2*size+yO,size,size); g.fillRect(size+xO,2*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(xO,2*size+yO,size,size); g.drawRect(size+xO,2*size+yO,size,size); } //图形六,三种形态 if(typeO==15){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(2*size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(2*size+xO,size+yO,size,size); } if(typeO==16){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(xO,size+yO,size,size); g.fillRect(xO,2*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(xO,size+yO,size,size); g.drawRect(xO,2*size+yO,size,size); } if(typeO==17){ g.setColor(colorO); g.fillRect(xO,yO,size,size); g.fillRect(size+xO,yO,size,size); g.fillRect(2*size+xO,yO,size,size); g.fillRect(2*size+xO,size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(xO,yO,size,size); g.drawRect(size+xO,yO,size,size); g.drawRect(2*size+xO,yO,size,size); g.drawRect(2*size+xO,size+yO,size,size); } if(typeO==18){ g.setColor(colorO); g.fillRect(size+xO,yO,size,size); g.fillRect(size+xO,size+yO,size,size); g.fillRect(xO,2*size+yO,size,size); g.fillRect(size+xO,2*size+yO,size,size); g.setColor(Color.BLACK); g.drawRect(size+xO,yO,size,size); g.drawRect(size+xO,size+yO,size,size); g.drawRect(xO,2*size+yO,size,size); g.drawRect(size+xO,2*size+yO,size,size); } } } }}

方块及相关事件设计类

/**方块*/publicclassDiamonds{ privateintx;//坐标x privateinty;//坐标y privateColorcolor;//颜色 privateinttype;//类型 Randomrm=newRandom(); privatebooleanstatus=true;//状态 privateintwidth; publicDiamonds(){ super(); //TODOAuto-generatedconstructorstub } //方块初始化 publicDiamonds(intwidth){ super(); this.width=width; this.x=rm.nextInt(width-3); this.y=0; //this.color=Color.GREEN; this.color=newColor(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)); this.type=rm.nextInt(16); } //向左移动 publicvoidleft(Mapmap){ int[][]map_xy=map.getMap(); if(x!=0){ switch(type){ case0:if(map_xy[x-1][y]==0){x--;}break; case1:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0&&map_xy[x-1][y+2]==0&&map_xy[x-1][y+3]==0){x--;}break; case2:if(map_xy[x][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case3:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0&&map_xy[x-1][y+2]==0){x--;}break; case4:if(map_xy[x-1][y]==0&&map_xy[x][y+1]==0){x--;}break; case5:if(map_xy[x][y]==0&&map_xy[x-1][y+1]==0&&map_xy[x][y+2]==0){x--;}break; case6:if(map_xy[x][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case7:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case8:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case9:if(map_xy[x-1][y]==0&&map_xy[x][y+1]==0){x--;}break; case10:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case11:if(map_xy[x+1][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case12:if(map_xy[x-1][y]==0&&map_xy[x][y+1]==0&&map_xy[x][y+2]==0){x--;}break; case13:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case14:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0&&map_xy[x-1][y+2]==0){x--;}break; case15:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0){x--;}break; case16:if(map_xy[x-1][y]==0&&map_xy[x-1][y+1]==0&&map_xy[x-1][y+2]==0){x--;}break; case17:if(map_xy[x-1][y]==0&&map_xy[x+1][y+1]==0){x--;}break; case18:if(map_xy[x][y]==0&&map_xy[x][y+1]==0&&map_xy[x-1][y+2]==0){x--;}break; } } } //向右移动 publicvoidright(Mapmap){ int[][]map_xy=map.getMap(); switch(type){ case0:if(x<width-4&&map_xy[x+4][y]==0){x++;}break; case1:if(x<width-1&&map_xy[x+1][y]==0&&map_xy[x+1][y+1]==0&&map_xy[x+1][y+2]==0&&map_xy[x+1][y+3]==0){x++;}break; case2:if(x<width-3&&map_xy[x+2][y]==0&&map_xy[x+3][y+1]==0){x++;}break; case3:if(x<width-2&&map_xy[x+1][y]==0&&map_xy[x+2][y+1]==0&&map_xy[x+1][y+2]==0){x++;}break; case4:if(x<width-3&&map_xy[x+3][y]==0&&map_xy[x+2][y+1]==0){x++;}break; case5:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+2][y+1]==0&&map_xy[x+2][y+2]==0){x++;}break; case6:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+2][y+1]==0){x++;}break; case7:if(x<width-2&&map_xy[x+1][y]==0&&map_xy[x+2][y+1]==0){x++;}break; case8:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+1][y+1]==0){x++;}break; case9:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+2][y+1]==0){x++;}break; case10:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+2][y+1]==0){x++;}break; case11:if(x<width-3&&map_xy[x+3][y]==0&&map_xy[x+3][y+1]==0){x++;}break; case12:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+2][y+1]==0&&map_xy[x+2][y+2]==0){x++;}break; case13:if(x<width-3&&map_xy[x+3][y]==0&&map_xy[x+1][y+1]==0){x++;}break; case14:if(x<width-2&&map_xy[x+1][y]==0&&map_xy[x+1][y+1]==0&&map_xy[x+2][y+2]==0){x++;}break; case15:if(x<width-3&&map_xy[x+1][y]==0&&map_xy[x+3][y+1]==0){x++;}break; case16:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+1][y+1]==0&&map_xy[x+1][y+2]==0){x++;}break; case17:if(x<width-3&&map_xy[x+3][y]==0&&map_xy[x+3][y+1]==0){x++;}break; case18:if(x<width-2&&map_xy[x+2][y]==0&&map_xy[x+2][y+1]==0&&map_xy[x+2][y+2]==0){x++;}break; } } //向下移动 publicvoidmovement(Mapmap){ int[][]map_xy=map.getMap(); Color[][]color_xy=map.getColor(); //向下移动一格 switch(type){ case0: if(map_xy[x][y+1]==0&&map_xy[x+1][y+1]==0&&map_xy[x+2][y+1]==0&&map_xy[x+3][y+1]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x+2][y]=1; map_xy[x+3][y]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x+2][y]=color; color_xy[x+3][y]=color; status=false; } break; case1: if(map_xy[x][y+4]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x][y+1]=1; map_xy[x][y+2]=1; map_xy[x][y+3]=1; color_xy[x][y]=color; color_xy[x][y+1]=color; color_xy[x][y+2]=color; color_xy[x][y+3]=color; status=false; } break; case2: if(map_xy[x][y+2]==0&&map_xy[x+1][y+2]==0&&map_xy[x+2][y+2]==0){ y+=1; }else{ map_xy[x+1][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; map_xy[x+2][y+1]=1; color_xy[x+1][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; color_xy[x+2][y+1]=color; status=false; } break; case3: if(map_xy[x][y+3]==0&&map_xy[x+1][y+2]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; map_xy[x][y+2]=1; color_xy[x][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; color_xy[x][y+2]=color; status=false; } break; case4: if(map_xy[x][y+1]==0&&map_xy[x+1][y+2]==0&&map_xy[x+2][y+1]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x+2][y]=1; map_xy[x+1][y+1]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x+2][y]=color; color_xy[x+1][y+1]=color; status=false; } break; case5: if(map_xy[x][y+2]==0&&map_xy[x+1][y+3]==0){ y+=1; }else{ map_xy[x+1][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; map_xy[x+1][y+2]=1; color_xy[x+1][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; color_xy[x+1][y+2]=color; status=false; } break; case6: if(map_xy[x][y+2]==0&&map_xy[x+1][y+2]==0){ y+=1; }else{ map_xy[x+1][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; color_xy[x+1][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; status=false; } break; case7: if(map_xy[x][y+2]==0&&map_xy[x+1][y+2]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; color_xy[x][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; status=false; } break; case8: if(map_xy[x][y+2]==0&&map_xy[x+1][y+1]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x][y+1]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x][y+1]=color; status=false; } break; case9: if(map_xy[x][y+1]==0&&map_xy[x+1][y+2]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x+1][y+1]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x+1][y+1]=color; status=false; } break; case10: if(map_xy[x][y+2]==0&&map_xy[x+1][y+2]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; status=false; } break; case11: if(map_xy[x][y+2]==0&&map_xy[x+1][y+2]==0&&map_xy[x+2][y+2]==0){ y+=1; }else{ map_xy[x+2][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; map_xy[x+2][y+1]=1; color_xy[x+2][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; color_xy[x+2][y+1]=color; status=false; } break; case12: if(map_xy[x][y+1]==0&&map_xy[x+1][y+3]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x+1][y+1]=1; map_xy[x+1][y+2]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x+1][y+1]=color; color_xy[x+1][y+2]=color; status=false; } break; case13: if(map_xy[x][y+2]==0&&map_xy[x+1][y+1]==0&&map_xy[x+2][y+1]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x+2][y]=1; map_xy[x][y+1]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x+2][y]=color; color_xy[x][y+1]=color; status=false; } break; case14: if(map_xy[x][y+3]==0&&map_xy[x+1][y+3]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x][y+1]=1; map_xy[x][y+2]=1; map_xy[x+1][y+2]=1; color_xy[x][y]=color; color_xy[x][y+1]=color; color_xy[x][y+2]=color; color_xy[x+1][y+2]=color; status=false; } break; case15: if(map_xy[x][y+2]==0&&map_xy[x+1][y+2]==0&&map_xy[x+2][y+2]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x][y+1]=1; map_xy[x+1][y+1]=1; map_xy[x+2][y+1]=1; color_xy[x][y]=color; color_xy[x][y+1]=color; color_xy[x+1][y+1]=color; color_xy[x+2][y+1]=color; status=false; } break; case16: if(map_xy[x][y+3]==0&&map_xy[x+1][y+1]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x][y+1]=1; map_xy[x][y+2]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x][y+1]=color; color_xy[x][y+2]=color; status=false; } break; case17: if(map_xy[x][y+1]==0&&map_xy[x+1][y+1]==0&&map_xy[x+2][y+2]==0){ y+=1; }else{ map_xy[x][y]=1; map_xy[x+1][y]=1; map_xy[x+2][y]=1; map_xy[x+2][y+1]=1; color_xy[x][y]=color; color_xy[x+1][y]=color; color_xy[x+2][y]=color; color_xy[x+2][y+1]=color; status=false; } break; case18: if(map_xy[x][y+3]==0&&map_xy[x+1][y+3]==0){ y+=1; }else{ map_xy[x+1][y]=1; map_xy[x+1][y+1]=1; map_xy[x][y+2]=1; map_xy[x+1][y+2]=1; color_xy[x+1][y]=color; color_xy[x+1][y+1]=color; color_xy[x][y+2]=color; color_xy[x+1][y+2]=color; status=false; } break; } map.setMap(map_xy); map.setColor(color_xy); } publicintgetX(){ returnx; } publicvoidsetX(intx){ this.x=x; } publicintgetY(){ returny; } publicvoidsetY(inty){ this.y=y; } publicColorgetColor(){ returncolor; } publicvoidsetColor(Colorcolor){ this.color=color; } publicintgetType(){ returntype; } publicvoidsetType(inttype){ this.type=type; } publicbooleanisStatus(){ returnstatus; } publicvoidsetStatus(booleanstatus){ this.status=status; }}

Java的优点是什么

1. 简单,只需理解基本的概念,就可以编写适合于各种情况的应用程序;2. 面向对象;3. 分布性,Java是面向网络的语言;4. 鲁棒性,java提供自动垃圾收集来进行内存管理,防止程序员在管理内存时容易产生的错误。;5. 安全性,用于网络、分布环境下的Java必须防止病毒的入侵。6. 体系结构中立,只要安装了Java运行时系统,就可在任意处理器上运行。7. 可移植性,Java可以方便地移植到网络上的不同机器。8.解释执行,Java解释器直接对Java字节码进行解释执行。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java怎样实现经典俄罗斯方块游戏的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:怎么解决php的exit中文乱码问题下一篇:

18 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18