我編程我的C#學校項目一個俄羅斯方塊克隆。我使用Microsoft Visual Studio 2012遊戲本身作爲塊的二維陣列來實現(名單塊列表的),每塊都有其自己的紋理(BMP圖像)。我正在繪製整個數組到一個PictureBox控件,這是問題開始的地方。更新PictureBox上的圖像(移動/旋轉活動形狀)時,遊戲會略微滯後。我試圖畫一個Panel控件,但結果是一樣的。我有一個粗略的想法,可能會導致滯後,但我不知道如何擺脫它。C#俄羅斯方塊遊戲性能下降
這是遊戲「網格」的繪製方法:
public void Draw(Graphics g)
{
Brush brush;
Font font = new System.Drawing.Font("Arial", 5);
for (int i = 0; i < Width; i++)
for (int j = 0; j < Height; j++)
{
brush = new TextureBrush(Blocks[i][j].Texture);
if (Blocks[i][j].Occupied==true)
g.FillRectangle(brush, i * 20, j * 20, i * 20 + Blocks[i][j].Texture.Width, j * 20 + Blocks[i][j].Texture.Height);
}
}
這是活性四格拼板的拉伸方法:
public void Draw(Graphics g)
{
Brush brush = new TextureBrush(Blocks[0].Texture);
foreach (FullBlock b in Blocks)
g.FillRectangle(brush, b.x * 20, b.y * 20,b.Texture.Width, b.Texture.Height);
}
遊戲本身然後使用這兩者(雙緩衝未遂):
public void GameDraw(PictureBox p)
{
Graphics g = Graphics.FromImage(gb);
gameGrid.Draw(g);
PlayingShape.Draw(g);
p.Image = gb;
p.Refresh();
}
其中是私人Bitmap
v可以在類構造函數中創建一次(減少(不成功))滯後)。
每當比賽的狀態改變時,GameDraw
方法被調用(例如移動/旋轉活動四格拼板和每一個「萬有引力」打勾)
你不需要使用圖片框,使用標準的GDI +繪圖,它會運行*很多*更快。 – Tigran 2013-05-14 11:27:09
if(Blocks [i] [j] .Occupied == true)<---不要這樣做。只需使用.Occupied,它將返回一個布爾值。 – 2013-05-14 13:04:07