C# Can';我不明白为什么广场赢了;不要在网格上设置动画
在我的一生中,我无法解决如何通过“Arxl”对象数组通过每个循环绘制块,从而在网格上设置动画 如果您有任何建议,我将不胜感激,而不是找人帮我完成代码。只是一双新的眼睛C# Can';我不明白为什么广场赢了;不要在网格上设置动画,c#,C#,在我的一生中,我无法解决如何通过“Arxl”对象数组通过每个循环绘制块,从而在网格上设置动画 如果您有任何建议,我将不胜感激,而不是找人帮我完成代码。只是一双新的眼睛 public partial class Game : Form { //attributes private Bitmap _grid; private Arxl[,] _cartesianGrid; private int _arxlAmount; const int ARXL = 4;
public partial class Game : Form
{
//attributes
private Bitmap _grid;
private Arxl[,] _cartesianGrid;
private int _arxlAmount;
const int ARXL = 4;
public Game()
{
InitializeComponent();
_arxlAmount = (gridPictureBox.Height / ARXL);//in case height/arxl is not an even number?
_cartesianGrid = new Arxl[_arxlAmount, _arxlAmount];
_grid = new Bitmap(gridPictureBox.Width, gridPictureBox.Height);
int x;
int y;
for (x = 0; x < _arxlAmount; x++)
{
for (y = 0; y < _arxlAmount; y++)
{
_cartesianGrid[x, y] = new Arxl();
}
}
SetSeed(_cartesianGrid);
}
private void SetSeed(Arxl[,] cartesianGrid)
{
_cartesianGrid[1, 1].Active = true;
}
private void DrawArxl(Bitmap _grid, Arxl[,] cartesianGrid,int arxlAmount)
{
int x, y;
x=0;
y=0;
Graphics graphics = Graphics.FromImage(_grid);
graphics.Clear(Color.White);
for (x = 1; x < arxlAmount;x++ )
{
for (y = 1; y < arxlAmount; y++)
{
if (cartesianGrid[x, y].Active==true)
{
cartesianGrid[x, y].Area = new Rectangle(x * ARXL, y * ARXL, ARXL, ARXL);
graphics.FillRectangle(Brushes.Black, cartesianGrid[x, y].Area);
}
else if(cartesianGrid[x,y].Active==false)
{
Pen newPen=new Pen(Color.Black);
cartesianGrid[x, y].Area = new Rectangle(x * ARXL, y * ARXL, ARXL, ARXL);
graphics.DrawRectangle(newPen,cartesianGrid[x, y].Area);
newPen.Dispose();
}
}
}
}
private void timer_Tick(object sender, EventArgs e)
{
//GameOfLife(_cartesianGrid, _arxlAmount);
ScrollBlock(_cartesianGrid, _arxlAmount);
DrawArxl(_grid, _cartesianGrid, _arxlAmount);
gridPictureBox.Image = _grid;
}
private void ScrollBlock(Arxl[,] cartesianGrid, int arxlAmount)
{
int x = 0;
int y = 0;
for (x = 0; x < arxlAmount; x++)
{
for (y = 0; y < arxlAmount; y++)
{
if (_cartesianGrid[x, y].Active == true)
{
if (x>=0)
{
if (x == (arxlAmount-1))
{
_cartesianGrid[x, y].Active = false;
_cartesianGrid[1, y].Active = true;
}
else if(x<(arxlAmount-1))
{
_cartesianGrid[x, y].Active = false;
_cartesianGrid[x+1, y].Active = true;
}
}
}
}
}
}
公共部分类游戏:表单
{
//属性
私有位图网格;
私人Arxl[,]U cartesianGrid;
私人投资额;
常数int ARXL=4;
公共游戏()
{
初始化组件();
_arxlAmount=(gridPictureBox.Height/ARXL);//如果Height/ARXL不是偶数?
_cartesianGrid=新的Arxl[_arxlAmount,_arxlAmount];
_grid=新位图(gridPictureBox.Width、gridPictureBox.Height);
int x;
int-y;
对于(x=0;x<\u arxlAmount;x++)
{
对于(y=0;y<\u arxlAmount;y++)
{
_cartesianGrid[x,y]=新的Arxl();
}
}
固定种子(_cartesianGrid);
}
专用无效固定种子(Arxl[,]cartesianGrid)
{
_cartesianGrid[1,1]。Active=true;
}
私有void DrawArxl(位图网格,Arxl[,]cartesianGrid,int arxlAmount)
{
int x,y;
x=0;
y=0;
Graphics Graphics=Graphics.FromImage(_grid);
图形。清晰(颜色。白色);
对于(x=1;x=0)
{
如果(x==(arxlAmount-1))
{
_cartesianGrid[x,y].Active=false;
_cartesianGrid[1,y]。Active=true;
}
否则,如果计时器中的(x将图像分配给picturebox后,请尝试调用gridPictureBox.Invalidate()。这将强制picturebox重新绘制自身。我发现了您的问题
问题是您正在更新单元格位置(在此特定初始状态下将其向右移动),但for循环中的下一次迭代会从上一次迭代中找到更新的状态,因此它会一次又一次地更新单元格,当循环停止时,单元格会滚动到其初始单元格位置,其间不会重新绘制
我正在修改您的代码以添加一个UpdateList,它将打开网格扫描完成后需要打开的单元格,以避免多次更新同一个“活动点”。这将显示一个从左到右移动的点
private void ScrollBlock(Arxl[,] cartesianGrid, int arxlAmount) {
int x = 0;
int y = 0;
List<Point> updateList = new List<Point>();
for( x = 0; x < arxlAmount; x++ ) {
for( y = 0; y < arxlAmount; y++ ) {
if( _cartesianGrid[x, y].Active == true ) {
if( x >= 0 ) {
if( x == (arxlAmount - 1) ) {
_cartesianGrid[x, y].Active = false;
//_cartesianGrid[1, y].Active = true;
updateList.Add(new Point(1, y));
} else if( x < (arxlAmount - 1) ) {
_cartesianGrid[x, y].Active = false;
//_cartesianGrid[x + 1, y].Active = true;
updateList.Add(new Point(x + 1, y));
}
}
}
}
}
foreach( var pt in updateList ) {
_cartesianGrid[pt.X, pt.Y].Active = true;
}
}
private void滚动块(Arxl[,]cartesianGrid,int arxlAmount){
int x=0;
int y=0;
列表更新列表=新列表();
对于(x=0;x=0){
如果(x==(arxlAmount-1)){
_cartesianGrid[x,y].Active=false;
//_cartesianGrid[1,y]。Active=true;
Add(新的点(1,y));
}否则,如果(x<(arxlAmount-1)){
_cartesianGrid[x,y].Active=false;
//_cartesianGrid[x+1,y].Active=true;
Add(新点(x+1,y));
}
}
}
}
}
foreach(updateList中的变量pt){
_cartesianGrid[pt.X,pt.Y].Active=true;
}
}
根据您代码中的一条注释,您希望对life游戏进行编程。如果您在适当的位置更改单元格,它将不起作用,因为您必须从未更改的旧状态计算新状态。因此,您需要有两个游戏板,一个具有当前状态,另一个具有新状态。而不是全部创建新的板现在,最好有两块板并交换它们。此外,在板中存储矩形也没有意义。因此,我将这些板声明为布尔矩阵
const int CellSize = 4;
private int _boardSize;
private bool[,] _activeBoard, _inactiveBoard;
Bitmap _grid;
表单构造函数是这样更改的
public Game()
{
InitializeComponent();
_boardSize = Math.Min(gridPictureBox.Width, gridPictureBox.Height) / CellSize;
_grid = new Bitmap(gridPictureBox.Width, gridPictureBox.Height);
_activeBoard = new bool[_boardSize, _boardSize];
_inactiveBoard = new bool[_boardSize, _boardSize];
SetSeed();
}
private void SwapBoards()
{
bool[,] tmp = _activeBoard;
_activeBoard = _inactiveBoard;
_inactiveBoard = tmp;
}
我们像这样初始化游戏(作为示例)
计时器滴答作响就是这样做的
ScrollBlock();
DrawGrid();
滚动块中的逻辑是全新的。我们查看\u activeBoard
上的状态,设置\u inactiveBoard
的状态。然后交换两块板
private void ScrollBlock()
{
for (int x = 0; x < _boardSize; x++) {
for (int y = 0; y < _boardSize; y++) {
if (_activeBoard[x, y]) {
_activeBoard[x, y] = false;
int newX = x + 1;
int newY = y;
if (newX == _boardSize) {
newX = 0;
newY = (newY + 1) % _boardSize;
}
_inactiveBoard[newX, newY] = true;
}
}
}
SwapBoards();
}
最后,DrawGrid
绘制\u activeBoard
private void DrawGrid()
{
Graphics graphics = Graphics.FromImage(_grid);
graphics.Clear(Color.White);
for (int x = 0; x < _boardSize; x++) {
for (int y = 0; y < _boardSize; y++) {
var rect = new Rectangle(x * CellSize, y * CellSize, CellSize, CellSize);
if (_activeBoard[x, y]) {
graphics.FillRectangle(Brushes.Black, rect);
} else {
graphics.DrawRectangle(Pens.Black, rect);
}
}
}
gridPictureBox.Image = _grid;
}
private void DrawGrid()
{
Graphics Graphics=Graphics.FromImage(_grid);
图形。清晰(颜色。白色);
对于(int x=0;x<_boardSize;x++){
用于(int y)
private void DrawGrid()
{
Graphics graphics = Graphics.FromImage(_grid);
graphics.Clear(Color.White);
for (int x = 0; x < _boardSize; x++) {
for (int y = 0; y < _boardSize; y++) {
var rect = new Rectangle(x * CellSize, y * CellSize, CellSize, CellSize);
if (_activeBoard[x, y]) {
graphics.FillRectangle(Brushes.Black, rect);
} else {
graphics.DrawRectangle(Pens.Black, rect);
}
}
}
gridPictureBox.Image = _grid;
}