我试图用C#制作象棋游戏,并在移动了几块后一直遇到“内存不足”的问题。我是C#的初学者。我的目标是制作一个功能良好的国际象棋游戏,其中棋子只能移动到有效位置(即典当只能向前走,除非它可以取对角线棋子等)。我知道解决方案是使棋盘成为单个位图而不是每次移动一块新的位图,但不确定如何解决。
我不确定下一步要去哪里,但是我知道我需要先解决位图问题,然后再进行其他操作。我认为问题是该类中的代码。我已将()中图像文件所在的部分更改为GetImageLocation
此代码的格式为1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Checkards
{
public partial class Form1 : Form
{
List<Pieces> chessBoard = new List<Pieces>();
int move_col = -1;
int move_row = -1;
Bitmap DBrown = new Bitmap("E:\\VS\\Checkards\\Images\\DarkBrownSquare.png");
Bitmap LBrown = new Bitmap("E:\\VS\\Checkards\\Images\\LightBrownSquare.png");
Pieces.Player current = Pieces.Player.User;
public Form1()
{
InitializeComponent();
Drawboard();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
Drawboard();
}
private void Form1_Load(object sender, EventArgs e)
{
Drawboard();
for (int i = 0; i < 8; i++)
{
Pieces p = new Pieces(i, 1, Pieces.PieceType.Pawn, Pieces.Player.Computer);
p.x = i;
chessBoard.Add(p);
}
chessBoard.Add(new Pieces(0, 0, Pieces.PieceType.Rook, Pieces.Player.Computer));
chessBoard.Add(new Pieces(1, 0, Pieces.PieceType.Knight, Pieces.Player.Computer));
chessBoard.Add(new Pieces(2, 0, Pieces.PieceType.Bishop, Pieces.Player.Computer));
chessBoard.Add(new Pieces(3, 0, Pieces.PieceType.Queen, Pieces.Player.Computer));
chessBoard.Add(new Pieces(4, 0, Pieces.PieceType.King, Pieces.Player.Computer));
chessBoard.Add(new Pieces(5, 0, Pieces.PieceType.Bishop, Pieces.Player.Computer));
chessBoard.Add(new Pieces(6, 0, Pieces.PieceType.Knight, Pieces.Player.Computer));
chessBoard.Add(new Pieces(7, 0, Pieces.PieceType.Rook, Pieces.Player.Computer));
for(int i = 0; i < 8; i++)
{
Pieces p = new Pieces(i, 6, Pieces.PieceType.Pawn, Pieces.Player.User);
p.x = i;
chessBoard.Add(p);
}
chessBoard.Add(new Pieces(0, 7, Pieces.PieceType.Rook, Pieces.Player.User));
chessBoard.Add(new Pieces(1, 7, Pieces.PieceType.Knight, Pieces.Player.User));
chessBoard.Add(new Pieces(2, 7, Pieces.PieceType.Bishop, Pieces.Player.User));
chessBoard.Add(new Pieces(3, 7, Pieces.PieceType.Queen, Pieces.Player.User));
chessBoard.Add(new Pieces(4, 7, Pieces.PieceType.King, Pieces.Player.User));
chessBoard.Add(new Pieces(5, 7, Pieces.PieceType.Bishop, Pieces.Player.User));
chessBoard.Add(new Pieces(6, 7, Pieces.PieceType.Knight, Pieces.Player.User));
chessBoard.Add(new Pieces(7, 7, Pieces.PieceType.Rook, Pieces.Player.User));
}
public void Drawboard()
{
Bitmap pawn = new Bitmap("E:\\VS\\Checkards\\Images\\BlackPawn.png");
Bitmap landscape = new Bitmap(panel1.Width, panel1.Height);
Graphics g = Graphics.FromImage(landscape);
g.Clear(Color.Black);
float width = panel1.Width / 8.0f;
float height = panel1.Height / 8.0f;
for(int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (((i % 2 == 0) && (j % 2 == 0)) || ((i % 2 == 1) && (j % 2 == 1)))
{
g.DrawImage(LBrown, i * width, j * height, width, height);
}
else
{
g.DrawImage(DBrown, i * width, j * height, width, height);
}
}
}
for (int i = 0; i < chessBoard.Count; i++)
{
g.DrawImage(chessBoard[i].GetBitmap(), chessBoard[i].x * width, chessBoard[i].y * height, width, height);
}
Graphics g2 = panel1.CreateGraphics();
g2.DrawImage(landscape, 0, 0);
landscape.Dispose();
g.Dispose();
}
此代码在“部件”类别下
public Bitmap GetBitmap()
{
return new Bitmap(GetImageLocation());
}
我希望能够在不耗尽内存的情况下运行代码。