[英]C# MonoGame OpenGL reading in a file and loading textures onto the frame
我在C#和OpenGL應用程序上使用MonoGame。
我正在制作塔防游戲,並且想要加載如下所示的地圖:
++++++++++++++++++++
++++++++++++++++++++
$###++++++++++++++++
+++#+++++++######+++
+++#+++++++#++++#+++
+++#++++####++++##++
+++#++++#++++++++#++
+++#++++#####++++#++
+++#++++++++#++++#++
+++#++++++++#++++#++
+++#++++#####++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++#++++#++++++++#++
+++######++++++++#++
+++++++++++++++++#++
+++++++++++++++++#++
+++++++++#########++
+++++++++#++++++++++
+++++++++&++++++++++
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);
}
}
}
我想在MonoGame中做相同或類似的事情,我想不出辦法。
我到目前為止有這個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.OpenStream讀取文本文件。
像這樣:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.