欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-01 18:08:02
目录前言主要设计功能截图代码实现总结前言俄罗斯方块是一个最初由阿列克谢帕吉特诺夫在苏联设计和编程的益智类视频游戏。《俄罗斯方块》的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行...

前言

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

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

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

主要需求

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

主要设计

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

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

3、设计不同的方块

4、设计方块下落的速度

5、设计分数系统

功能截图

游戏启动页

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

开始游戏

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

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

游戏结束

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

代码实现

窗口设计类

public class windows extends jframe {
	private toolkit took = null;
	private static int width= 470;     //宽
	private static int height = 680;   //高
	private static int width_g = 10;   //游戏区域
	private static int height_g = 22;  //
	private static int size = 30;      //方块大小
	private static int space= 10;      //坐标距边界间隔
	map map = new map(width_g,height_g);//地图坐标
	arraylist <diamonds> ds = new arraylist<diamonds>();//方块数组
	private boolean game=false;        //游戏开始
	private int flag = 0;              //游戏状态【暂停:0,继续:1】
	private jlabel jl2;
	private jbutton jb1;
	private int speed = 500;           //速度
	private int score = 0;             //分数
	private int[] scores = new int[4]; //排名
	
	public static void main(string[] args) {
		windows win = new windows();
		win.run();
	}

	public void run(){
		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 = new arraylist<diamonds>();
							map = new map(width_g,height_g);
							joptionpane.showmessagedialog(new jframe().getcontentpane(), "游戏结束!\n您获得【"+score+"】点分数");

							score=0;
							jl2.settext("分数:   "+score);
							jb1.setenabled(true);
							jb1.settext("重新开始");
						}else {
							//消除判断
							score=map.dispel(score);
							jl2.settext("分数:   "+score);
			
							//生成新方块
							diamonds diamonds = new diamonds(width_g);
							ds.add(diamonds);
						}
					}
				}
				repaint();
				thread.sleep(speed);
			}
		}catch(interruptedexception e) {
			e.printstacktrace();
		}
	}
	//窗口加载
	public void init() {
		//界面设置
		this.settitle("俄罗斯方块");    //标题
		this.setsize(width, height); //界面大小
		took = toolkit.getdefaulttoolkit();
		dimension dm = took.getscreensize();
		int swidth = (dm.width - width)/2;
		int sheight = (dm.height - height)/2;
	    this.setlocation(swidth, sheight);
	    
	    //容器
	    jpanel p1 = new jpanel();   //地图
	    jpanel p2 = new jpanel();   //俄罗斯方块控制界面
	    jpanel p3 = new jpanel();   //按钮
	    jpanel p4 = new jpanel();   //说明
	    
	    //图形绘制容器
	    jpanel contentpane =  new paintpanel();
	    setcontentpane(contentpane);
	    
	   //标签
	    jlabel jl1 = new jlabel("俄罗斯方块控制界面");
	    jl2 = new jlabel("分数:   "+score);
	    jlabel jl3 = new jlabel("游戏说明:");
	    
	    //按钮
	    jb1 = new jbutton("游戏开始");
	    jbutton jb2 = new jbutton("难度选择");
	    jbutton jb3 = new jbutton("继续/暂停");
	    jbutton jb4 = new jbutton("游戏退出");
	    jbutton jb5 = new jbutton("高级");
	    jbutton jb6 = new jbutton("中级");
	    jbutton jb7 = new jbutton("低级");
	    jbutton jb8 = new jbutton("显示排名");
	    
	    //文本
	    jtextarea jta = new jtextarea("1.点击【游戏开始】按钮开始游戏。"
	    		+ "\n2.游戏中可随时暂停,使用方向键可继续游戏"
	    		+ "\n3.用键盘操作,\"←\"左移一格;\"→\"右移一格;\"↑\"旋转方块;↓ 方块丢下(方块下落到底)",50,9);
	    jta.setselectioncolor(color.red);
	    jta.seteditable(false);
	    jta.setlinewrap(true);
	    
	    //布局
	    this.setlayout(new borderlayout(5,5));
	    p2.setlayout(new gridlayout(2,1,5,5));
	    p3.setlayout(new gridlayout(10,1,5,5));
	    p4.setlayout(new borderlayout(5,5));
	    
	    //设置边界
	    p2.setborder(borderfactory.createemptyborder(20,20,15,15));
	    
	    //背景颜色
	    p1.setbackground(new color(255,255,255,0));
	    p2.setbackground(new color(255,255,255,0));
	    p3.setbackground(new color(255,255,255,0));
	    p4.setbackground(new color(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(new keyadapter() {
			public void keypressed(keyevent e) {
				//游戏开始时按键有效
				if(ds.size()!=0) {
					int type = ds.get(ds.size()-2).gettype();
					int x = 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);
						}else if(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);
						}else if(type<=9) {
							ds.get(ds.size()-2).settype(type==9?6:++type);
						}else if(type<=10) {
							ds.get(ds.size()-2).settype(10);
						}else if(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);
						}else if(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;
				}
			}
			public void keyreleased(keyevent e) {
				if(e.getkeycode()==keyevent.vk_down) {
					speed = 500;
				}
			}
		});
		
		//游戏开始
		jb1.addmouselistener(new mouseadapter() {
			@override
			public void mouseclicked(mouseevent e) {
				requestfocus();
				//生成第一个方块
				diamonds diamonds = new diamonds(width_g);
				ds.add(diamonds);
				//生成提示方块
				diamonds point = new diamonds(width_g);
				ds.add(point);
				//游戏开始
				game=true;
				flag=1;
				//游戏开始后禁止使用
				jb1.setenabled(false);
			}
		});
		
		//退出
		jb4.addmouselistener(new mouseadapter() {
			@override
			public void mouseclicked(mouseevent e) {
				system.exit(0);
			}
		});	
		
		//暂停/继续
		jb3.addmouselistener(new mouseadapter() {
			@override
			public void mouseclicked(mouseevent e) {
				if(flag==0) {
					flag=1;
				}else {
					flag=0;
				}
				requestfocus();
			}
		});
	}
	
	//重写paintcomponent(图形绘制)
	private class paintpanel extends jpanel{
		@override
		protected void paintcomponent(graphics g) {
			//还原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(int i=0;i<width_g;i++) {
					for(int j=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);
						}
					}
				}
				//绘制可移动的方块
				int type=ds.get(ds.size()-2).gettype();
				int x=ds.get(ds.size()-2).getx();
				int y=ds.get(ds.size()-2).gety();
				color color = ds.get(ds.size()-2).getcolor();
				//绘制提示方块
				int typeo=ds.get(ds.size()-1).gettype();
				int xo=width-100;
				int yo=260;
				color coloro = 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);
					}
			}		
		}
	}
}

方块及相关事件设计类

/*
 * 方块
 */
public class diamonds {
	private int x;        //坐标x
	private int y;        //坐标y
	private color color;  //颜色
	private int type;     //类型
	random rm = new random();
	private boolean status=true;//状态
	private int width;
	
	public diamonds() {
		super();
		// todo auto-generated constructor stub
	}
	
	//方块初始化
	public diamonds(int width) {
		super();
		this.width = width;
		this.x = rm.nextint(width-3);
		this.y = 0;
		//this.color = color.green;
		this.color = new color(rm.nextint(255),rm.nextint(255),rm.nextint(255));
		this.type = rm.nextint(16);
	}

	//向左移动
	public void left(map map) {
		int[][]map_xy = map.getmap();
		if(x!=0) {
			switch(type){
			case 0:if(map_xy[x-1][y]==0) {x--;} break;
			case 1: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;
			
			case 2:if(map_xy[x][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 3:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0 && map_xy[x-1][y+2]==0) {x--;} break;
			case 4:if(map_xy[x-1][y]==0 && map_xy[x][y+1]==0) {x--;} break;
			case 5:if(map_xy[x][y]==0 && map_xy[x-1][y+1]==0 && map_xy[x][y+2]==0) {x--;} break;
			
			case 6:if(map_xy[x][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 7:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 8:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 9:if(map_xy[x-1][y]==0 && map_xy[x][y+1]==0) {x--;} break;
			
			case 10:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			
			case 11:if(map_xy[x+1][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 12:if(map_xy[x-1][y]==0 && map_xy[x][y+1]==0 && map_xy[x][y+2]==0) {x--;} break;
			case 13:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 14:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0 && map_xy[x-1][y+2]==0) {x--;} break;
			
			case 15:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0) {x--;} break;
			case 16:if(map_xy[x-1][y]==0 && map_xy[x-1][y+1]==0 && map_xy[x-1][y+2]==0) {x--;} break;
			case 17:if(map_xy[x-1][y]==0 && map_xy[x+1][y+1]==0) {x--;} break;
			case 18:if(map_xy[x][y]==0 && map_xy[x][y+1]==0 && map_xy[x-1][y+2]==0) {x--;} break;
			}
		}
	}
	//向右移动
	public void right(map map) {
		int[][]map_xy = map.getmap();
		switch(type) {
		case 0: if(x<width-4 && map_xy[x+4][y]==0) {x++;}break;
		
		case 1: 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;
			
		case 2: if(x<width-3 && map_xy[x+2][y]==0 && map_xy[x+3][y+1]==0) {x++;}break;
		
		case 3: 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;
			
		case 4: if(x<width-3 && map_xy[x+3][y]==0 && map_xy[x+2][y+1]==0) {x++;}break;
			
		case 5: 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;
			
		case 6: if(x<width-2 && map_xy[x+2][y]==0 && map_xy[x+2][y+1]==0) {x++;}break;
			
		case 7: if(x<width-2 && map_xy[x+1][y]==0 && map_xy[x+2][y+1]==0) {x++;}break;
			
		case 8: if(x<width-2 && map_xy[x+2][y]==0 && map_xy[x+1][y+1]==0) {x++;}break;
			
		case 9: if(x<width-2 && map_xy[x+2][y]==0 && map_xy[x+2][y+1]==0) {x++;}break;
			
		case 10: if(x<width-2 && map_xy[x+2][y]==0 && map_xy[x+2][y+1]==0) {x++;}break;
		
			
		case 11: if(x<width-3 && map_xy[x+3][y]==0 && map_xy[x+3][y+1]==0) {x++;}break;
			
		case 12: 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;
			
		case 13: if(x<width-3 && map_xy[x+3][y]==0 && map_xy[x+1][y+1]==0) {x++;}break;
		
		case 14: 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;
		
			
		case 15: if(x<width-3 && map_xy[x+1][y]==0 && map_xy[x+3][y+1]==0) {x++;}break;
			
		case 16: 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;
			
		case 17: if(x<width-3 && map_xy[x+3][y]==0 && map_xy[x+3][y+1]==0) {x++;}break;
		
		case 18: 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;
		}
	}
	
	//向下移动
	public void movement(map map){
		int[][]map_xy = map.getmap();
		color[][]color_xy = map.getcolor();
		//向下移动一格
		switch(type) {
		case 0:
			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;
		case 1:
			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;
		case 2:
			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;
		case 3:
			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;
		case 4:
			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;
		case 5:
			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;
		
			
		case 6:
			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;
		case 7:
			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;
		case 8:
			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;
		case 9:
			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;
			
		case 10:
			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;
			
		case 11:
			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;
		case 12:
			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;
		case 13:
			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;
		case 14:
			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;
		case 15:
			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;
		case 16:
			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;
		case 17:
			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;
		case 18:
			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);
	}
	
	public int getx() {
		return x;
	}
	public void setx(int x) {
		this.x = x;
	}
	public int gety() {
		return y;
	}
	public void sety(int y) {
		this.y = y;
	}
	public color getcolor() {
		return color;
	}
	public void setcolor(color color) {
		this.color = color;
	}
	public int gettype() {
		return type;
	}
	public void settype(int type) {
		this.type = type;
	}
	public boolean isstatus() {
		return status;
	}
	public void setstatus(boolean status) {
		this.status = status;
	}
}

总结

通过此次的《俄罗斯方块》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。

java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。

以上就是java实现经典俄罗斯方块游戏的详细内容,更多关于java俄罗斯方块的资料请关注其它相关文章!