Skip to main content
 首页 » 编程设计

C# ascii游戏碰撞

2024年02月13日11del

我的 ascii 游戏冲突需要帮助。我已经知道如何在玩家击中另一个物体时做某事。但是当我想让玩家在玩家击中物体时停止时,我有点困惑。

我在玩家与另一个物体碰撞时使用它。

if(player.x == object.x && player.y == object.y) 
  { 
       //Does something 
  } 

所以,如果您能帮助我,那就太棒了,因为我已经研究了一段时间的碰撞问题,并且我想将其实现到我的游戏中,因此它可能是一个类似迷宫的游戏。感谢您的阅读:3

这是我的游戏“WHOLE CODE”的结构:

    public static void Main(string[] args) 
    { 
        int x = 40; 
        int y = 12; 
        int ax = 23; 
        int ay = 7; 
        int apple = 0; 
        int starX = 64; 
        int starY = 5; 
        int score = 0; 
        int total = 0; 
        int time = 100; 
        int xt; 
        int yt; 
        bool quit = false; 
        Console.Title = "Catch and Run"; 
 
        while (quit == false) 
        { 
            Console.BackgroundColor = ConsoleColor.DarkGreen; 
 
            //Blocks 
            Console.SetCursorPosition(29, 18); 
            Console.BackgroundColor = ConsoleColor.DarkGreen; 
 
            //Clear Screen 
            Console.Clear(); 
 
            //Players 
            Console.SetCursorPosition(x, y); 
            Console.ForegroundColor = ConsoleColor.Yellow; 
            Console.Write("☻"); 
 
            Console.SetCursorPosition(ax, ay); 
            Console.ForegroundColor = ConsoleColor.Red; 
            Console.Write("o"); 
 
            Console.SetCursorPosition(starX, starY); 
            Console.ForegroundColor = ConsoleColor.Gray; 
            Console.Write("☻"); 
            Console.ResetColor(); 
 
            //score 
            Console.SetCursorPosition(0, 0); 
            Console.WriteLine("People: " + score); 
            //apples 
            Console.SetCursorPosition(20, 0); 
            Console.WriteLine("Apples: " + apple); 
            //Total 
            Console.SetCursorPosition(40, 0); 
            total = apple + score; 
            Console.WriteLine("Total: " + total); 
            //Timer 
            Console.SetCursorPosition(0, 23); 
            Console.WriteLine("Steps Left: "+time); 
            time--; 
 
            ConsoleKeyInfo keyInfo = Console.ReadKey(false); 
 
            //key controlls 
            switch (keyInfo.Key) 
            { 
                case ConsoleKey.Escape: 
                    quit = true; 
                    break; 
                case ConsoleKey.UpArrow: 
                    if (y > 1) 
                        y--; 
 
                    break; 
                case ConsoleKey.DownArrow: 
                    if (y < 22) 
                        y++; 
 
                    break; 
                case ConsoleKey.LeftArrow: 
                    if (x > 0) 
                        x--; 
 
                    break; 
                case ConsoleKey.RightArrow: 
                    if (x < 79) 
                        x++; 
 
                    break; 
            } 
            // Randomize Blacks 
            if (x == starX && y == starY) 
            { 
 
                Random random = new Random(); 
                starX = random.Next(0, 80); 
                starY = random.Next(1, 22); 
                score += 10; 
                time += 30; 
            } 
            //Randomize Apples 
            if (x == ax && y == ay) 
            { 
 
                Random random = new Random(); 
                ax = random.Next(0, 80); 
                ay = random.Next(1, 22); 
                apple += 5; 
                time += 20; 
            } 
            // Game Over timer set to 0 
            if (time == 0) 
            { 
 
                Console.Clear(); 
                Console.SetCursorPosition(40, 0); 
                Console.WriteLine("GAME OVER"); 
 
                Console.WriteLine("Score" + total); 
                Console.WriteLine("Press any key to continue"); 
                Console.ReadKey(); 
                quit = true; 
            } 
            if (x == 29 && y == 18) 
            { 
                x = 0; 
                y = 1; 
 
            } 
        } 
    } 
} 

请您参考如下方法:

这感觉就像您是 C#/面向对象编程的新手。我的回答会有点复杂,但欢迎提出更多问题。

回答您的核心问题“我希望玩家在玩家击中物体时停止”,您需要找出人类玩家想要移动英雄的位置,然后进行碰撞检测,如果玩家没有被挡住,你再移动他。

如果这最终将成为一个类似迷宫的游戏,您将需要以某种方式存储游戏环境。一个简单的方法是:

char[,] maze = new char[25,80]; // [y,x] 

您应该首先创建这个数组,将几个苹果放入其中,然后显示棋盘。

例如,我们可以通过以下方式将苹果放在 5,5 处:

maze[5,5] = 'o'; 

一旦显示板的工作原理,您就可以扩展它来创建墙,例如:

for(int by=10; by<20; by++) 
{ 
   maze[by, 0] = '#'; 
   maze[by, 2] = '#'; 
} 

这将在屏幕左侧创建一个 10 高的走廊。

打印板应该在您调用 Clear 之后发生:

for (int by = 0; by < 25; by++) 
{ 
    for (int bx = 0; bx < 80; bx++) 
    { 
        Console.Write(maze[by, bx]); 
    } 
} 

为了满足您的碰撞检测,您需要执行以下操作:

// !! Replace your switch with this, then continue experimenting 
 
int tempX, tempY; 
 
tempX = x; 
tempY = y; 
 
switch (keyInfo.Key) 
{ 
    case ConsoleKey.Escape: 
        quit = true; 
        break; 
    case ConsoleKey.UpArrow: 
        if (tempY > 1) 
            tempY--; 
        break; 
    case ConsoleKey.DownArrow: 
        if (tempY < 22) 
            tempY++; 
        break; 
    case ConsoleKey.LeftArrow: 
        if (tempX > 0) 
            tempX--; 
        break; 
    case ConsoleKey.RightArrow: 
        if (tempX < 79) 
            tempX++; 
        break; 
} 
 
// Bounds checking already done by switch 
 
if(maze[tempY, tempX] == '#') 
{ 
    // Collision with wall is detected - forbid player from moving 
} 
else if(maze[tempY, tempX] == 'o') 
{ 
    // Collision with apple is detected, handle consuming an apple 
    x = tempX; // Now we update the player's position 
    y = tempY; 
} else { 
    x = tempX; // No collision with anything, simply update player's position 
    y = tempY; 
} 

这是您可以对代码进行的简短修改。我强烈建议您考虑将各种代码块移动到它们自己的方法甚至类中,以使您的代码更易于开发和维护。你的目标应该是每个方法都有一个责任(你的主要方法在这里做的太多了,单一责任方法更容易编码和更容易修复)并且每个类都有一个责任。

例如,诸如“bool CanMoveTo(int x, int y)”之类的方法允许您将边界检查和碰撞检测放置在 Main 之外的某处,然后您可以调用此方法来查看玩家是否正在一个有效的举动。

对于类,您可能有一个 Board 类负责管理迷宫及其上的棋子,一个 BoardPiece 类代表您可以放置​​在迷宫中的不同元素,而主程序将负责显示板给玩家。

祝你游戏顺利!