Yah, so I'm having trouble geting my ghosts to move in an orderly fashion. They are so chaotic and sometimes get stuck.That is the first code I will post. The other code is for my Pacman movement. I want him to only be able to change directions whenever he is sguare in an empty or dot tile. In other words when row%30 == 0 and column%30 == 0. I can't figure out where I can place this code so it will work.
Ok here is the Ghost code
#pragma once
#include "Pacman.h"
#include "Maze.h"
#include "stdlib.h"
//Blinky coordinates
int XB = 270;
int YB = 240;
//Inky coordinates
int XI = 270;
int YI = 240;
//Pinky coordinates
int XP = 270;
int YP = 240;
//Clyde coordinates
int XC = 270;
int YC = 240;
int blinkyDirection = 1;
int inkyDirection = 2;
int pinkyDirection = 1;
int clydeDirection = 2;
int path;
void MoveGhosts(int &XG, int &YG, int GSpriteID,int ImageID,int &ghostDirection)
{
//Maze X
int column = YG;
//Maze Y
int row = XG;
int collision_top = maze[((column - 1)/30)][((row + 15)/30)];
int collision_left = maze[(column + 15)/30][((row - 1)/30)];
int collision_right = maze[(column + 15)/30][((row + 31)/30)];
int collision_bottom = maze[((column + 31)/30)][(row + 15)/30];
if (row%30 && column%30)
{
path = rand()%4;
}
//Path Directions
//Only assign paths when there is a fork in the maze
if ( (collision_left !=7 && collision_top !=7 && collision_bottom != 7 && collision_right != 7) || (collision_left !=7 && collision_top !=7 && collision_bottom == 7 && collision_right != 7) || (collision_left !=7 && collision_top !=7 && collision_bottom != 7 && collision_right == 7) || (collision_left !=7 && collision_top ==7 && collision_bottom != 7 && collision_right != 7) || (collision_left ==7 && collision_top !=7 && collision_bottom != 7 && collision_right != 7) )
{
if ( path >= 0 && path <= 1 )
ghostDirection = 1;
else if ( path > 1 && path <= 2 )
ghostDirection = 2;
else if ( path > 2 && path <= 3)
ghostDirection = 3;
else if (path > 3 && path <= 4)
ghostDirection = 4;
}
//Left
if ( (ghostDirection == 1))// && (collision_left != 7) )
{
//Check for top left and bottom left corner
if (collision_left == 7 && collision_top == 7)
{
ghostDirection = 4;
}
else if (collision_left == 7 && collision_bottom == 7)
{
ghostDirection = 3;
}
else
{
//Controls the position of Ghost
//dbPlaySprite ( ID, start frame, end frame, delay );
dbSprite ( GSpriteID, XG, YG, ImageID );
//Decrement Ghost
XG = XG - 1;
}
}
//Right
if ( (ghostDirection == 2))// && (collision_right != 7) )
{
//Check for top right and bottom right corner
if (collision_right == 7 && collision_top == 7)
{
ghostDirection = 4;
}
else if (collision_right == 7 && collision_bottom == 7)
{
ghostDirection = 3;
}
else
{
//Controls the position of Ghost
dbSprite ( GSpriteID, XG, YG, ImageID );
//dbShowSprite ( SpriteID );
//dbPlaySprite ( SpriteID, 1, 2, 200 );
//Increment Ghost
XG = XG + 1;
}
}
//Up
if ( (ghostDirection == 3))// && ( (collision_top) != 7 ) )
{
//Check for top left and top right corner
if (collision_left == 7 && collision_top == 7)
{
ghostDirection = 2;
}
else if (collision_right == 7 && collision_top == 7)
{
ghostDirection = 1;
}
else
{
//Controls the position of Ghost
dbSprite ( GSpriteID, XG, YG, ImageID );
//dbShowSprite ( SpriteID );
//dbPlaySprite ( SpriteID, 1, 2, 200 );
//Decrement Ghost
YG = YG - 1;
}
}
//Down
if ( (ghostDirection == 4))// && (collision_bottom != 7) )
{
//Check for bottom left and bottom right corner
if (collision_left == 7 && collision_bottom == 7)
{
ghostDirection = 2;
}
else if (collision_right == 7 && collision_bottom == 7)
{
ghostDirection = 1;
}
else
{
//Controls the position of Ghost
dbSprite ( GSpriteID, XG, YG, ImageID );
//dbShowSprite ( SpriteID );
//dbPlaySprite ( SpriteID, 1, 2, 200 );
//Increment Ghost
YG = YG + 1;
}
}
//Ghost Maze wrapping
if (XG == 0 && ( (YG = 300) || ( (YG >285) && (YG < 315) )) )
{
XG = 540;
XG--;
}
else if (XG == 540 && ( (YG = 300) || ( (YG >285) && (YG < 315) )) )
{
XG = 0;
XG++;
}
}
void GhostAI()
{
//Blinky ( red "Shadow" or chaser )
//Increase the speed a little less than Pinky
MoveGhosts(XB,YB,168,13,blinkyDirection);
//Inky ( blue "Bashful" or fickle)
//Change directions slightly less quickly than Clyde
MoveGhosts(XI,YI,205,10,inkyDirection);
//Pinky ( pink "Speedy" or ambusher)
//Increase the speed by 1
MoveGhosts(XP,YP,206,11,pinkyDirection);
//Clyde ( orange "Pokey" or stupid)
//Change directions more quickly in an unorderly fashion
MoveGhosts(XC,YC,207,12,clydeDirection);
}
Here is the Pacman code
#pragma once
int X = 270;
int Y = 480;
//Pacman implementation
#include "Sprites.h"
#include "Input.h"
#include "Maze.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();*/
//Maze X
int column = Y;
//Maze Y
int row = X;
int collision_top = maze[(column)/30][(row + 15)/30];
int collision_left = maze[(column + 15)/30][(row)/30];
int collision_right = maze[(column + 15)/30][(row + 30)/30];
int collision_bottom = maze[(column + 30)/30][(row + 15)/30];
if ( CheckLeft())
MyDirection = Left;
else if ( CheckRight())
MyDirection = Right;
else if (CheckUp())
MyDirection = Up;
else if (CheckDown())
MyDirection = Down;
if ( (MyDirection == Left) && (collision_left != 7) )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 320 );
//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) && (collision_right != 7) )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 320 );
//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 PacMam
X = X + 1;
if (dbSpriteVisible ( 4 ))
dbHideSprite ( 4 );
else if (dbSpriteVisible ( 5 ) )
dbHideSprite ( 5 );
else if (dbSpriteVisible ( 6 ) )
dbHideSprite (6);
}
else if ( (MyDirection == Up) && ( (collision_top) != 7 ) )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 320 );
//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 );
//Decrement Pacman
Y = Y - 1;
if (dbSpriteVisible ( 3 ))
dbHideSprite ( 3 );
else if (dbSpriteVisible ( 4 ) )
dbHideSprite ( 4 );
else if (dbSpriteVisible ( 6 ) )
dbHideSprite (6);
}
else if ( (MyDirection == Down) && (collision_bottom != 7) )
{
//Delete Pacman_still sprite
dbDeleteSprite ( 320 );
//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 );
//Increment Pacman
Y = Y + 1;
if (dbSpriteVisible ( 3 ))
dbHideSprite ( 3 );
else if (dbSpriteVisible ( 5 ) )
dbHideSprite ( 5 );
else if (dbSpriteVisible ( 4 ) )
dbHideSprite (4);
}
//Maze wrapping
if (X == 0 && ( (Y = 300) || ( (Y >285) && (Y < 315) )) )
{
X = 540;
X--;
}
else if (X == 540 && ( (Y = 300) || ( (Y >285) && (Y < 315) )) )
{
X = 0;
X++;
}
}