如何解决我如何修复我的 c# 控制台网络框架 PacMan 游戏?
我开始编写 pacMan 控制台游戏并且在 pacMan 移动时遇到了问题,我只能向左移动他,如果我向上、向下或向右移动他,会发生非常奇怪的事情,但我的代码看起来不错。请检查我在 RunGame 类中的 runGame 函数(这里我有移动代码)并告诉我如何修复它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PacMan
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.GetEncoding(1251);
Console.CursorVisible = false;
gameField.setGameArr();
gameField.drawField();
while (true)
{
RunGame.runGame();
}
}
}
class gameField
{
public static Random random = new Random();
public static string[,] gameArr = new string[28,84]; // 28 rows and 84 columns | Row - y coordinate,Column - x
public static int xnum = gameArr.GetUpperBound(1);
public static int yNum = gameArr.GetUpperBound(0);
public static void setGameArr()
{
gameArr[5,5] = PacMan.rightSprite;
for (int y = 0; y < yNum; y++)
{
for (int x = 0; x < xnum; x++)
{
if (y == 0 || x == 0 || y == yNum - 1 || x == xnum - 1)
{
gameArr[y,x] = "#";
}
}
}
for (int i = 0; i < yNum; i++)
{
for (int j = 0; j < xnum; j++)
{
if (gameArr[i,j] == null)
gameArr[i,j] = ".";
}
}
}
public static void drawField()
{
for (int y = 0; y < yNum; y++)
{
for (int x = 0; x < xnum; x++)
{
if (gameArr[y,x] != null)
Console.Write(gameArr[y,x]);
else Console.Write("");
}
Console.WriteLine();
}
}
}
class PacMan
{
public static string upSprite = "v";
public static string downsprite = "^"; //"ᗣ";
public static string rightSprite = "<"; //"ᗧ";
public static string leftSprite = ">"; //"ᗤ";
public static (int y,int x) getPlayerPos()
{
for (int y = 0; y < gameField.yNum; y++)
{
for (int x = 0; x < gameField.xnum; x++)
{
if (gameField.gameArr[y,x] == upSprite ||
gameField.gameArr[y,x] == downsprite ||
gameField.gameArr[y,x] == rightSprite ||
gameField.gameArr[y,x] == leftSprite)
{
return (y,x);
}
}
}
return (0,0);
}
}
class RunGame
{
public static void runGame()
{
(int y,int x) playerPos = PacMan.getPlayerPos();
ConsoleKeyInfo ki = Console.ReadKey(true);
if (ki.Key == ConsoleKey.LeftArrow &&
gameField.gameArr[playerPos.y,playerPos.x - 1] != "#")
{
gameField.gameArr[playerPos.y,playerPos.x] = null;
gameField.gameArr[playerPos.y,playerPos.x - 1] = PacMan.leftSprite;
Console.Clear();
gameField.drawField();
}
if (ki.Key == ConsoleKey.RightArrow &&
gameField.gameArr[playerPos.y,playerPos.x + 1] != "#")
{
gameField.gameArr[playerPos.y,playerPos.x + 1] = PacMan.rightSprite;
Console.Clear();
gameField.drawField();
}
if (ki.Key == ConsoleKey.UpArrow &&
gameField.gameArr[playerPos.y - 1,playerPos.x] != "#")
{
gameField.gameArr[playerPos.y,playerPos.x] = null;
gameField.gameArr[playerPos.y - 1,playerPos.x] = PacMan.upSprite;
Console.Clear();
gameField.drawField();
}
if (ki.Key == ConsoleKey.DownArrow &&
gameField.gameArr[playerPos.y + 1,playerPos.x] = null;
gameField.gameArr[playerPos.y + 1,playerPos.x] = PacMan.downsprite;
Console.Clear();
gameField.drawField();
}
}
}
class Ghost
{
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。