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() {

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);

//共有两种旋转状态

states =new State[2];

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

states[0]=new State(0,0,0,-1,0,1,0,2);

states[1]=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() {

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);

states=new State[4];

states[0]=new State(0,0,0,-1,0,1,1,1);

states[1]=new State(0,0,-1,0,1,0,1,-1);

states[2]=new State(0,0,0,1,0,-1,-1,-1);

states[3]=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() {

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);

states=new State[4];

states[0]=new State(0,0,0,-1,0,1,1,-1);

states[1]=new State(0,0,-1,0,1,0,-1,-1);

states[2]=new State(0,0,0,1,0,-1,-1,1);

states[3]=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() {

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);

//无旋转状态

states = new State[0];

}

}

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() {

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);

//共有两种旋转状态

states =new State[2];

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

states[0]=new State(0,0,0,1,1,-1,1,0);

states[1]=new State(0,0,1,0,-1,-1,0,-1);

}

}

T

package com.zhao.demo.shape;

import com.zha

相关推荐
Hy行者勇哥11 分钟前
物联网软件开发过程中,数据流图(DFD),用例图,类图,活动图,序列图,状态图,实体关系图(ERD),BPMN(业务流程建模)详解分析
java·物联网·struts
Miracle65816 分钟前
从 0 到 1 开发校园二手交易系统:飞算 JavaAI 全流程实战
java
A尘埃32 分钟前
Java+Python混合微服务OCR系统设计
java·python·微服务·混合
Seven971 小时前
剑指offer-22、从上往下打印⼆叉树
java
A尘埃1 小时前
企业级Java项目金融应用领域——保险系统(补充)
java·金融·保险系统
冬天vs不冷1 小时前
Java基础(九):Object核心类深度剖析
java·开发语言·python
悟空聊架构2 小时前
我的网站被攻击了,被干掉了 120G 流量,还在持续攻击中...
java·前端·架构
Dajiaonew2 小时前
Spring AI RAG 检索增强 应用
java·人工智能·spring·ai·langchain
IT古董6 小时前
第四章:大模型(LLM)】06.langchain原理-(3)LangChain Prompt 用法
java·人工智能·python
轻抚酸~9 小时前
小迪23年-32~40——java简单回顾
java·web安全