JAVA---经典俄罗斯方块游戏完善版

 

进阶版本的欧罗斯经典方块游戏,还没有实现最后满格消格功能,目前 功能:(1)随机方块体下落;(2)控制左右下移动

第一步依然是建立最小的方块类CELL类 

package com.hyxy.oop.day08;

import java.awt.image.BufferedImage;

public class Cell {
	private int row;
	private int col;
	private BufferedImage image;
	public Cell(){}
	public Cell(int row, int col,BufferedImage image) {
		super();
		this.row = row;
		this.col = col;
		this.setImage(image);
	}
	public int getRow() {
		return row;
	}
	public void setRow(int row) {
		this.row = row;
	}
	public int getCol() {
		return col;
	}
	public void setCol(int col) {
		this.col = col;
	}
	public BufferedImage getImage() {
		return image;
	}
	public void setImage(BufferedImage image) {
		this.image = image;
	}	
	public void left(){
		col--;
	}
	public void drop(){
		row++;
	}
	public void right(){
		col++;
	}
	public String toString() {
		return "Cell [row=" + row + ", col=" + col + "]";
	}	
}

第二步写Tetromino父类,并在其内部定义其子类以及一个随机取方块体的方法

package com.hyxy.oop.day08;

public class Tetromino {
	Cell[] cells = new Cell[4];

	public void moveLeft() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].left();
		}
	}

	public void moveDrop() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].drop();
		}
	}

	public void moveRight() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].right();
		}
	}

	/** 添加一个方法:用于随机产生一种子类型对象 */
	public static Tetromino randomOne() {
		Tetromino one = null;
		int num = (int) (Math.random() * 7);
		switch (num) {
		case 0:
			one = new T();
			break;
		case 1:
			one = new I();
			break;
		case 2:
			one = new O();
			break;
		case 3:
			one = new S();
			break;
		case 4:
			one = new Z();
			break;
		case 5:
			one = new J();
			break;
		case 6:
			one = new L();
			break;
		}
		return one;
	}

	/** 使用内部类定义七个子类型 */
	static class T extends Tetromino {
		public T() {
			cells[0] = new Cell(0, 4, TestTetromino.T);
			cells[1] = new Cell(0, 3, TestTetromino.T);
			cells[2] = new Cell(0, 5, TestTetromino.T);
			cells[3] = new Cell(1, 4, TestTetromino.T);
		}
	}

	static class J extends Tetromino {
		public J() {
			cells[0] = new Cell(0, 4, TestTetromino.J);
			cells[1] = new Cell(0, 3, TestTetromino.J);
			cells[2] = new Cell(0, 5, TestTetromino.J);
			cells[3] = new Cell(1, 5, TestTetromino.J);
		}
	}

	static class L extends Tetromino {
		public L() {
			cells[0] = new Cell(0, 4, TestTetromino.L);
			cells[1] = new Cell(0, 3, TestTetromino.L);
			cells[2] = new Cell(0, 5, TestTetromino.L);
			cells[3] = new Cell(1, 3, TestTetromino.L);
		}
	}

	static class O extends Tetromino {
		public O() {
			cells[0] = new Cell(0, 3, TestTetromino.O);
			cells[1] = new Cell(0, 4, TestTetromino.O);
			cells[2] = new Cell(1, 3, TestTetromino.O);
			cells[3] = new Cell(1, 4, TestTetromino.O);
		}
	}

	static class I extends Tetromino {
		public I() {
			cells[0] = new Cell(0, 4, TestTetromino.I);
			cells[1] = new Cell(0, 3, TestTetromino.I);
			cells[2] = new Cell(0, 5, TestTetromino.I);
			cells[3] = new Cell(0, 6, TestTetromino.I);
		}
	}

	static class S extends Tetromino {
		public S() {
			cells[0] = new Cell(0, 4, TestTetromino.S);
			cells[1] = new Cell(0, 5, TestTetromino.S);
			cells[2] = new Cell(1, 3, TestTetromino.S);
			cells[3] = new Cell(1, 4, TestTetromino.S);
		}
	}

	static class Z extends Tetromino {
		public Z() {
			cells[0] = new Cell(0, 4, TestTetromino.Z);
			cells[1] = new Cell(0, 3, TestTetromino.Z);
			cells[2] = new Cell(1, 4, TestTetromino.Z);
			cells[3] = new Cell(1, 5, TestTetromino.Z);
		}
	}

}

第三部,建立测试类,用静态块封存图片的抓取和打印功能,調用畫板功能和窗口功能

package com.hyxy.oop.day08;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * TestTetromino是一个画板
 * */
public class TestTetromino extends JPanel{
	//常量矩形的宽度,即方格的宽度
	public static final int CELL_SIZE = 26;
	
	//成员变量
	Tetromino currentOne = Tetromino.randomOne();
	
	public static BufferedImage I;
	public static BufferedImage T;
	public static BufferedImage O;
	public static BufferedImage S;
	public static BufferedImage Z;
	public static BufferedImage L;
	public static BufferedImage J;
	public static BufferedImage gameover;
	public static BufferedImage tetris;
	
	static{
		try {
			//read()读取硬盘上的图片,
			//getRsource(String path):path是图片的相对当前包的位置
			I = ImageIO.read(TestTetromino.class.getResource("I.png" ));
			T= ImageIO.read(TestTetromino.class.getResource("T.png" ));
			O = ImageIO.read(TestTetromino.class.getResource("O.png" ));
			S = ImageIO.read(TestTetromino.class.getResource("S.png" ));
			Z = ImageIO.read(TestTetromino.class.getResource("Z.png" ));
			L = ImageIO.read(TestTetromino.class.getResource("L.png" ));
			J = ImageIO.read(TestTetromino.class.getResource("J.png" ));
			gameover = ImageIO.read(TestTetromino.class.getResource("game-over.png" ));
			tetris = ImageIO.read(TestTetromino.class.getResource("tetris.png" ));
		} catch (Exception e) {
			//打印栈中的信息
			e.printStackTrace();
		}
	}
	/**重写绘制功能,此功能画板会默认调用*/
	public void paint(Graphics g){
		g.drawImage(tetris,0,0,null);
		//平移坐标轴
		g.translate(15, 15);
		paintWall(g);
		paintCurrentOne(g);
	}
	/**绘制当前正在下落的四方块*/
	private void paintCurrentOne(Graphics g) {
		//获取正在下落的方块组合内的数组对象
		Cell[] cells = currentOne.cells;
		for(int i = 0;i<cells.length;i++){
			Cell cell = cells[i];
			int col = cell.getCol();
			int row = cell.getRow();
			g.drawImage(cell.getImage(), col*CELL_SIZE, row*CELL_SIZE, null);
		}
		
	}
	/**绘制一个墙:20行,10列的一个表格*/
	public void paintWall(Graphics g){
		for(int i = 0;i<20;i++){
			for(int j = 0;j<10;j++){
				//横坐标为j,纵坐标是i				
				g.drawRect(j*CELL_SIZE, i*CELL_SIZE,CELL_SIZE , CELL_SIZE);
			}
		}		
	}
	public void action(){
		/**
		 * 定义监听器对象,用于监听
		 * 键盘按钮(事件)
		 * 
		 * */
		KeyListener l = new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				int code = e.getKeyCode();
				System.out.println("code:"+code);
				switch(code){
				case 37:
				    currentOne.moveLeft();break;
				case 39:
					currentOne.moveRight();break;
				case 40:
					currentOne.moveDrop();break;
				}
				//重新绘制
				repaint();
			}
		};
		//面板添加键盘监听时间
		this.addKeyListener(l);
		//面板设置成屏幕焦点
		this.requestFocus();		
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			currentOne.moveDrop();			
			//重新绘制整个画板上的内容
			repaint();
		}	
	}	
	private void addKeyListener(int k) {
		// TODO Auto-generated method stub
		
	}
	public static void main(String[] args) {
		//创建一个游戏窗口
		JFrame frame = new JFrame("俄罗斯方块");
		//设计窗口的大小
		frame.setSize(545, 605);
		//创建一个画板对象
		TestTetromino panel = new TestTetromino();
		//将画板嵌入窗口
		frame.add(panel);		
		//设置窗口居中
		frame.setLocationRelativeTo(null);	
		//设置窗口关闭即程序停止
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		//设置可见性
		frame.setVisible(true);		
		panel.action();
	}
}

================================================================================================

後續程序歡迎一同探討。。。。。游戲圖片可以網上下載也可以私信傳送。。。。

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值