在此處輸入影像描述
紅方:播放器圖片框;
藍色方塊:終點;
黑方:墻;
我在 PictureBox 中組織了一個迷宮。
玩家是一個特定大小的物件,其 Picturebox 是已知的。我想讓玩家順利通過迷宮,墻壁阻止他們通過。
如圖所示,目前播放器穿過墻壁存在問題。
public partial class Form1 : Form
{
private int XTILES = 25;
private int YTILES = 25;
private int TILESIZE = 10;
private PictureBox[,] mazeTiles;
}
public void CreateNewMaze()
{
mazeTiles = new PictureBox[XTILES, YTILES];
for (int i = 0; i < XTILES; i )
{
for (int j = 0; j < YTILES; j )
{
mazeTiles[i, j] = new PictureBox();
int xPosition = (i * TILESIZE) 25;
int yPosition = (j * TILESIZE) 10;
mazeTiles[i, j].SetBounds(xPosition, yPosition, TILESIZE, TILESIZE);
this.Controls.Add(mazeTiles[i, j]);
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
bool isRoad = mazeTiles[(PlayerPictureBox.Left - 25) / TILESIZE, (PlayerPictureBox.Top - 10) / TILESIZE].BackColor != Color.Black;
switch (keyData)
{
case Keys.Left:
if (isRoad)
PlayerPictureBox.Left -= 10;
return true;
case Keys.Right:
if (isRoad)
PlayerPictureBox.Left = 10;
return true;
case Keys.Up:
if (isRoad)
PlayerPictureBox.Top -= 10;
return true;
case Keys.Down:
if (isRoad)
PlayerPictureBox.Top = 10;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
uj5u.com熱心網友回復:
您需要檢查墻壁是否在預期移動的方向上。您現在正在做的是檢查當前位置的道路。
嘗試這樣的事情:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
int dx = 0;
int dy = 0;
switch (keyData)
{
case Keys.Left: dx = -1; break;
case Keys.Right: dx = 1; break;
case Keys.Up: dy = -1; break;
case Keys.Down: dy = 1; break;
default: return base.ProcessCmdKey(ref msg, keyData);
}
var x = dx (PlayerPictureBox.Left - 25) / TILESIZE;
var y = dy (PlayerPictureBox.Top - 10) / TILESIZE;
bool isRoad = mazeTiles[x, y].BackColor != Color.Black;
if (isRoad)
{
PlayerPictureBox.Left = dx * TILESIZE;
PlayerPictureBox.Top = dy * TILESIZE;
}
return true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516752.html
標籤:C#表格迷宫
上一篇:.map功能有問題
