gpt4 book ai didi

c# - 初学者角色扮演游戏碰撞检测

转载 作者:太空宇宙 更新时间:2023-11-03 22:39:59 26 4
gpt4 key购买 nike

再次感谢您对我之前问题的帮助。

我在为娱乐和教育而制作的角色扮演游戏上取得了更多进步。我现在可以从 .txt 文件(彩色!)显示我的 map ,所以我很高兴一切正常。

接下来我要实现的是碰撞检测系统。例如:如果我的玩家角色试图踩到一个'~'角色,程序不会移动玩家角色,因为'~'是水,不能在上面行走。

我的移动系统和加载 map 的代码如下:

using System;
using System.IO;

namespace TextFileReaderTest
{
class Program
{
public static int PosX;
public static int PosY;
static void DisplayMap()
{

string line;
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader(@"D:\personal\tests\Tests\ascii map tools\map1.txt");

//Read the first line of text
line = sr.ReadLine();

//Continue to read until you reach end of file
while (line != null)
{
Char[] MapArray = line.ToCharArray();
foreach (Char c in MapArray)
{
if (c == '/')
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write('/');
}
else if (c == '^')
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write('^');
}
else if (c == '|')
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write('|');
}
else if (c == '.')
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write('.');
}
else if (c == 'o')
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write('o');
}
else if (c == '~')
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write('~');
}
else
{
Console.Write(c);
Console.ForegroundColor = System.ConsoleColor.White;
}


}
//Read the next line
line = sr.ReadLine();
}

//close the file
sr.Close();

}




static void Main(string[] args)
{
Console.WindowWidth = 128;
Console.WindowHeight = 32;
DisplayMap();
Console.SetCursorPosition(10, 10); //the cursor will be set at x = 10 and y = 10

while (true)
{
ConsoleKeyInfo input = Console.ReadKey(true);
PosX = Console.CursorLeft;
PosY = Console.CursorTop;

switch (input.KeyChar)
{

case 'w':
if (PosY >= 1)
{
Console.SetCursorPosition(PosX + 0, PosY - 1);
PosX = Console.CursorLeft;
PosY = Console.CursorTop;
//if (PosX == 11 && PosY == 11 ) //this is a portal
//{
// Console.SetCursorPosition(20, 20); // clear console, teleport player, load new map
//}
}
break;

case 'a':
if (PosX >= 1)
{
Console.SetCursorPosition(PosX - 1, PosY + 0);
}
break;

case 's':
if (PosY < 31)
{
Console.SetCursorPosition(PosX + 0, PosY + 1);
}
break;

case 'd':
if (PosX < 127)
{
Console.SetCursorPosition(PosX + 1, PosY + 0);
}
break;

}
}
}
}

许多“if else”语句是针对 map 中的颜色。请不要介意我的代码中有很多注释。每当我有想法时,我通常会在可以实现的地方写评论。另外,这是其他程序员的解释,我觉得很有用。我仍然是一个初学者,所以如果你能解释你的解决方案,那将不胜感激。

提前致谢!

最佳答案

为特定事物设置特定类是一种很好的做法。 IE。一个用于处理 map 的 Map 类,一个用于处理播放器的 Player 类,等等。

现在,为了处理碰撞,我将有两个 Map,一个用于存储和显示播放器图标以及诸如此类的东西,另一个用于图 block 的引用 Map (可以更容易地检查玩家在哪个方 block 上并正朝这边移动)。

Map 应该有一个 private char[][] 字段来存储瓦片,以便可以快速调用所述瓦片。

在你的 Player 类中的两个字段中存储角色的当前位置,XY,然后存储 next 位置到另外两个字段,NewXNewY。在移动 Player 之前,将当前坐标存储到 PrevXPrevY 中。

如果 TileMap[Player.NewX][Player.NewY] 处的图 block 是玩家不应该移动的地方,请不要更改 Player.XPlayer.Y。否则,将 Player.NewX 存储到 Player.X 并将 Player.NewY 存储到 Player.Y

长话短说:

Player 类示例:

public class Player{
public int X, Y;
private int NewX, NewY;
private int PrevX, PrevY;

public Player(){
X = 10;
Y = 10;
NewX = 0;
NewY = 0;
PrevX = 0;
PrevY = 0;
}

//Add other constructors to initialize the coordinates

public void Update(){
//Call this method in `Main()` in a loop. Make sure to add a delay between each call!
PrevX = X;
PrevY = Y;

ConsoleKeyInfo input = Console.ReadKey(true);
switch(input.KeyChar){
case 'w':
NewX = X;
NewY = Y - 1;
break;
case 'a':
NewX = X - 1;
NewY = Y;
break;
case 's':
NewX = X;
NewY = Y + 1;
break;
case 'd':
NewX = X + 1;
NewY = Y;
}

//Add code to restrict the player to the window

if(RPG.ReferenceMap[NewX][NewY] == '~'){ //Water tile
NewX = X;
NewY = Y;
}

//Check for other tiles the player should not walk on

X = NewX;
Y = NewY;
}
}

Map 类示例:

public class Map{
public char[][] TileMap;
public int Height, Width;

public Map(){
TileMap = new char[16][16]; //Default to a 16x16 map
Height = 16;
Width = 16;
}
public Map(string file){
//Load the map like you did above, but store the chars read into the TileMap array
//Give the 'Height' and 'Width' fields the height and width of the map
}

public void Update(){
//Call this AFTER Player.Update()

//Use the reference map to add the correct tile to the previous position
//and then update the Player's icon
TileMap[Player.PrevX][Player.PrevY] = RPG.ReferenceMap.TileMap[Player.PrevX][Player.PrevY];

TileMap[Player.X][Player.Y] = 'O'; //Assuming that 'O' is your player icon
}

public void Draw(){
//Use the "TileMap" field to refresh the map

Console.Clear(); //Clear the console

foreach(char[] array in TileMap){
foreach(char tile in array){
//Use the logic you used in your "foreach" loop to draw the tiles.
}
}
}
}

在你的主类中应该调用什么的例子,假设它被称为 RPG:

public class RPG{
Player player;
Map ReferenceMap, ActualMap;

public static void Main(){
player = new Player(); //Use the default constructor or another constructor
//to set the starting coords of the player

ActualMap = new Map(); //Load the data into the map

ReferenceMap = ActualMap; //Since they are the same for now, just store
//the data already there
while(true){
//Add a delay using Stopwatch, preferably 1/60th of a second
Update();
}
}

public void Update(){
Player.Update();
ActualMap.Update();

ActualMap.Draw();
}
}

关于c# - 初学者角色扮演游戏碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52779977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com