Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>if you calculate the position of the pacman to correspond to the landTitle array by dividing the pacmanPosX and pacmanPosY with the size of each square (32) and then cast it to an int. then you could simply check if the position in front of you is set to 1 or 0 in the landTile and in that way you can get a very simple and fast collision.</p> <p>Tell me if you get into any hindrance</p> <p><strong>EDIT</strong></p> <p>What I mean is that you an array that explains where all the walls should be in your map. Because of that you don't have to do any sprite collision, instead you can do a simple collision check based on where you are supposed to be in the array. Lets consider the case where you are supposed to move up on the map:</p> <pre><code>int colPosX = pacmanPosX / 32; int colPosY = (pacmanPosY-1) / 32; bool collision = (bool)landTitle[colPosX][colPosY]; </code></pre> <p>if <em>collision</em> is true then you have a collision one pixel upwards from your position, thats what the <em>-1</em> in the calculation of <em>colPosY</em> is for.</p> <p><strong>Edit 2</strong></p> <p>If i should edit your code then I would do it more like this</p> <pre><code>int blockSize = 32; int pacmanPosX = blockSize * 1; int pacmanPosY = blockSize * 1; 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-&gt;Clear( 0 ); for (int indexX = 0; indexX &lt;= 14; indexX++) { for (int indexY = 0; indexY &lt;= 16; indexY++) { int tile = landTile[indexY][indexX]; tileSet[tile]-&gt;CopyTo( m_Screen, indexX * blockSize, indexY * blockSize ); } } if (GetAsyncKeyState( VK_UP ) &amp;&amp; CheckCollision(0,-1)) pacmanPosY -=2; if (GetAsyncKeyState( VK_DOWN ) &amp;&amp; CheckCollision(0, 1)) pacmanPosY += 2; if (GetAsyncKeyState( VK_RIGHT ) &amp;&amp; CheckCollision(1, 0)) pacmanPosX += 2; if (GetAsyncKeyState( VK_LEFT ) &amp;&amp; CheckCollision(-1,0)) pacmanPosX -= 2; Sleep( 10 ); } bool Game::CheckCollision(int dirX, int dirY){ return (bool)landTile[(pacmanPosX+dirX)/blockSize][(pacmanPosY+dirY)/blockSize]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload