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

目录
  • 前言
  • 主要设计
  • 功能截图
  • 代码实现
  • 总结

前言

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

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

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

主要需求

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

主要设计

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

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

3、设计不同的方块

4、设计方块下落的速度

5、设计分数系统

功能截图

游戏启动页

开始游戏

游戏结束

代码实现

窗口设计类

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俄罗斯方块的资料请关注我们其它相关文章!

(0)

相关推荐

  • java实现简单的俄罗斯方块

    本文实例为大家分享了java实现简单俄罗斯方块的具体代码,供大家参考,具体内容如下 结合网上的资料刚做完课程设计,具体代码如下: public class TetrisPanel extends JPanel{     private final int[][] map = new int[13][23];// map[列号][行号].真正的方块区是:21行*10列.边框(2列,1行)     // 方块的形状:     // 第一维代表方块类型(包括7种:S.Z.L.J.I.O.T)    

  • java实现俄罗斯方块小程序

    这是java课最后做的课程设计,由于java是初学的,所以做的时候有参考一些技术大牛的博客,在此表示感谢. 发在这里跟大家交流学习一下. 如需要完整工程文件.说明文档以及可运行jar文件,下载地址:点击打开链接 RussianBlocksGame.java package RussiaBlocksGame; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.Bor

  • java实现俄罗斯方块小游戏

    本文实例为大家分享了java实现俄罗斯方块的具体代码,供大家参考,具体内容如下 使用一个二维数组保存游戏的地图: // 游戏地图格子,每个格子保存一个方块,数组纪录方块的状态 private State map[][] = new State[rows][columns]; 游戏前先将所有地图中的格子初始化为空: /* 初始化所有的方块为空 */ for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length;

  • Java实现简易俄罗斯方块

    本文实例为大家分享了Java实现简易俄罗斯方块的具体代码,供大家参考,具体内容如下 一.将对象抽象为类 首先考虑俄罗斯方块游戏中含有哪些具体的对象,对象中含有哪些具体属性和方法,然后用代码来实现. 建立如下类: Cell类:代表最小的方格单位,构成7种图形的最基本图形. 含有row(行号),col(列号),image(对应的图片)属性, 含有left(左移),right(右移),drop(下落)方法. Tetromino类:代表由4个最小方格构成的7种图形的合集. 含有cells(四个方块)属性

  • Java实现俄罗斯方块游戏简单版

    本文实例为大家分享了Java实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 游戏页面效果如下: 俄罗斯方块游戏本身的逻辑: 俄罗斯方块游戏的逻辑是比较简单的.它就类似于堆砌房子一样,各种各样的方地形状是不同的.但是,俄罗斯方块游戏的界面被等均的分为若干行和若干列,因此方块的本质就是占用了多少个单元. 首先来考虑一下数据的问题.对于界面来说,需要一个二维的 int 型数组,它保存着那些地方应该有着色,哪些没有:然后是方块本身,尽管它们的形状不统一,但是它们可以用一个4X4比例的方块所包围,

  • java实现俄罗斯方块游戏

    本文实例为大家分享了java实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 1.功能需求 2.软件功能架构图 3.界面设计 4.程序逻辑图 5.实现代码 创建控制面板并添加按钮 初始化界面 添加事件监听 创建方块 实现对方块操作 游戏主类,实现游戏控制 功能需求 1. 在二维平面里面用各种随机产生的方块堆积木,每满一行消去一行,当达到顶部时,游戏结束. 2. 玩家通过方向键来控制方块转动,左移,右移和直落. 3. 每种类型的方块都有颜色. 4. 玩家能够在玩的过程中给出分数,分数是由方块

  • Java实现俄罗斯方块小游戏源码

    本文实例为大家分享了Java实现俄罗斯方块小游戏的具体代码,供大家参考,具体内容如下 一.最终效果 二.功能需求 1. 在二维平面里面用各种随机产生的方块堆积木,每满一行消去一行,当达到顶部时,游戏结束.2. 通过方向键来控制方块转动,左移,右移和直落.3. 方块下落统一设置蓝色,接触底部变粉色.4. 计算分数,分数是由方块的类型决定的,每堆积一个方块就把分数累加到总分中.5. 游戏有开始.重新开始.降低提高级数(速度).暂停.退出 三.程序实现 这个是最基础的方块素材 package 俄罗斯方

  • java代码实现俄罗斯方块

    本文实例为大家分享了java实现俄罗斯方块的具体代码,供大家参考,具体内容如下 俄罗斯方块设计思想 俄罗斯方块都从小玩到大吧,什么规则大家都知道了吧,以前感觉那玩意贼好玩,但是就是老赢不了,现在学会了自己写一个天天练! 键盘操作: 左键:左移: 右键:右移: 上键:变换造型 下键:加速下掉(没毛病吧,没有继续整) 任意一行的方块满格,这一行就消除,消除一行方块得10分,目前小主我还没有设置关卡,各位喜欢的宝宝们可以自己设置关卡哦: 那么那些方块的造型到底从哪里来的呢,那就是我们自己设计的,常见的

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

    目录 前言 主要设计 功能截图 代码实现 总结 前言 俄罗斯方块是一个最初由阿列克谢帕吉特诺夫在苏联设计和编程的益智类视频游戏. <俄罗斯方块>的基本规则是移动.旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分. 用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 由小方块组成的不同形状的板块陆续从屏幕上方落下来,玩家通过调整板块的位置和方向,使它们在屏幕底部拼出完整的一条或几条.这些完整的横条会随即消失,给新落下来的板块腾出

  • Java编程经典小游戏设计-打砖块小游戏源码

    [程序中使用的数据结构和符号说明] HitBrick类 GreenBallThread控制小球路线 xUp,yUp,bouncing定义变量存储16位数值形式 x,y小球坐标 xDx,yDy坐标增量 MAX_X,MAX_Y坐标最大值 renew初始化 label标签 Rx,Ry横板坐标 Brick[]砖块 ball小球 HitBrick()定义小球横板及砖块位置坐标 keyPressd(keyEent)定义小球启动键(按空格键启动) keyReleased(keyEvent)接收键盘事件侦听器接

  • Java实现经典大富翁游戏的示例详解

    目录 前言 主要设计 功能截图 代码实现 总结 前言 大富翁,又名地产大亨.是一种多人策略图版游戏.参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地.建楼以赚取租金.英文原名monopoly意为“垄断”,因为最后只得一个胜利者,其余均破产收场. <大富翁>游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 可多人参与的大富翁游戏,玩家有初始资金,通过掷骰子,玩家移动指定骰子点数步骤,根据对应格子上的交易策略,来决定是赚钱还是亏钱,其他玩家破

  • JAVA实现经典扫雷游戏的示例代码

    目录 前言 主要设计 功能截图 代码实现 总结 前言 windows自带的游戏<扫雷>是陪伴了无数人的经典游戏,本程序参考<扫雷>的规则进行了简化,用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 1.要有难度等级,初级,中级,高级 2.由玩家逐个翻开方块,以找出所有地雷为最终游戏目标.如果玩家翻开的方块有地雷,则游戏结束 3.游戏主区域由很多个方格组成.使用鼠标左键随机点击一个方格,方格即被打开并显示出方格中的数字:方格中数字则表示其

  • 利用Jetpack Compose实现经典俄罗斯方块游戏

    目录 可组合函数 游戏机身 - TetrisBody 游戏按钮 - TetrisButton 游戏屏幕 - TetrisScreen 调度器 - TetrisViewModel 项目地址 你的童年是否有俄罗斯方块呢,本文就来介绍如何通过 Jetpack Compose 实现一个俄罗斯方块 ~~ 先看下效果图,功能还是挺完善的 就我自己的体验来说,使用 Compose 开发的应用我感受不到和 Android 原生开发之间有什么性能差异,但 Compose 在开发难度上会低很多 Google 官网上

  • JAVA实现经典游戏坦克大战的示例代码

    目录 前言 主要设计 功能截图 代码实现 总结 前言 小时候大家都玩过坦克大战吧,熟悉的旋律和丰富的关卡陪伴了我们一整个寒暑假,还记得传说中的经典坦克大战 吗?那些怀旧的记忆,伴随着我们一起走过来的经典坦克大战,刚开始那战战兢兢,屡屡被敌人坦克击毁的情景历历在目.现在好了,再也不用担心敌人坦克了,可 以横冲直撞,横扫敌人坦克了.快哉!!! <坦克大战>游戏以坦克战斗为主题,用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想. 主要需求 可以生成不同的地图,消灭地

  • Java实现经典游戏复杂迷宫

    目录 前言 主要设计 功能截图 代码实现 总结 前言 人类建造迷宫已有5000年的历史.在世界的不同文化发展时期,这些奇特的建筑物始终吸引人们沿着弯弯曲曲.困难重重的小路吃力地行走,寻找真相.迷宫类小游戏应运而生.在游戏中,迷宫被表现为冒险舞台里,藏有各式各样奇妙与谜题或宝藏的危险区域.型态有洞窟.人工建筑物.怪物巢穴.密林或山路等.迷宫内有恶徒或凶猛的生物(真实存在或想像物体都有)徘徊,其中可能会有陷阱.不明设施.遗迹等. <复杂迷宫>游戏是用java语言实现,采用了swing技术进行了界面

  • Java+Swing实现经典五子棋游戏

    目录 前言 主要需求 主要设计 功能截图 代码实现 总结 前言 五子棋是世界智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏,是世界智力运动会竞技项目之一,通常双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成5子连线者获胜. 棋具与围棋通用,起源于中国上古时代的传统黑白棋种之一.主要流行于华人和汉字文化圈的国家以及欧美一些地区,是世界上最古老的棋. 容易上手,老少皆宜,而且趣味横生,引人入胜:不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性. 用java语言实现

随机推荐