Here is my header file for my Pacman movement and I don't understand why collision detection is not working and why my Pacman does not move within the blank area and is able to move over tiles.
Basically, I need some type of algorithm for Pacman's movement in the game. Right now he moves continuously but there is no collision detection working.
#pragma once
int X = 320;
int Y = 240;
//Pacman implementation
#include "DarkGDK.h"
#include <time.h>
#include "Sprites.h"
#include "Input.h"
#include "Pacman.h"
int SpriteID;
//#define DELTA 100
enum Direction {Left, Right, Up, Down, Stop};
Direction MyDirection = Stop;
//Controls the movement of Pacman
void PacmanMove()
{
//Only if I include PacmanMove() in game loop
/*static clock_t c = clock();
if ( clock() - c < DELTA )
return;
c = clock();*/
int column = X/30;
int row = Y/30;
if ( (X/30.0f == row) && (Y/30.0f == column)
{
if ( CheckLeft() )
MyDirection = Left;
else if ( CheckRight() )
MyDirection = Right;
else if (CheckUp())
MyDirection = Up;
else if (CheckDown())
MyDirection = Down;
}
//Check for collisions
if (dbSpriteCollision ( 3, 7 ) || dbSpriteCollision ( 4, 7 ) || dbSpriteCollision ( 5, 7 ) || dbSpriteCollision ( 6, 7 ) )
MyDirection = Stop;
if ( MyDirection == Stop )
{
dbSprite(SpriteID, X, Y, SpriteID);
}
else if ( MyDirection == Left )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 2 );
//Show animated sprite
//dbSetSpriteImage ( 4, 4 );
//Controls the position of Pacman
//dbPlaySprite ( ID, start frame, end frame, delay );
SpriteID = 4;
dbSprite ( SpriteID, X, Y, 4 );
dbShowSprite ( SpriteID );
dbPlaySprite ( SpriteID, 1, 2, 200 );
//Decrement PacMan
X = X - 1;
if (dbSpriteVisible ( 3 ))
dbHideSprite ( 3 );
else if (dbSpriteVisible ( 5 ) )
dbHideSprite ( 5 );
else if (dbSpriteVisible ( 6 ) )
dbHideSprite (6);
}
else if ( MyDirection == Right )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 2 );
//Show animated sprite
//dbSetSpriteImage ( 3, 3 );
SpriteID = 3;
//Controls the position of Pacman
dbSprite ( SpriteID, X, Y, 3 );
dbShowSprite ( SpriteID );
dbPlaySprite ( SpriteID, 1, 2, 200 );
//Increment PacMan
X = X + 1;
if (dbSpriteVisible ( 4 ))
dbHideSprite ( 4 );
else if (dbSpriteVisible ( 5 ) )
dbHideSprite ( 5 );
else if (dbSpriteVisible ( 6 ) )
dbHideSprite (6);
}
else if ( MyDirection == Up )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 2 );
//Show animated sprite
//dbSetSpriteImage ( 5, 5 );
SpriteID = 5;
//Controls the position of Pacman
dbSprite ( SpriteID, X, Y, 5 );
dbShowSprite ( SpriteID );
dbPlaySprite ( SpriteID, 1, 2, 200 );
Y = Y - 1;
if (dbSpriteVisible ( 3 ))
dbHideSprite ( 3 );
else if (dbSpriteVisible ( 4 ) )
dbHideSprite ( 4 );
else if (dbSpriteVisible ( 6 ) )
dbHideSprite (6);
}
else if ( MyDirection == Down )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 2 );
//Show animated sprite
//dbSetSpriteImage ( 6, 6 );
SpriteID = 6;
//Controls the position of Pacman
dbSprite ( SpriteID, X, Y, 6 );
dbShowSprite ( SpriteID );
dbPlaySprite ( SpriteID, 1, 2, 200 );
Y = Y + 1;
if (dbSpriteVisible ( 3 ))
dbHideSprite ( 3 );
else if (dbSpriteVisible ( 5 ) )
dbHideSprite ( 5 );
else if (dbSpriteVisible ( 4 ) )
dbHideSprite (4);
}
}