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

展开全部

代码多了,传不过去 分开给你传吧

还是发你邮箱吧

下面是一部分代码

代码如下:

package com.tarena.tetris;//包名倒636f70793231313335323631343130323136353331333335323362写

//导包

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[i].drop();//使每一个方块下落

}

}

public void moveLeft() {

for(int i=0; i

cells[i].moveLeft();//使每一个方块左移

}

}

public void moveRight() {

for(int i=0; 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)};

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值