c# MonoGame OpenGL读取文件并将纹理加载到框架上
本文关键字:加载 纹理 框架 MonoGame OpenGL 读取 文件 | 更新日期: 2023-09-27 18:18:06
我在c#上使用MonoGame和OpenGL应用程序。
我正在制作一款塔防游戏,我想要加载一个像这样的地图:
++++++++++++++++++++
++++++++++++++++++++
$###++++++++++++++++
+++#+++++++######+++
+++#+++++++#++++#+++
+++#++++####++++##++
+++#++++#++++++++#++
+++#++++#####++++#++
+++#++++++++#++++#++
+++#++++++++#++++#++
+++#++++#####++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++######++++++++#++
+++++++++++++++++#++
+++++++++++++++++#++
+++++++++#########++
+++++++++#++++++++++
+++++++++&++++++++++
Key:
$ = start
# = path
& = finish
+ = grass area
通过读入文件,我想读入这些键并在指定位置放置一个平铺。我在c# Visual Studio中做了以下工作
public partial class Game : System.Windows.Controls.UserControl
{
#region Variables
private Wave _GameWave;
public Wave GameWave
{
get { return _GameWave; }
set { _GameWave = value; }
}
private Map _GameMap;
public Map GameMap
{
get { return _GameMap; }
set { _GameMap = value; }
}
private System.Timers.Timer _GameLoop;
public System.Timers.Timer GameLoop
{
get { return _GameLoop; }
set { _GameLoop = value; }
}
#endregion
public Game()
{
InitializeComponent();
GameLoop = new System.Timers.Timer(1000);
GameWave = new Wave();
CreateMap();
Image I = new Image();
I.Source = new BitmapImage(new Uri(@"..'Pictures'RawModels'GreenMonster.png", UriKind.Relative));
Grid.SetColumn(I, 0);
Grid.SetRow(I, 2);
GameGrid.Children.Add(I);
}
private void CreateMap()
{
GameGrid.RowDefinitions.Clear();
GameGrid.ColumnDefinitions.Clear();
GameMap = new Map();
for (int x = 0; x < GameMap.ActualWidth; x++)
{
GameGrid.ColumnDefinitions.Add(new ColumnDefinition());
}
for (int y = 0; y < GameMap.ActualHeight; y++)
{
GameGrid.RowDefinitions.Add(new RowDefinition());
}
for (int y = 0; y < GameMap.ActualHeight; y++)
{
for (int x = 0; x < GameMap.ActualWidth; x++)
{
Image I = new Image();
if (GameMap.MapArray[x, y].CanPlaceTower)
{
I.Source = new BitmapImage(new Uri(@"..'Pictures'Squares'Grass.jpg", UriKind.Relative));
}
else
{
I.Source = new BitmapImage(new Uri(@"..'Pictures'Squares'Path.jpg", UriKind.Relative));
}
I.Stretch = Stretch.Fill;
Grid.SetColumn(I, x);
Grid.SetRow(I, y);
GameGrid.Children.Add(I);
}
}
}
我想在《一夫一妻》中做同样的事情或类似的事情,我想不出办法。
到目前为止我有这个base.Draw (gameTime);
//Drawing and rendering out the image
spriteBatch.Begin();
for (int y = 0; y < WindowWidth; y+=64)
{
for (int x = 0; x < WindowWidth; x+=64)
{
spriteBatch.Draw(grass, new Rectangle(x, y, 64, 64), Color.White);
}
}
spriteBatch.End();
我想你基本上想使用TitleContainer。打开流读取文本文件
像这样:
var path = @"Content'YourData.txt";
using (var stream = TitleContainer.OpenStream(path))
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
// do your thing
}
}