关于一个大一学生的俄罗斯方块项目分享C#开发,附源码(三)

关于边界碰撞和方块碰撞

完成了下落对象基本的移动和旋转还不够,在很多情况下下落对象是不能移动和旋转的。

 

第一种情况,我们不能让对象超出边界

 

第二种情况,在周围存在已经固定的方块时,我们不能让对象和这些方块重合

 

为了解决这个问题,我想到了办法:

在玩家按下按键后,先生成一个GameObject

检验这个临时对象能不能存在,即检验x,y是否在0-9,0-23(有四行不在界面范围内,给玩家反应时间和方便检验失败)内(第一种情况)

把坐标带入staticblock数组内验证是否为true(第二种情况)

public bool CrachCheck(Tpoint[] point4)
        {
            
            for(byte i=0;i<4;i++)
            {
               
                if(point4[i].x<0|| point4[i].x>9 || point4[i].y == 24)
                {

                    return false;
                }
                if(point4[i].y>4&& point4[i].y<23)
                {
                    if (staticBlock[point4[i].y - 4, point4[i].x])
                        return false;
                }
            }
            return true;
        }

 

刚开始我是这样想的,以上判定为假的话,直接锁住按键,然后不做任何行为,自然下落,不就行了吗?

 

然而,思考之后我发现不行。玩过俄罗斯方块的人都知道,如果在边界按旋转的话,依然是可以旋转的,只不过旋转中心不是原来的旋转中心,所以以上判断只能验证方块是否存在,判断能否移动可以,判断旋转还要另想办法

 

经过几节英语课摸鱼的思考(哈哈哈),我想到了三种方法,最后我用了最简单的方法,且听我一一道来

 

第一种方法(复杂,但完美)

我们之前做了一个中心坐标和他的三个矢量坐标来实现旋转,如果我们把临时对象的每个方块都匹配三个矢量坐标,收到旋转命令时把每一个都试一下,(当然存在优先级,中间的优先级高,周围的优先级低)如果有满足的,就执行,如果没有,就不执行(太完美了,可以解决周围有固定方块的情形,我后面两个方法均不能解决此问题,我强烈怀疑真正的游戏源码就是采用的这个方法)

 

最先想到的方法,但每个方块都要算矢量坐标,一个方块3个坐标,一个对象就是12个坐标,七个对象加起来一共是84个坐标,太麻烦,当然也可以编个程序算,但是我懒的编

 

第二种方法

当临时对象的四个方块坐标存在负数时,给中心对象加上这个负数的绝对值,然后生成这个新对象,再检验,如果通过,则采用新对象对象,不通过,则采用初始对象

 

我一开始用的是这个方法,但由于本人代码不精,出现了bug,老消不掉,气得我把这段全删了

 

第三种方法

我用了这个最简单的方法,这个方法特别简单,也特别巧妙。当四个坐标有x=0或者x=9时(即有对象处于边缘时。注意!不是临时对象,是按键还没有按下的时候的初始对象)直接锁死左或右键,(在0锁死左键,反之亦然)并且如果玩家按下旋转键,这里以在左边缘举例,执行下落对象向右移动的命令,然后旋转,此时即等同于玩家按下了右键和旋转键

if (Keypress!=Keypress.NULL)
            {
                response = 0;
                switch (Keypress)
                {
                    case Keypress.A: tempcenturePoint.x--; break;
                    case Keypress.D: tempcenturePoint.x++; break;
                    case Keypress.J: tempAngle++;tangle = true; break;
                    default:break;
                }

                
                if (tangle && tempcenturePoint.x == 0)
                {
                    if (tempAngle == 1 && gO == GO.I)
                        tempcenturePoint.x += 2;
                    else
                        tempcenturePoint.x++;
                }
                if (tangle && tempcenturePoint.x == 9)
                {
                    if (tempAngle == 1 && gO == GO.I)
                        tempcenturePoint.x -= 2;
                    else
                        tempcenturePoint.x--;
                }
                Objectgame tempobjectgame = new Objectgame();
                Tpoint[] temppoint4 = tempobjectgame.GetPoint(gO, tempcenturePoint, (byte)(tempAngle % 4 + 3));
                if (CrachCheck(temppoint4))
                {
                    centureX = (byte)tempcenturePoint.x;
                    centureY = (byte)tempcenturePoint.y;
                    angle = tempAngle;
                }
                Keypress = Keypress.NULL;
             
            }

time1的代码

 

(嘿嘿,各位看官能理解吗,是不是很巧妙)

然而,当出现长条对象时(I),在某个角度旋转,移动一格是不行的,这个可以加一个特例,处于这个情况时移动两格

 

关于下落碰撞检测,和这个差不多,码字累了,就不赘述了,直接上代码吧

tempcenturePoint.y++;
            Objectgame tempobjectgame1 = new Objectgame();
            Tpoint[] temppoint41 = tempobjectgame1.GetPoint(gO, tempcenturePoint, (byte)(tempAngle % 4 + 3));
            if (FallCheck(temppoint41))
                centureY = (byte)tempcenturePoint.y;
            else
                tfall = true;
            
            
            Tpoint centurePoint = new Tpoint();
            centurePoint.x = centureX;
            centurePoint.y = centureY;
            
            Objectgame objectgame = new Objectgame();
            Tpoint[] point4 = objectgame.GetPoint(gO, centurePoint, (byte)(angle % 4 + 3));

time1的代码

private bool FallCheck(Tpoint[] point4)
        {
            for (byte i = 0; i < 4; i++)
            {

                
                if (point4[i].x < 0 || point4[i].x > 9)
                {

                    break;
                }
                if(point4[i].y == 24)
                {
                    return false;
                }
                if (point4[i].y > 4 )
                {
                    if (staticBlock[point4[i].y - 4, point4[i].x])
                        return false;
                }
            }
            return true;
        }

 

这样的话就解决了碰撞问题,如果你觉得我的方法好的话,就给我点个赞吧!

 

下一节我会讲定时器的一次执行的整体流程,就是把我们之前写的函数拼接在一起,这样游戏就大功告成啦!

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答您的问题!以下是一个简单的C#俄罗斯方块游戏的示例代码: ```csharp using System; using System.Collections.Generic; namespace Tetris { class Program { static void Main(string[] args) { // 初始化游戏板和方块 int[,] board = new int[20, 10]; Block currentBlock = new Block(); // 游戏循环 while (true) { // 显示游戏板和当前方块 DisplayBoard(board, currentBlock); // 获取用户输入 ConsoleKeyInfo keyInfo = Console.ReadKey(true); // 移动方块或旋转方块 if (keyInfo.Key == ConsoleKey.LeftArrow) { currentBlock.MoveLeft(board); } else if (keyInfo.Key == ConsoleKey.RightArrow) { currentBlock.MoveRight(board); } else if (keyInfo.Key == ConsoleKey.DownArrow) { currentBlock.MoveDown(board); } else if (keyInfo.Key == ConsoleKey.UpArrow) { currentBlock.Rotate(board); } // 下落方块 if (!currentBlock.MoveDown(board)) { // 如果方块无法再下落,将方块固定到游戏板上 currentBlock.Lock(board); // 判断是否有一行已经填满 int linesCleared = 0; for (int i = board.GetLength(0) - 1; i >= 0; i--) { bool isFullLine = true; for (int j = 0; j < board.GetLength(1); j++) { if (board[i, j] == 0) { isFullLine = false; break; } } if (isFullLine) { // 如果有一行已经填满,将该行删除并将上面的方块下移 for (int k = i; k > 0; k--) { for (int j = 0; j < board.GetLength(1); j++) { board[k, j] = board[k - 1, j]; } } linesCleared++; i++; // 由于上面的方块下移,需要重新检查该行 } } // 如果有行被删除,更新分数 if (linesCleared > 0) { UpdateScore(linesCleared); } // 创建一个新的方块 currentBlock = new Block(); } } } static void DisplayBoard(int[,] board, Block currentBlock) { // 清空控制台 Console.Clear(); // 显示分数 Console.WriteLine("Score: " + score); // 显示游戏板和当前方块 for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { if (board[i, j] == 0) { Console.Write("."); } else { Console.Write("X"); } } Console.Write(" "); for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { if (currentBlock.Shape[j, k] == 0) { Console.Write("."); } else { Console.Write("X"); } } Console.WriteLine(); } } } static int score = 0; static void UpdateScore(int linesCleared) { score += linesCleared * 100; } } class Block { public int[,] Shape { get; private set; } public int Row { get; private set; } public int Col { get; private set; } private static Random random = new Random(); private static List<int[,]> shapes = new List<int[,]>() { new int[,] { { 1, 1 }, { 1, 1 } }, new int[,] { { 0, 1, 0 }, { 1, 1, 1 } }, new int[,] { { 1, 0, 0 }, { 1, 1, 1 } }, new int[,] { { 0, 0, 1 }, { 1, 1, 1 } }, new int[,] { { 1, 1, 0 }, { 0, 1, 1 } }, new int[,] { { 0, 1, 1 }, { 1, 1, 0 } }, new int[,] { { 1, 1, 1, 1 } } }; public Block() { // 随机选择一个方块 int index = random.Next(shapes.Count); Shape = shapes[index]; // 将方块放到游戏板的顶部中央 Row = 0; Col = (10 - Shape.GetLength(1)) / 2; } public bool MoveLeft(int[,] board) { if (Col > 0 && CanMove(board, Row, Col - 1)) { Col--; return true; } return false; } public bool MoveRight(int[,] board) { if (Col < board.GetLength(1) - Shape.GetLength(1) && CanMove(board, Row, Col + 1)) { Col++; return true; } return false; } public bool MoveDown(int[,] board) { if (Row < board.GetLength(0) - Shape.GetLength(0) && CanMove(board, Row + 1, Col)) { Row++; return true; } return false; } public void Rotate(int[,] board) { int[,] rotatedShape = new int[Shape.GetLength(1), Shape.GetLength(0)]; for (int i = 0; i < Shape.GetLength(0); i++) { for (int j = 0; j < Shape.GetLength(1); j++) { rotatedShape[j, Shape.GetLength(0) - 1 - i] = Shape[i, j]; } } if (CanMove(board, Row, Col, rotatedShape)) { Shape = rotatedShape; } } public void Lock(int[,] board) { for (int i = 0; i < Shape.GetLength(0); i++) { for (int j = 0; j < Shape.GetLength(1); j++) { if (Shape[i, j] != 0) { board[Row + i, Col + j] = 1; } } } } private bool CanMove(int[,] board, int newRow, int newCol) { return CanMove(board, newRow, newCol, Shape); } private bool CanMove(int[,] board, int newRow, int newCol, int[,] newShape) { for (int i = 0; i < newShape.GetLength(0); i++) { for (int j = 0; j < newShape.GetLength(1); j++) { if (newShape[i, j] != 0) { int row = newRow + i; int col = newCol + j; if (row < 0 || row >= board.GetLength(0) || col < 0 || col >= board.GetLength(1) || board[row, col] != 0) { return false; } } } } return true; } } } ``` 此代码创建了一个简单的俄罗斯方块游戏,其中方块可以左右移动、向下落、旋转,并且可以与游戏板上的方块进行碰撞检测。该代码可以运行在控制台中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值