进阶版本的欧罗斯经典方块游戏,还没有实现最后满格消格功能,目前 功能:(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
================================================================================================
後續程序歡迎一同探討。。。。。游戲圖片可以網上下載也可以私信傳送。。。。