为了账号安全,请及时绑定邮箱和手机立即绑定

动画控制函数(设置光标位置)需要包含什么头文件?

没用头文件加进去都是错?用linux g++编译器

正在回答

4 回答

/* .h文件  */
#ifndef _maze_h_
#define _maze_h_

#include <stdio.h>

 #define wall 1
 // #typedef 1 wall;
 #define road 0
// #typedef 0 road;
 
int map[10][11] = {  
					 {road,road,road,road,road,road,road,road,road,road,road},	
                     {road,wall,wall,wall,wall,wall,wall,wall,wall,wall,road},
                     {road,wall,wall,wall,wall,road,wall,wall,road,wall,road},
                     {road,wall,road,road,road,road,wall,wall,road,wall,road},
                     {road,wall,wall,wall,wall,road,wall,wall,road,wall,road},
                     {road,wall,wall,road,road,road,road,road,road,wall,road},
                     {road,wall,wall,road,wall,wall,road,road,road,wall,road},
                     {road,wall,road,road,wall,wall,wall,wall,wall,wall,road},
                     {road,wall,road,wall,wall,wall,wall,wall,wall,wall,road},
					 {road,road,road,road,road,road,road,road,road,road,road}
                 };

class maze
{
public:
	maze(int x,int y):m_X(x),m_Y(y){}
	void setMazeMap(int x,int y);
//	void setMazeWall(char c);
//	void drawMap();
private:
	int m_X; //
	int m_Y; //
};


enum direction{Up,Down,Left,Right};
class mazer
{
public:
	mazer():direct(Up){}
	void setMazerPosition(int x,int y);
	void resetMazerPosition(int x,int y);
	void MazerStart(void);
	direction getDirection(void);
	void setDirection(direction D);
	int getPx(void);
	int getPy(void);
private:	
	direction direct;
	int m_Px;
	int m_Py;
};


#define moveto(x,y) printf("\033[%d;%dH",(x),(y))
#define clear()  	printf("\033[2J")
#define setRed()	printf("\033[31m")
#define setBlack()  printf("\033[30m")
//#define setBlack() 	printf("\033[30m\033[4m")
#endif 

/* .cpp文件 */
#include <iostream>
#include <string>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "maze.h" 
using namespace std;
int main()
{
	clear();
	moveto(1,1);
	////////////////////////////////////////////
	maze myMaze(10,11);
	myMaze.setMazeMap(10,11);

	mazer myMazer;
	myMazer.setMazerPosition(10,3);
	myMazer.MazerStart();

	////////////////////////////////////////////
	moveto(15,1);	
	setBlack();
	return 0;
}

void maze::setMazeMap(int x,int y)
{
     int i=0,j=0;
     for(i=0;i<x;i++)
     {
         for(j=0;j<y;j++)
         {
         	if(map[i][j]==wall)
         		cout << "*";
			else
				cout << " ";
         }
         cout <<endl;
     }
}

void mazer::setMazerPosition(int x,int y)
{
	m_Px = x;
	m_Py = y;

	moveto(x,y);
	setRed();
	cout << "P" << endl ;
//	sleep(1); // n second
	usleep(500000); //n usecond
}
void mazer::resetMazerPosition(int x,int y)
{
	moveto(x,y);
	setBlack();
	cout << " " << endl;
}

int mazer::getPx()
{
	return m_Px;
}
int mazer::getPy()
{
	return m_Py;
}

direction mazer::getDirection()
{
	return direct;
}
void mazer::setDirection(direction D)
{
	direct = D;
}

void mazer::MazerStart()
{
	int Px,Py;
	int over=0,step=0;
	Px = getPx();
	Py = getPy();
	while(1)
	{
		direction D = getDirection();
		switch (D)
		{
			case Up:
				resetMazerPosition(Px,Py);
				setMazerPosition(Px-1,Py);
				Px = getPx();
				Py = getPy();
				if(map[Px-1][Py+1-1] != wall)
				{
					if((map[Px-1-1][Py-1] !=wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
					else setDirection(Right);
				}	
				else if(map[Px-1-1][Py-1] == wall) 
				{
					if(map[Px-1][Py-1-1] != wall)setDirection(Left);
					else setDirection(Down);
				}	
				else setDirection(Up);
				break;		
			case Down:
			    resetMazerPosition(Px,Py);
			    setMazerPosition(Px+1,Py);
				Px = getPx();
				Py = getPy();
				if(map[Px-1][Py-1-1] != wall) 
				{
					if((map[Px+1-1][Py-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
					else setDirection(Left);
				}
				else if(map[Px+1-1][Py-1] == wall)
				{
					if(map[Px-1][Py+1-1] != wall)setDirection(Right);
					else setDirection(Up);
				}
				else setDirection(Down);
				break;
			case Left:
			   	resetMazerPosition(Px,Py);
			    setMazerPosition(Px,Py-1);
				Px = getPx();
				Py = getPy();
				if(map[Px-1-1][Py-1] != wall)
				{
					if((map[Px-1][Py-1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
					else setDirection(Up);
				}
				else if(map[Px-1][Py-1-1] == wall) 
				{
					if(map[Px+1-1][Py-1] != wall)setDirection(Down);
					else setDirection(Right);
				}
				else setDirection(Left);
				break;
			case Right:
				resetMazerPosition(Px,Py);
				setMazerPosition(Px,Py+1);
				Px = getPx();
				Py = getPy();
				if(map[Px+1-1][Py-1] != wall)
				{
					if((map[Px-1][Py+1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
					else setDirection(Down);
				}
				else if(map[Px-1][Py+1-1] == wall)
				{
					if(map[Px-1-1][Py-1] != wall)setDirection(Up);
					else setDirection(Left);
				}
				else setDirection(Right);
				break;						
		}
		
		if(over)
		{
			moveto(12,1);
			cout << "Congratulation! use step " << step <<endl;
			break;
		}
		step ++;
	}	
}


0 回复 有任何疑惑可以回复我~
/***  .h文件  */
#ifndef _maze_h_
#define _maze_h_

#include <stdio.h>

 #define wall 1
 // #typedef 1 wall;
 #define road 0
// #typedef 0 road;
 
int map[10][11] = {  
					 {road,road,road,road,road,road,road,road,road,road,road},	
                     {road,wall,wall,wall,wall,wall,wall,wall,wall,wall,road},
                     {road,wall,wall,wall,wall,road,wall,wall,road,wall,road},
                     {road,wall,road,road,road,road,wall,wall,road,wall,road},
                     {road,wall,wall,wall,wall,road,wall,wall,road,wall,road},
                     {road,wall,wall,road,road,road,road,road,road,wall,road},
                     {road,wall,wall,road,wall,wall,road,road,road,wall,road},
                     {road,wall,road,road,wall,wall,wall,wall,wall,wall,road},
                     {road,wall,road,wall,wall,wall,wall,wall,wall,wall,road},
					 {road,road,road,road,road,road,road,road,road,road,road}
                 };

class maze
{
public:
	maze(int x,int y):m_X(x),m_Y(y){}
	void setMazeMap(int x,int y);
//	void setMazeWall(char c);
//	void drawMap();
private:
	int m_X; //
	int m_Y; //
};


enum direction{Up,Down,Left,Right};
class mazer
{
public:
	mazer():direct(Up){}
	void setMazerPosition(int x,int y);
	void resetMazerPosition(int x,int y);
	void MazerStart(void);
	direction getDirection(void);
	void setDirection(direction D);
	int getPx(void);
	int getPy(void);
private:	
	direction direct;
	int m_Px;
	int m_Py;
};


#define moveto(x,y) printf("\033[%d;%dH",(x),(y))
#define clear()  	printf("\033[2J")
#define setRed()	printf("\033[31m")
#define setBlack()  printf("\033[30m")
//#define setBlack() 	printf("\033[30m\033[4m")
#endif

/*   .cpp文件 */

#include <iostream>

#include <string>

#include <unistd.h>

#include <time.h>

#include <sys/time.h>

#include "maze.h" 

using namespace std;

int main()

{

clear();

moveto(1,1);

////////////////////////////////////////////

maze myMaze(10,11);

myMaze.setMazeMap(10,11);


mazer myMazer;

myMazer.setMazerPosition(10,3);

myMazer.MazerStart();


////////////////////////////////////////////

moveto(15,1);

setBlack();

return 0;

}


void maze::setMazeMap(int x,int y)

{

     int i=0,j=0;

     for(i=0;i<x;i++)

     {

         for(j=0;j<y;j++)

         {

          if(map[i][j]==wall)

          cout << "*";

else

cout << " ";

         }

         cout <<endl;

     }

}


void mazer::setMazerPosition(int x,int y)

{

m_Px = x;

m_Py = y;


moveto(x,y);

setRed();

cout << "P" << endl ;

// sleep(1); // n second

usleep(500000); //n usecond

}

void mazer::resetMazerPosition(int x,int y)

{

moveto(x,y);

setBlack();

cout << " " << endl;

}


int mazer::getPx()

{

return m_Px;

}

int mazer::getPy()

{

return m_Py;

}


direction mazer::getDirection()

{

return direct;

}

void mazer::setDirection(direction D)

{

direct = D;

}


void mazer::MazerStart()

{

int Px,Py;

int over=0,step=0;

Px = getPx();

Py = getPy();

while(1)

{

direction D = getDirection();

switch (D)

{

case Up:

resetMazerPosition(Px,Py);

setMazerPosition(Px-1,Py);

Px = getPx();

Py = getPy();

if(map[Px-1][Py+1-1] != wall)

{

if((map[Px-1-1][Py-1] !=wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;

else setDirection(Right);

}

else if(map[Px-1-1][Py-1] == wall) 

{

if(map[Px-1][Py-1-1] != wall)setDirection(Left);

else setDirection(Down);

}

else setDirection(Up);

break;

case Down:

   resetMazerPosition(Px,Py);

   setMazerPosition(Px+1,Py);

Px = getPx();

Py = getPy();

if(map[Px-1][Py-1-1] != wall) 

{

if((map[Px+1-1][Py-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;

else setDirection(Left);

}

else if(map[Px+1-1][Py-1] == wall)

{

if(map[Px-1][Py+1-1] != wall)setDirection(Right);

else setDirection(Up);

}

else setDirection(Down);

break;

case Left:

  resetMazerPosition(Px,Py);

   setMazerPosition(Px,Py-1);

Px = getPx();

Py = getPy();

if(map[Px-1-1][Py-1] != wall)

{

if((map[Px-1][Py-1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;

else setDirection(Up);

}

else if(map[Px-1][Py-1-1] == wall) 

{

if(map[Px+1-1][Py-1] != wall)setDirection(Down);

else setDirection(Right);

}

else setDirection(Left);

break;

case Right:

resetMazerPosition(Px,Py);

setMazerPosition(Px,Py+1);

Px = getPx();

Py = getPy();

if(map[Px+1-1][Py-1] != wall)

{

if((map[Px-1][Py+1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;

else setDirection(Down);

}

else if(map[Px-1][Py+1-1] == wall)

{

if(map[Px-1-1][Py-1] != wall)setDirection(Up);

else setDirection(Left);

}

else setDirection(Right);

break;

}

if(over)

{

moveto(12,1);

cout << "Congratulation! use step " << step <<endl;

break;

}

step ++;

}

}


0 回复 有任何疑惑可以回复我~

那是在windows下面的。视频里讲的都是在windows平台的,在linux下已经实现了。

0 回复 有任何疑惑可以回复我~
#1

迟早是大神

我也是在Linux环境下开发的,想问一下你是如何控制光标的,视频里没讲linux下的操作,能把你的源代码共享一下吗
2016-03-29 回复 有任何疑惑可以回复我~

#include <windows.h>

0 回复 有任何疑惑可以回复我~
#1

爱飞的小鸟 提问者

那是在windows下面的,视频里讲的都是在windows平台的。后面自己在linux下已经实现了,效果还行。
2015-08-25 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之封装篇(下)
  • 参与学习       70919    人
  • 解答问题       514    个

封装--面向对象三大特征之一,通过案例让C++所学知识融会贯通

进入课程

动画控制函数(设置光标位置)需要包含什么头文件?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信