Java 俄罗斯方块

游戏规则

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

整体代码分为三个模块:方格模块,七种图形模块,俄罗斯方块主模块。

小方块类:Cell

package com.zhao.demo.block;

import java.awt.image.BufferedImage;

import java.util.Objects;

/**

* @author xiaoZhao

* @date 2022/5/7

* @describe

* 小方块类

* 方法: 左移、右移、下落

*/

public class Cell {

// 行

private int row;

// 列

private int col;

private BufferedImage image;

public Cell() {

}

public Cell(int row, int col, BufferedImage image) {

this.row = row;

this.col = col;

this.image = 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;

}

@Override

public String toString() {

return "Cell{" +

"row=" + row +

", col=" + col +

", image=" + image +

'}';

}

@Override

public boolean equals(Object o) {

if (this == o) {

return true;

}

if (!(o instanceof Cell)) {

return false;

}

Cell cell = (Cell) o;

return getRow() == cell.getRow() &&

getCol() == cell.getCol() &&

Objects.equals(getImage(), cell.getImage());

}

@Override

public int hashCode() {

return Objects.hash(getRow(), getCol(), getImage());

}

//左移动一格

public void left(){

col--;

}

//右移动一格

public void right(){

col++;

}

//下移动一格

public void down(){

row++;

}

}

七种图形类:I、J、L、O、S、T、Z

I

package com.zhao.demo.shape;

import com.zhao.demo.App.Tetris;

import com.zhao.demo.block.Cell;

import com.zhao.demo.block.Tetromino;

/**

* @author xiaoZhao

* @date 2022/5/11

* @describe

*/

public class I extends Tetromino {

public I() {

cells0 = new Cell(0,4, Tetris.I);

cells1 = new Cell(0,3, Tetris.I);

cells2 = new Cell(0,5, Tetris.I);

cells3 = new Cell(0,6, Tetris.I);

//共有两种旋转状态

states =new State2;

//初始化两种状态的相对坐标

states0=new State(0,0,0,-1,0,1,0,2);

states1=new State(0,0,-1,0,1,0,2,0);

}

}

J

package com.zhao.demo.shape;

import com.zhao.demo.App.Tetris;

import com.zhao.demo.block.Cell;

import com.zhao.demo.block.Tetromino;

/**

* @author xiaoZhao

* @date 2022/5/11

* @describe

*/

public class J extends Tetromino {

public J() {

cells0 = new Cell(0,4, Tetris.J);

cells1 = new Cell(0,3, Tetris.J);

cells2 = new Cell(0,5, Tetris.J);

cells3 = new Cell(1,5, Tetris.J);

states=new State4;

states0=new State(0,0,0,-1,0,1,1,1);

states1=new State(0,0,-1,0,1,0,1,-1);

states2=new State(0,0,0,1,0,-1,-1,-1);

states3=new State(0,0,1,0,-1,0,-1,1);

}

}

L

package com.zhao.demo.shape;

import com.zhao.demo.App.Tetris;

import com.zhao.demo.block.Cell;

import com.zhao.demo.block.Tetromino;

/**

* @author xiaoZhao

* @date 2022/5/11

* @describe

*/

public class L extends Tetromino {

public L() {

cells0 = new Cell(0,4, Tetris.L);

cells1 = new Cell(0,3, Tetris.L);

cells2 = new Cell(0,5, Tetris.L);

cells3 = new Cell(1,3, Tetris.L);

states=new State4;

states0=new State(0,0,0,-1,0,1,1,-1);

states1=new State(0,0,-1,0,1,0,-1,-1);

states2=new State(0,0,0,1,0,-1,-1,1);

states3=new State(0,0,1,0,-1,0,1,1);

}

}

O

package com.zhao.demo.shape;

import com.zhao.demo.App.Tetris;

import com.zhao.demo.block.Cell;

import com.zhao.demo.block.Tetromino;

/**

* @author xiaoZhao

* @date 2022/5/11

* @describe

*/

public class O extends Tetromino {

public O() {

cells0 = new Cell(0, 4, Tetris.O);

cells1 = new Cell(0, 5, Tetris.O);

cells2 = new Cell(1, 4, Tetris.O);

cells3 = new Cell(1, 5, Tetris.O);

//无旋转状态

states = new State0;

}

}

S

package com.zhao.demo.shape;

import com.zhao.demo.App.Tetris;

import com.zhao.demo.block.Cell;

import com.zhao.demo.block.Tetromino;

/**

* @author xiaoZhao

* @date 2022/5/11

* @describe

*/

public class S extends Tetromino {

public S() {

cells0 = new Cell(0,4, Tetris.S);

cells1 = new Cell(0,5, Tetris.S);

cells2 = new Cell(1,3, Tetris.S);

cells3 = new Cell(1,4, Tetris.S);

//共有两种旋转状态

states =new State2;

//初始化两种状态的相对坐标

states0=new State(0,0,0,1,1,-1,1,0);

states1=new State(0,0,1,0,-1,-1,0,-1);

}

}

T

package com.zhao.demo.shape;

import com.zha

相关推荐
学编程就要猛4 分钟前
Spring AI Alibaba(上)
java·人工智能·spring·springaialibaba
imDwAaY17 分钟前
Spring Boot的核心配置文件一览及其加载顺序
java·spring boot·后端
zhangx1234_19 分钟前
javaEE 多线程1
java·linux·服务器
鬼手点金24 分钟前
opencode-crawl4ai使用建议
java·开发语言·人工智能·python·机器学习
专业程序开发源38 分钟前
springboot校园志愿者管理系统28562-计算机课程设计、毕业设计
java·spring boot·后端·python·django·php·课程设计
thefool11226642 分钟前
多线程 2
java
daidaidaiyu10 小时前
ThingsBoard 集群的核心逻辑源码分析
java
我可能是个假开发11 小时前
FTP Unicode 文件名导致 `550` 的问题分析与解决方案
java·开发语言·spring
caoerzhong11 小时前
中小企业上 WMS 该先上哪几块:JeeWMS 开源 Java 仓库管理系统的分批上线清单
java·python·开源
【JAVA】玩家12 小时前
Spring核心原理全解析:从零到生产实战
java·后端·spring