0
所以我正在製作一個非常簡單的程序,我希望一個精靈與另一個精靈發生碰撞。 我通過使用二維數組創建一個包含圖像的網格來製作'層次'。 我將如何實現簡單的碰撞?我會很感激代碼示例,所以我實際上可以看到 發生了什麼事情。謝謝。這裏的代碼:與2維數組中的.bmp的簡單衝突?
int pacmanPosX = 32;
int pacmanPosY = 32;
Sprite pacman (new Surface("assets/tiles/pacman.png"), 1);
Surface* tileSet[2];
int landTile[14][16] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,},
{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,},
{1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}};
void Game::Init()
{
// put your initialization code here; will be executed once
tileSet[0] = new Surface("assets/tiles/void.bmp"); // Sets up data for tileSet
tileSet[1] = new Surface("assets/tiles/wall.bmp");
}
void Game::Tick(float a_DT)
{
m_Screen->Clear(0);
for (int indexX = 0; indexX <= 14; indexX++)
{
for (int indexY = 0; indexY <= 16; indexY++)
{
int tile = landTile[indexY][indexX];
tileSet[tile]->CopyTo(m_Screen, indexX * 32, indexY * 32);
}
}
pacman.Draw(pacmanPosX, pacmanPosY, m_Screen);
Sleep(10);
int colPosX = pacmanPosX /32;
int colPosY = (pacmanPosY-1)/32;
bool collision = (bool)landTile[colPosX][colPosY];
int direction;
if(collision = false)
{
if (GetAsyncKeyState(VK_UP)) pacmanPosY -=2;
if (GetAsyncKeyState(VK_DOWN)) pacmanPosY += 2;
if (GetAsyncKeyState(VK_RIGHT)) pacmanPosX += 2;
if (GetAsyncKeyState(VK_LEFT)) pacmanPosX -= 2;
}
}
這是行不通的,現在角色根本無法移動。爲了您的信息,每個精靈都是 32x32,包括字符。
對不起,我不明白。我能得到的唯一的東西是:bool collision = false; int pacmanposition =(pacmanPosX/32)+(pacmanPosX/32); if(pacmanposition = landTile [1])//這是應該是實體的圖塊。 { collision = true; } 我甚至不會嘗試。我只是不知道自己在這裏做什麼,對我來說這太抽象了,不知何故。我從來沒有料到這比指針和類以及我所有的更難。 – Turbosheep
我會在答案中進行編輯,以便進一步解釋 – Udrian
對不起,如果我脫落密密麻麻,但爲什麼除以32部分?我沒有得到最後的...... =(布爾)landtitle [colPosx] [colPosY]行。我從來沒有見過像這樣的數組之前使用大括號的布爾。現在這非常令人沮喪,這應該不是那麼難。我仍然沒有得到任何這一點。哦,檢查我的問題的結束......在你的代碼中似乎有一個邏輯錯誤。 – Turbosheep