C#ascii游戏冲突

本文关键字:冲突 游戏 C#ascii | 更新日期: 2023-09-27 18:25:19

我需要帮助处理ascii游戏冲突。我已经知道当玩家击中另一个物体时该怎么做了。但当我想让球员在球员击中物体时停下来时,我有点困惑。

当玩家与另一个物体碰撞时,我会使用这个。

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

所以,如果你能帮助我,那就太棒了,因为我已经研究碰撞问题有一段时间了,我想把它应用到我的游戏中,这样它就可能是一个迷宫般的游戏。感谢阅读:3

这是我的游戏"完整代码"的结构:

    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#ascii游戏冲突

这感觉就像是对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类代表你可以放置在迷宫中的不同元素,主程序将负责向玩家显示棋盘。

祝你的比赛好运!