1.[文件]
Game.cpp ~ 2KB
下载(134)
// Game.cpp: implementation of the Game class.
//
//
#include "Game.h"
#include
#include
using namespace std;
char map[WIDTH][HEIGHT] ;
const char WAY = ' ';//路
const char WALL= '#';//墙
const char MOUSE = '&';//老鼠
const char DOOR = '>';//门
const char PASS = '.';//走过的路
const char DEAD_PASS='X';//死路
const int LEFT = 0;
const int RIGHT=1;
const int DOWN = 2;
const int UP =3;
//
// Construction/Destruction
//
Game::Game():mouse(0,2)
{
}
Game::~Game()
{
}
//启动游戏,初始化地图
void Game::init_map()
{
srand(time(NULL));
for(int i = 0 ; i
{
for(int j= 0 ; j
{
if(i==0 || i==WIDTH-1 || j==0 || j==HEIGHT-1)
map[i][j] = WALL;
else
map[i][j] = rand()%4==0?WALL:WAY;
}
}
map[0][2]=MOUSE;
map[WIDTH-1][HEIGHT-2]=DOOR;
}
//显示地图
void Game::show_map()
{
system("cls");
for(int i = 0 ; i
{
for(int j= 0 ; j
{
cout<< map[i][j];
}
cout<
}
}
//开始游戏
void Game::start()
{
int x,y;
while(1)//mouse.getX()!=WIDTH-1 &&mouse.getY()!=HEIGHT-2
{
x=mouse.getX();
y=mouse.getY();
map[x][y]=MOUSE;
show_map();
if((map[x][y+1]!=WALL && map[x][y+1]!=DEAD_PASS &&map[x][y+1]!=PASS)&& test_bound(x,y+1)==true)
{
map[x][y]=PASS;
mouse.goRight();
}
else if((map[x+1][y]!=WALL && map[x+1][y]!=DEAD_PASS&&map[x+1][y]!=PASS)&& test_bound(x+1,y)==true)
{
map[x][y]=PASS;
mouse.goDown();
}
else if((map[x][y-1]!=WALL && map[x][y-1]!=DEAD_PASS&&map[x][y-1]!=PASS)&& test_bound(x,y-1)==true)
{
map[x][y]=PASS;
mouse.goLeft();
}
else if((map[x-1][y]!=WALL && map[x-1][y]!=DEAD_PASS && map[x-1][y]!=PASS)&& test_bound(x-1,y)==true)
{
map[x][y]=PASS;
mouse.goUp();
}
else
{
map[x][y]=DEAD_PASS;
int rb=mouse.reBack();
if(rb==-1)
break;
}
if(mouse.getX()==WIDTH-1 && mouse.getY()==HEIGHT-2)
break;
}
if(mouse.getX()==WIDTH-1 && mouse.getY()==HEIGHT-2)
cout<
else
cout<
}
//测试坐标是否合法
bool Game::test_bound(int x , int y)
{
if(x>0 && x0 && y
return true;
return false;
}