俄罗斯方块中的形状旋转变形算法 java 动画演示

Test.java:



import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class Test {

     /*
            0  0  0  0  0  0  0  0  0  0  0  0
            0  0  0  0  0  0  0  0  0  0  0  0
            0  0  0  0  0  0  1  1  0  0  0  0
            0  0  0  0  0  0  1  0  0  0  0  0
            0  0  0  1  0  0  1  0  0  0  0  0
            0  0  0  1  1  1  1  1  1  1  0  0
            0  0  0  0  0  0  1  0  0  1  0  0
            0  0  0  0  0  0  1  0  0  0  0  0
            0  0  0  0  0  1  1  0  0  0  0  0
            0  0  0  0  0  0  0  0  0  0  0  0
            0  0  0  0  0  0  0  0  0  0  0  0

            中心点:5行6列
            5行6列             5行6列               5行6列
            4行6列             5行7列               6行6列
            3行6列  ==》        5行8列     ==》      7行6列
            2行6列             5行9列               8行6列
            2行7列             6行9列               8行5列


            0行0列              0行0列              0行0列
            -1行0列             0行1列              1行0列
            -2行0列             0行2列              2行0列
            -3行0列             0行3列              3行0列
            -3行1列             1行3列              3行-1列
         */
    public static void main(String[] args) throws Exception {

        ShapeVO shape1 = new ShapeVO();
        List<PointVO> points = new ArrayList<>();

        points.add( new PointVO( 4,7 ) );
        points.add( new PointVO( 5,7 ) );
        points.add( new PointVO( 5,6 ) );
        points.add( new PointVO( 5,8 ) );

        shape1.setPoints( points );

        // 将 shape1 按照一个点顺时针旋转90度得到 shape2,输出 shape1和 shape2
        PointVO centerPoint = new PointVO( 6,7 );
        ShapeVO shape2 = rotate90DegreesTowardsClockDirection( shape1,centerPoint );
        ShapeVO shape3 = rotate90DegreesTowardsClockDirection(shape2, centerPoint);
        ShapeVO shape4 = rotate90DegreesTowardsClockDirection(shape3, centerPoint);

        drawShape( 20,20,shape1,shape2,shape3,shape4,"C:\\E\\xxx.png" );
    }

    private static void printShape(ShapeVO shape) {
        List<PointVO> points = shape.getPoints();
        for( PointVO point:points ){
            System.out.println( point.getRowNum() + " 行 " + point.getColNum() + " 列" );
        }
    }

    /**
     * 将指定形状以指定点作为中心点顺时针旋转90度
     * @param shape
     * @param centerPoint
     * @return
     */
    private static ShapeVO rotate90DegreesTowardsClockDirection(ShapeVO shape, PointVO centerPoint) {
        //  遍历 shape中的每个点,让每个点的 rowNum、colNum 分别减去 centerPoint 的   rowNum、colNum 得到新的 rowNum、colNum
        // 然后交换  rowNum、colNum 获得新的  rowNum、colNum,然后 新的 colNum 乘以-1获得新的 colNum
        List<PointVO> points = shape.getPoints();
        ShapeVO shape_new = new ShapeVO();
        List<PointVO> points_new = new ArrayList<>();
        for ( PointVO point:points ){
            int rowNum = point.getRowNum() - centerPoint.getRowNum();
            int colNUm = point.getColNum() - centerPoint.getColNum();

            int temp = rowNum;
            rowNum = colNUm;
            colNUm = temp;

            colNUm = -1 * colNUm;

            rowNum = rowNum + centerPoint.getRowNum();
            colNUm = colNUm + centerPoint.getColNum();

            PointVO point_new = new PointVO(rowNum, colNUm);
            points_new.add( point_new );
        }
        shape_new.setPoints( points_new );
        return shape_new;
    }

    public static void  drawShape( int rowCount,int colCount,ShapeVO shape1,ShapeVO shape2,ShapeVO shape3,ShapeVO shape4,String outputPath ) throws IOException {
        int width = 500; // 图片宽度
        int height = 500; // 图片高度
        int cellWidth = width / colCount; // 格子宽度
        int cellHeight = height / rowCount; // 格子高度

        // 创建一个BufferedImage对象,用于绘制图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();

        // 绘制网格
        g.setColor(Color.WHITE);
        for (int i = 0; i <= rowCount; i++) {
            int y = i * cellHeight;
            g.drawLine(0, y, width, y);
        }
        for (int i = 0; i <= colCount; i++) {
            int x = i * cellWidth;
            g.drawLine(x, 0, x, height);
        }

        // 填充不同颜色的格子
        if( shape1 != null ){
            g.setColor(Color.RED);
            List<PointVO> points = shape1.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }

        if( shape2 != null ){
            g.setColor(Color.BLUE);
            List<PointVO> points = shape2.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }

        if( shape3 != null ){
            g.setColor(Color.GREEN);
            List<PointVO> points = shape3.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }

        if( shape4 != null ){
            g.setColor(Color.YELLOW);
            List<PointVO> points = shape4.getPoints();
            for ( PointVO point:points ){
                g.fillRect(cellWidth * point.getColNum(), cellHeight * point.getRowNum(), cellWidth, cellHeight);
            }
        }


        // 将图片保存到本地
        File output = new File(outputPath);
        ImageIO.write(image, "png", output);
    }
}

PointVO.java:


import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


@Getter
@Setter
public class PointVO implements Serializable {

    private int rowNum;
    private int colNum;

    public PointVO(int rowNum, int colNum) {
        this.rowNum = rowNum;
        this.colNum = colNum;
    }
}

ShapeVO.java:

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;


@Getter
@Setter
public class ShapeVO implements Serializable {

    private List<PointVO> points;

}

测试输出样例:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
俄罗斯方块旋转算法是解决俄罗斯方块形状旋转的核心算法之一。在制作俄罗斯方块游戏时,旋转算法被用来改变方块的形状。根据引用提到的Unity俄罗斯方块算法,可以实现方块的下落、旋转、移动、加速、填充和销毁等核心功能。如果你对具体的旋转算法感兴趣,可以参考引用给出的代码。该代码给出了旋转部分的具体实现,你可以通过访问作者的博客代码片段查看完整的俄罗斯方块代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [小游戏算法系列一之俄罗斯方块矩阵旋转的一种方法](https://blog.csdn.net/fengsser/article/details/8489261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [基于Unity俄罗斯方块算法实现](https://download.csdn.net/download/qq_33547099/13454704)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [俄罗斯方块方块的旋转变形](https://blog.csdn.net/hanxueyu666/article/details/51338531)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值