Files
snake/Snake/Map.cs
2026-02-03 13:44:30 +01:00

87 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
namespace Snake
{
public class Map
{
public Cell[,] Cells;
private int _h;
private int _w;
private const int CELL_SIZE = 20;
public Map(int h, int w)
{
Cells = new Cell[h, w];
_h = h;
_w = w;
for (int x = 0; x < _w; x++)
{
for (int y = 0; y < _h; y++)
{
Cells[x, y] = new Cell(x, y, CellTypes.None);
}
}
}
public void SetCell(Cell cell)
{
Cells[cell.X,cell.Y] = cell;
}
public void SwapCells(Cell cell1, Cell cell2)
{
int tmpX = cell1.X;
int tmpY = cell1.Y;
cell1.X = cell2.X; cell1.Y = cell2.Y;
cell2.X = tmpX; cell2.Y = tmpY;
Cells[cell1.X, cell1.Y] = cell1;
Cells[cell2.X, cell2.Y] = cell2;
}
public void Render(Graphics graphics)
{
SolidBrush brush = new SolidBrush(Color.RebeccaPurple);
for (int x = 0; x < _w; x++)
{
for (int y = 0; y < _h; y++)
{
Cell cell = Cells[x,y];
if (cell.Type == CellTypes.None)
{
brush.Color = Color.LightGray;
}
else if (cell.Type == CellTypes.Snake)
{
brush.Color = cell.Color;
}
else if (cell.Type == CellTypes.Food)
{
brush.Color = Color.Red;
}
int totalHeight = CELL_SIZE * _h;
graphics.FillRectangle(
brush,
x * CELL_SIZE,
totalHeight - (y + 1) * CELL_SIZE,
CELL_SIZE,
CELL_SIZE
);
}
}
}
}
}