/*
NOTE: This code was written using Microsoft's Visual C++ v 6.0.
I don't know how well it will work with other compilers.
You also will need the Direct X 7.0 SDK, and to install the lib
and include files from the SDK to your C++ lib and include
folders. You can get the DirectX 7.0 SDK (large!) here:
http://download.microsoft.com/download/win98SE/SDK/7.0/W9X/EN-US/dx7sdk-700.1.exe
========================
A* Pathfinder - The Maze
========================
By Patrick Lester, pwlester@policyalmanac.org
A fuller implementation than the Basics demo. This is the
template you will want to work from if you want to write or
customize your own version of A*.
Instructions
------------
- Press enter to start pathfinding mode
- left or right click anywhere on the map to make the smiley
go there.
- search times are printed in the upper left hand coner
- map may be edited by pressing enter button to toggle to
map editing mode and left clicking on the map.
*/
#define WINDOW_NAME "Program" //Visible when game is minimized
#include "../common/launchWindows.h"
#include "../common/dxLibrary.h"
#include "aStarLibrary.h"
//-----------------------------------------------------------------------------
// Global variables and constants
//-----------------------------------------------------------------------------
cImage* wallImage; cImage* mapImage;
cImage* mousePointer;
cImage* smiley; cImage* chaser;
char smileyActivated = 0;
int xLoc [4]; int yLoc [4]; int speed [4];
int searchTime, g_showDirections=0;
//-----------------------------------------------------------------------------
// Function Prototypes: where necessary
//-----------------------------------------------------------------------------
void CheckUserInput (void);
void CreateMapImage (void);
void CreateWallImage (void);
void DrawMap (void);
void EditMap (void);
void LoadGraphics (void);
void LoadMapData (void);
void LoadUnitData (void);
void MoveChaser (int ID);
void MoveSmiley (void);
void MoveSprite(int ID);
void RenderScreen (void);
void SaveMapData (void);
void ShowDirections (void);
//-----------------------------------------------------------------------------
// Name: GameMain
// Desc: Launch and run game.
//-----------------------------------------------------------------------------
void GameMain (HWND hwnd)
{
Graphics (800,600,16,hwnd);
LoadMapData();
LoadUnitData();
LoadGraphics();
InitializePathfinder();
//Main game loop
while (!KeyDown(27)) //While escape key not pressed
{
CheckUserInput();
//Move smiley
if (smileyActivated == 1) MoveSmiley();
//Move chasers
if (smileyActivated == 1)
for (int ID = 2; ID <= 3; ID++)
MoveChaser(ID);
RenderScreen(); //draw stuff on screen
CheckWinMessages();
}
SaveMapData();
EndPathfinder();
EndGraphics();
return;
}
//-----------------------------------------------------------------------------
// Name: CheckUserInput
// Desc: Process key and mouse presses.
//-----------------------------------------------------------------------------
void CheckUserInput (void)
{
if (KeyHit(13))
{
smileyActivated = 1 - smileyActivated;
if (smileyActivated == 1) CreateMapImage();
}
if (smileyActivated == 0) EditMap();
//Show/hide directions by pressing space bar.
if (KeyHit(32)) g_showDirections = 1-g_showDirections;
}
//-----------------------------------------------------------------------------
// Name: CreateMapImage
// Desc: Creates the map image
//-----------------------------------------------------------------------------
void CreateMapImage (void)
{
FreeImage(mapImage);
mapImage = CreateImage(800,600); //create a new map image.
SetBuffer (0,mapImage);
Color(0,0,255);//set default color to blue
for (int x = 0; x <= 79; x++){
for (int y = 0; y <= 59; y++){
//Draw blue walls
if (walkability[x][y] == unwalkable)
Rect(x*10,y*10,10,10,1);
}}
SetBuffer (2);
Color(255,255,255);//set default color to white
}
//-----------------------------------------------------------------------------
// Name: CreateWallImage
// Desc: Creates a blue 10x10 wall block image
//-----------------------------------------------------------------------------
void CreateWallImage (void)
{
wallImage = CreateImage(10,10); //create a new map image.
SetBuffer (0,wallImage);
Color(0,0,255);//set default color to blue
Rect(0,0,10,10,1);
SetBuffer (2);
Color(255,255,255);//set default color to white
}
//-----------------------------------------------------------------------------
// Name: DrawMap
// Desc: Edit the map by left clicking while in edit mode.
//-----------------------------------------------------------------------------
void DrawMap(void)
{
if (smileyActivated == 1)
DrawBlock (mapImage,0,0);
else
{
for (int x = 0; x <= 79; x++){
for (int y = 0; y <= 59; y++){
//Draw blue walls
if (walkability[x][y] == unwalkable)
DrawBlock (wallImage,x*10,y*10);
}}
}
}
//-----------------------------------------------------------------------------
// Name: EditMap
// Desc: Edit the map by left clicking while in edit mode.
//-----------------------------------------------------------------------------
void EditMap (void)
{
if (MouseHit(1))
{
int x = MouseX()/10;
int y = MouseY()/10;
walkability[x][y] = 1-walkability[x][y];
}
}
//-----------------------------------------------------------------------------
// Name: LoadGraphics
// Desc: Loads graphics
//-----------------------------------------------------------------------------
void LoadGraphics (void)
{
SetFont("Arial",14);
CreateWallImage();
CreateMapImage();
mousePointer = LoadImage("../../Graphics/red_pointer.bmp");
MaskImage (mousePointer, 255,255,255);
AutoMidHandle(true);
smiley = LoadImage("../../Graphics/smiley x 10.bmp");
MaskImage (smiley, 0,0,0);
chaser = LoadImage("../../Graphics/ghost x 10.bmp");
MaskImage (chaser, 0,0,0);
}
//-----------------------------------------------------------------------------
// Name: LoadMapData
// Desc: Load any pre-existing map when launching the program.
//-----------------------------------------------------------------------------
void LoadMapData (void)
{
ifstream filein;
filein.open("myTerrainData.dat",ios::nocreate);
if (filein)
{
for (int x = 0; x <= 79; x++){
for (int y = 0; y <= 59; y++){
filein >> walkability [x][y];//or filein.read(buffer,length)
if (walkability [x][y] > 1) walkability [x][y] = 0;
}}
filein.close();
}
else //initialize the map to completely walkable
{
for (int x = 0; x <= 79; x++){
for (int y = 0; y <= 59; y++){
walkability [x][y] = walkable;
}}
}
}
//-----------------------------------------------------------------------------
// Name: LoadUnitData
// Desc: Initialize unit-related data
//-----------------------------------------------------------------------------
void LoadUnitData (void)
{
xLoc[1] = 125 ; yLoc[1] = 325; //initial smiley location
xLoc[2] = 725 ; yLoc[2] = 325; //initial chaser location
xLoc[3] = 465 ; yLoc[3] = 145; //initial chaser location
speed[1] = 5;//smiley speed
speed[2] = 3;//chaser
speed[3] = 2;//chaser
}
//-----------------------------------------------------------------------------
// Name: MoveChaser
// Desc: This subroutine moves the chasers/ghosts around on the screen.
// In this case the findPath function is accessed automatically when
// a chaser reaches the end of his current path. The path info
// is also updated occasionally.
//-----------------------------------------------------------------------------
void MoveChaser (int ID)
{
int targetID = 1; //ID of target (the smiley)
//1. Find Path: If smiley and chaser are not at the same location on the
// screen and no path is c
评论0