我正在尝试编写一个简单的 2D 游戏。这是已经完成的工作:
public interface GameObject
{
Bitmap Picture { get; }
int OffsetX { get; }
int OffsetY { get; }
}
Picture 属性返回用于绘制当前对象的图片。
public static class Game
{
public class MapIndexer
{
public GameObject this[int x, int y]
{
get => map[y, x];
set => map[y, x] = value;
}
}
private static MapIndexer indexer;
public static MapIndexer Map
{
get => indexer ?? (indexer = new MapIndexer());
}
static GameObject[,] map = new GameObject[,]
{
{ null, null, null },
{ null, null, null },
{ null, null, null }
};
public static int MapWidth
{
get => map.GetLength(1);
}
public static int MapHeight
{
get => map.GetLength(0);
}
}
Game类,这里是访问笛卡尔系统中地图的索引器(例如下移是Game.Map[x, y+1],否则下移是Game.Map[x+1, y])。数组中的零是地图填充物,在我的例子中只是草。我还创建了一个简单的 Player 类,它返回自己的图像。
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int y = 0; y < Game.MapHeight; y++)
for (int x = 0; x < Game.MapWidth; x++)
{
e.Graphics.DrawImage(Properties.Resources.Grass, new Point(x * size, y * size));
if (Game.Map[x, y] is not null)
e.Graphics.DrawImage(Game.Map[x, y].Picture, new Point(x * size + Game.Map[x, y].OffsetX, y * size + Game.Map[x, y].OffsetY));
}
}
绘图代码本身。我觉得这很奇怪,他的作品证实了这一点 - 当绘制 200 毫秒的动画时,卡丁车会“闪烁”。实际上问题是如何优化所有这些渲染?