Zac*_*tén 0 c# optimization chess lag
我第一次在这里问一个问题,如果我做错了,请纠正我.
我国际象棋的图片:http: //img842.imageshack.us/img842/2695/65744343.png
每次我移动一块它都会滞后大约1秒钟.每件和瓷砖都有一个图像,正好有96个图像.每次我移动一块它都会用黑色清除所有内容,然后更新图形.
在国际象棋的早期阶段,我没有任何图像,而是使用了不同的颜色,只有几件没有明显的滞后,这件作品瞬间移动.
public void updateGraphics(PaintEventArgs e, Graphics g, Bitmap frame)
{
g = Graphics.FromImage(frame);
g.Clear(Color.Black);
colorMap(g);
g.Dispose();
e.Graphics.DrawImageUnscaled(frame, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
函数colorMap(g)看起来像这样:
private void colorMap(Graphics g)
{
for (int y = 0; y < SomeInts.amount; y++)
{
for (int x = 0; x < SomeInts.amount; x++)
{
//Tiles
Bundle.tile[x, y].colorBody(g, x, y);
//Pieces
player1.colorAll(g);
player2.colorAll(g);
}
}
}
Run Code Online (Sandbox Code Playgroud)
colorAll函数执行每个colorBody(g)函数,如下所示:
public void colorBody(Graphics g)
{
//base.colorBody() does the following: body = new Rectangle(x * SomeInts.size + SomeInts.size / 4, y * SomeInts.size + SomeInts.size / 4, size, size);
base.colorBody();
if (team == 1)
{
//If its a white queen
image = Image.FromFile("textures/piece/white/queen.png");
}
if (team == 2)
{
//If its a black queen
image = Image.FromFile("textures/piece/black/queen.png");
}
g.DrawImage(image, body);
}
Run Code Online (Sandbox Code Playgroud)
最后移动片的功能:
public void movePiece(MouseEventArgs e)
{
for (int y = 0; y < SomeInts.amount; y++)
{
for (int x = 0; x < SomeInts.amount; x++)
{
if (Bundle.tile[x, y].body.Contains(e.Location))
{
//Ignore this
for (int i = 0; i < queens.Count; i++)
{
Queen temp = queens.ElementAt<Queen>(i);
temp.move(x, y);
}
//Relevant
player1.move(x, y);
player2.move(x, y);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您阅读所有这些!如果我的编码示例不够,我可以链接到整个程序.
您Image.FromFile在每次刷新时调用每个图像 - 每次从磁盘重新加载每个图像文件.
您是否考虑过将图像加载一次,并将结果存储在Image有用的地方?(比方说,阵列Image[2,6]就足够了)