java俄罗斯方块制作方法 全面哦

 我来答
百度网友bcd3744
2014-06-03
知道答主
回答量:11
采纳率:0%
帮助的人:5.8万
展开全部
代码多了,传不过去 分开给你传吧
还是发你邮箱吧
下面是一部分代码

代码如下:
package com.tarena.tetris;//包名倒写
//导包
import java.awt.image.BufferedImage;
/**
* 格子类
*/
public class Cell {
//1定义属性
private int row;//行
private int col;//列
private BufferedImage image;//图片
//2构造器
public Cell(int row, int col, BufferedImage image) {
super();
this.row = row;
this.col = col;
this.image = image;
}
//3属性访问方法
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;
}
//5移动方法
public void drop() {
row++;
}
public void moveLeft() {
col--;
}
public void moveRight() {
col++;
}
//4重写toString
public String toString() {
return row + "," + col;
}
}

-----------------------------------------------------------------------
package com.tarena.tetris;//包名倒写
//导包
import java.util.Arrays;
import java.util.Random;
/**
* 四格方块类:包含七个子类T I J L S Z O
*/
public abstract class Tetromino {
//1创建四个格子
protected Cell[] cells = new Cell[4];
//9
protected State[] states;//旋转状态
protected int index = 10000;//旋转状态的序号 state[index%state.length]
//8旋转 内部类
protected class State {
int row0,col0,row1,col1,row2,col2,row3,col3;//转一圈 8个数
//构造器
public State(int row0, int col0, int row1, int col1, int row2,
int col2, int row3, int col3) {
super();
this.row0 = row0;
this.col0 = col0;
this.row1 = row1;
this.col1 = col1;
this.row2 = row2;
this.col2 = col2;
this.row3 = row3;
this.col3 = col3;
}
}
//10
/** 向右转 */
public void rotateRight() {
//1取得变换的下个数据状态state[n]
index++;
State s = states[index%states.length];
//2取得当前轴的row,col
Cell o = cells[0];
int row = o.getRow();
int col = o.getCol();
//3旋转以后的数据=(row,col) + state[n]
cells[1].setRow(row + s.row1);
cells[1].setCol(col + s.col1);
cells[2].setRow(row + s.row2);
cells[2].setCol(col + s.col2);
cells[3].setRow(row + s.row3);
cells[3].setCol(col + s.col3);
}
//11
/** 向左转 */
public void rotateLeft() {
//1取得变换的下个数据状态state[n]
index--;
State s = states[index%states.length];
//2取得当前轴的row,col
Cell o = cells[0];
int row = o.getRow();
int col = o.getCol();
//3旋转以后的数据=(row,col) + state[n]
cells[1].setRow(row + s.row1);
cells[1].setCol(col + s.col1);
cells[2].setRow(row + s.row2);
cells[2].setCol(col + s.col2);
cells[3].setRow(row + s.row3);
cells[3].setCol(col + s.col3);
}
//2工厂方法,随机产生四个格子
public static Tetromino randomOne() {
//4随机产生七种四格方块
Random random = new Random();
int type = random.nextInt(7);
switch(type) {
case 0 : return new T();
case 1 : return new I();
case 2 : return new J();
case 3 : return new L();
case 4 : return new S();
case 5 : return new Z();
case 6 : return new O();
}
return null;
}
//5移动方法
public void softDrop() {
for(int i=0; i<cells.length; i++) {//迭代每一个方块
cells[i].drop();//使每一个方块下落
}
}
public void moveLeft() {
for(int i=0; i<cells.length; i++) {//迭代每一个方块
cells[i].moveLeft();//使每一个方块左移
}
}
public void moveRight() {
for(int i=0; i<cells.length; i++) {//迭代每一个方块
cells[i].moveRight();//使每一个方块右移
}
}
//6重写toString
public String toStrig() {
return Arrays.toString(cells);
}
}
//3创建子类T继承父类Tetromino
class T extends Tetromino {
//7
public T() {
cells[0] = new Cell(0,4,Tetris.T);
cells[1] = new Cell(0,3,Tetris.T);
cells[2] = new Cell(0,5,Tetris.T);
cells[3] = new Cell(1,4,Tetris.T);
//12
states = new State[4];
states[0] = new State(0,0, 0,-1, 0,1, 1,0);
states[1] = new State(0,0, -1,0, 1,0, 0,-1);
states[2] = new State(0,0, 0,1, 0,-1, -1,0);
states[3] = new State(0,0, 1,0, -1,0, 0,1);
}
}
//3创建子类I继承父类Tetromino
class I extends Tetromino {
//7
public I() {
cells[0] = new Cell(0,4,Tetris.I);
cells[1] = new Cell(0,3,Tetris.I);
cells[2] = new Cell(0,5,Tetris.I);
cells[3] = new Cell(0,6,Tetris.I);
//12
states = new State[] {new State(0,0, 0,1, 0,-1, 0,-2),
new State(0,0, -1,0, 1,0, 2,0)};
}
}
//3创建子类J继承父类Tetromino
class J extends Tetromino {
//7
public J() {
cells[0] = new Cell(0,4,Tetris.J);
cells[1] = new Cell(0,3,Tetris.J);
cells[2] = new Cell(0,5,Tetris.J);
cells[3] = new Cell(1,5,Tetris.J);
//12
states = new State[]{
new State(0,0, 0,-1, 0,1, 1,1),
new State(0,0, -1,0, 1,0, 1,-1),
new State(0,0, 0,1, 0,-1, -1,-1),
new State(0,0, 1,0, -1,0, -1,1)};
}
}
//3创建子类L继承父类Tetromino
class L extends Tetromino {
//7
public L() {
cells[0] = new Cell(0,4,Tetris.L);
cells[1] = new Cell(0,3,Tetris.L);
cells[2] = new Cell(0,5,Tetris.L);
cells[3] = new Cell(1,3,Tetris.L);
//12
states = new State[]{
new State(0,0, 0,-1, 0,1, 1,-1),
new State(0,0, -1,0, 1,0, -1,-1),
new State(0,0, 0,1, 0,-1, -1,1),
new State(0,0, 1,0, -1,0, 1,1)};
}
}
//3创建子类S继承父类Tetromino
class S extends Tetromino {
//7
public S() {
cells[0] = new Cell(0,4,Tetris.S);
cells[1] = new Cell(0,5,Tetris.S);
cells[2] = new Cell(1,3,Tetris.S);
cells[3] = new Cell(1,4,Tetris.S);
//12
states = new State[]{
new State(0,0, 0,1, 1,-1, 1,0),
new State(0,0, -1,0, 1,1, 0,1)};
}
}
//3创建子类Z继承父类Tetromino
class Z extends Tetromino {
//7
public Z() {
cells[0] = new Cell(1,4,Tetris.Z);
cells[1] = new Cell(0,3,Tetris.Z);
cells[2] = new Cell(0,4,Tetris.Z);
cells[3] = new Cell(1,5,Tetris.Z);
//12
states = new State[]{
new State(0,0, -1,-1, -1,0, 0,1),
new State(0,0, -1,1, 0,1, 1,0)};
}
}
//3创建子类O继承父类Tetromino
class O extends Tetromino {
//7
public O() {
cells[0] = new Cell(0,4,Tetris.O);
cells[1] = new Cell(0,5,Tetris.O);
cells[2] = new Cell(1,4,Tetris.O);
cells[3] = new Cell(1,5,Tetris.O);
//12
states = new State[]{
new State(0,0, 0,1, 1,0, 1,1),
new State(0,0, 0,1, 1,0, 1,1)};
}
}
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式