c# - 照片矩阵
问题描述
所以我有一个代码通过资源将图像注入我的项目 Image foodWorld = Resources.orange
,我想用这张照片制作一个矩阵,所以它看起来像这样:
我有这段代码,但我不知道如何绘制矩阵。另外,我不知道这是否是正确的绘制方法:
this.Width = 400;
this.Height = 300;
Bitmap b = new Bitmap(this.Width, this.Height);
for(int i = 0; i < this.Height; i++)
{
for(int j = 0; j < this.Width; j ++)
{
//fill the matrix
}
}
解决方案
我对 WinForms 不太熟悉,但在 WPF 中,我会这样做:
var columns = 15;
var rows = 10;
var imageWidth = 32;
var imageHeight = 32;
var grid = new Grid();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
//Get the image in your project; I'm not sure how this is done in WinForms
var b = new Bitmap(imageWidth, imageHeight);
//Display it
var pictureBox = new PictureBox();
pictureBox.Image = b;
//Set the position
Grid.SetColumn(j, pictureBox);
Grid.SetRow(i, pictureBox);
//Insert into the "matrix"
grid.Children.Add(pictureBox);
}
}
对于移动吃豆人,重复上述步骤,但仅适用于一张图像。存储对当前位置的引用以及按下某些键时,
- 为它的边距设置动画,直到它看起来在相邻的单元格中(例如,如果每个单元格都是 16 像素宽并且 pacman 应该在任何给定单元格的中心,则将右边距设置 16 个像素以使其进入单元格对等等)。
- 移动到另一个单元格后,根据上次移动的方向设置新的行和列。
- 如果新位置有水果,则取出该位置的水果并将其从
Grid
. 您可以通过使用myGrid.Children[currentRow * totalColumns + currentColumn]
假设得到它,currentRow
并且currentColumn
都是从零开始的。 - 对它必须移动到的每个单元格重复此操作。
这确实意味着矩阵将具有固定大小,但在 WPF 中,有一个Viewbox
,这对于这些类型的场景很方便。此外,将 pacman 的 z-index 设置为大于水果,使其始终位于顶部。
推荐阅读
- php - Laravel - 所有默认验证错误消息的列表
- javascript - 如何在页面上克隆或添加多个 ck 编辑器
- javascript - 等待多个非嵌套回调
- tfs - 在 TFS 构建中使用 ClickOnce-Manifest 构建错误
- c# - MongoDB C# 驱动程序 (2.7) 和序列化问题
- javascript - 如果在页面加载禁用按钮
- python - 将 Mathematica 函数转换为 Java 形式
- angular - ngrx:如何订阅状态字段的更改
- oauth - EWS OAuth .net 核心 2.1
- c# - 在 ASP.NET Core 2.1 中添加自定义 TagHelper