我的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;
}
}
}
}
}
答案 0 :(得分:4)
这就像你是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类代表您可以放置在迷宫中的不同元素,主程序将负责显示登上玩家。
祝你好运!