C语言暑期实习 第六天

今天,开始研究那个小游戏,后来才知道,叫最初幻想,挺不错的名字,

附程序:

/*****************************************
* 模块名:ConsoleOut
* 功  能:用于控制台文本输出控制,可控制
*   文本颜色,光标位置
* 作  者:张松
* 日  期:2006.06.27
******************************************/
#include <windows.h>
#define BLACK_COLOR   0
#define BLUE_COLOR   1
#define GREEN_COLOR   2
#define RED_COLOR   3

/******************************************
/ 功    能:取得系统模块句柄
/ 输入参数:pdll 系统模块名
/ 输出参数:系统模块句柄
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.27  
******************************************/
HMODULE GetHmodule(char *pdll);


/******************************************
/ 功    能:切换控制台窗口为全屏或窗口模式
/ 输入参数:full -- true  表示全屏模式
/    false 表示窗口模式
/ 返回参数:无
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.27  
******************************************/
void SwitchConsoleWinMode(bool full);


/******************************************
/ 功    能:设置控制台文本显示颜色
/ 输入参数:nColorFront  --文本前景色
/   nColorBackGroup --文本背景色 
/ 返回参数:无
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.27  
******************************************/
void SetTextColor(int nColorFront, int nColorBackGroup);

/******************************************
/ 功    能:设置控制台光标位置
/ 输入参数:nX  -- 光标X轴坐标
/   nY  -- 光标Y走坐标
/ 返回参数:无
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.27  
******************************************/
void MoveCursorTo(int nX, int nY);

/******************************************
/ 功    能:关闭控制台光标
/ 输入参数:无
/   
/ 返回参数:无
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.29  
******************************************/
void CloseCursor();

/******************************************
/ 功    能:打开控制台光标
/ 输入参数:nSize -- 光标大小
/ 返回参数:无
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.29  
******************************************/
void OpenCursor();

 

/******************************************
/ 功    能:绘制矩形边框
/ 输入参数:title -- 边框标题
/   nX      -- 边框左上角X轴坐标
/           nY  -- 边框左上角Y轴坐标
/           nWidth  -- 边框宽度
/           nHight  -- 边框高度
/           frameColor  -- 边框前景色
/           bkColor     -- 边框背景色
/ 返回参数:无
/*作    者:李非
/*版    本:v1.0
/*修改日期:2005.09.30  
******************************************/
void CreateSmWindow(char title[],int nX, int nY, int nWidth, int nHight,
                    int frameColor, int bkColor);

int MenuCtrl(int nLeft,int nUp,char* cTxt[],int nSize,char* lpszTitle);
void sleep( int nWait );

 

 

 

// Element.cpp: implementation of the Element class.
//
//

#include "stdafx.h"
#include "Element.h"
#include "consoleout.h"
//
// Construction/Destruction
//

Element::Element()
{

}

Element::~Element()
{

}

Element::Element(CMap *map, Point point)
{
 host = map;
 Location = point;
}

char* Element::GetFace()
{
 return "";
}

void Element::Show()
{
 MoveCursorTo(Location.X*2,Location.Y);
 printf("%s",GetFace());
}

int Element::GetType()
{
 return -1;
}

 

 

 

// Element.h: interface for the Element class.
//
//

#if !defined(AFX_ELEMENT_H__571ACF0B_5A33_4345_8426_862366CF094C__INCLUDED_)
#define AFX_ELEMENT_H__571ACF0B_5A33_4345_8426_862366CF094C__INCLUDED_

#include "Point.h" // Added by ClassView
#include "Map.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Element 
{
public:
 virtual int GetType();
 void Show();
 virtual char* GetFace();
 Element(CMap*,Point);
 CMap *host;
 Point Location;
 Element();
 virtual ~Element();

};

#endif // !defined(AFX_ELEMENT_H__571ACF0B_5A33_4345_8426_862366CF094C__INCLUDED_)

 

 

 

// Hero.cpp: implementation of the CHero class.
//
//

#include "stdafx.h"
#include "Hero.h"
#include "consoleout.h"
//
// Construction/Destruction
//

CHero::CHero()
{

}

CHero::~CHero()
{

}

CHero::CHero(CMap *map, Point point):Element(map,point)
{
 
}

void CHero::GetMsg(char msg)
{
 switch(msg)
 {
 case 'a':
 case 's':
 case 'd':
 case 'w':
  this->Move(msg);
  break;
/* case 'y':
  this->Eat();
  break;*/
 default:
  break;
 }
}


void CHero::Move(char msg)
{
 Point nextPoint = Location.GetNextPoint(msg);

 switch(host->pElements[nextPoint.Y][nextPoint.X]->GetType())
 {
 case 0:
 case 8:
  host->pElements[Location.Y][Location.X]->Show();
  Location = nextPoint;
  this->Show();
  break;
 default:
  break;
 }
}

char* CHero::GetFace()
{
 return "";
}

int CHero::GetType()
{
 return 7;
}

 

 

 

// Hero.h: interface for the CHero class.
//
//

#if !defined(AFX_HERO_H__DF2487A4_6736_49CE_923C_53EC5D53177D__INCLUDED_)
#define AFX_HERO_H__DF2487A4_6736_49CE_923C_53EC5D53177D__INCLUDED_

#include "Point.h" // Added by ClassView
#include "Element.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CHero:public Element
{
public:
 virtual int GetType();
 virtual char* GetFace();
 void GetMsg(char);
 CHero(CMap*,Point);
 CHero();
 virtual ~CHero();

private:
 void Move(char);
};

#endif // !defined(AFX_HERO_H__DF2487A4_6736_49CE_923C_53EC5D53177D__INCLUDED_)

 

 

// Map.cpp: implementation of the CMap class.
//
//

#include "stdafx.h"
#include "Map.h"
#include "point.h"
#include "hero.h"
#include "element.h"
#include "stone.h"
#include "road.h"
#include "river.h"
//
// Construction/Destruction
//

CMap::CMap()
{

}
CMap::~CMap()
{
 delete []mapData;
}

void CMap::LoadMap(char *fileName)
{
 FILE *fp = fopen(fileName,"r");
 if(fp!=NULL)
 {
  fscanf(fp,"%d,%d,",&this->Rows,&this->Cols);
  mapData = new int[this->Rows*this->Cols];
  pElements = new Element**[this->Rows];
  for(int nRow=0;nRow<this->Rows;nRow++)
  {
   pElements[nRow] = new Element*[this->Cols];
   for(int nCol=0;nCol<this->Cols;nCol++)
   {   
    fscanf(fp,"%d,",mapData+nRow*this->Cols+nCol);
    Point location(nCol,nRow);
    pElements[nRow][nCol] = GenerateElementByType(mapData[nRow*this->Cols+nCol],location);
   }
  }
  fclose(fp);
 }
}
void CMap::Show()
{
 for(int nRow=0;nRow<this->Rows;nRow++)
 {
  for(int nCol=0;nCol<this->Cols;nCol++)
  {
   pElements[nRow][nCol]->Show();  
  }  
 }
 hero->Show();
}

void CMap::GetMsg(char msg)
{
 switch(msg)
 {
 case 'a':
 case 's':
 case 'd':
 case 'w':
  hero->GetMsg(msg);
  break;
 }
}

Element* CMap::GenerateElementByType(int type,Point location)
{
 Element *ele;
 switch(type)
 {
 case 1:
  ele = new Stone(this,location);
  break;
 case 7:  
  hero =  new CHero(this,location);
  ele = new Road(this,location);
  break;
 case 8:
  ele = new River(this,location);    
  break;
 default:
  ele = new Road(this,location);
  break;
 }
 return ele;
}

 

 

 

// Map.h: interface for the CMap class.
//
//

#if !defined(AFX_MAP_H__056F5E34_0A1C_479E_ADA2_9D9AF88EDB7E__INCLUDED_)
#define AFX_MAP_H__056F5E34_0A1C_479E_ADA2_9D9AF88EDB7E__INCLUDED_

//#include "Element.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CHero;
class Element;
class Point;
class CMap 
{
public:
 Element* GenerateElementByType(int type,Point point);
 Element ***pElements;
 CHero *hero;
 void GetMsg(char);
 int *mapData;
 int Cols;
 int Rows;
 void LoadMap(char*);
 void Show();
 CMap();
 virtual ~CMap();

};

#endif // !defined(AFX_MAP_H__056F5E34_0A1C_479E_ADA2_9D9AF88EDB7E__INCLUDED_)

 

 

 

// Point.cpp: implementation of the Point class.
//
//

#include "stdafx.h"
#include "Point.h"

//
// Construction/Destruction
//

Point::Point()
{

}

Point::~Point()
{

}

Point::Point(int x,int y)
{
 this->X = x;
 this->Y = y;

}

Point Point::GetNextPoint(char msg)
{
 Point nextPoint(this->X,this->Y);
 switch(msg)
 {
 case 'a':
  nextPoint.X--;
  break;
 case 'd':
  nextPoint.X++;
  break;
 case 's':
  nextPoint.Y++;
  break;
 case 'w':
  nextPoint.Y--;
  break;
 }
 return nextPoint;
}

 

 

// Point.h: interface for the Point class.
//
//

#if !defined(AFX_POINT_H__76044070_9E51_4AF9_86AE_DC7288DF7123__INCLUDED_)
#define AFX_POINT_H__76044070_9E51_4AF9_86AE_DC7288DF7123__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Point 
{
public:
 Point GetNextPoint(char);
 Point(int,int);
 int Y;
 int X;
 Point();
 virtual ~Point();

};

#endif // !defined(AFX_POINT_H__76044070_9E51_4AF9_86AE_DC7288DF7123__INCLUDED_)

 

 

// River.cpp: implementation of the River class.
//
//

#include "stdafx.h"
#include "River.h"

//
// Construction/Destruction
//

River::River()
{

}

River::~River()
{

}

char* River::GetFace()
{
 return "≈";
}

River::River(CMap *map, Point point):Element(map,point)
{

}

int River::GetType()
{
 return 8;
}

 

 

// River.h: interface for the River class.
//
//

#if !defined(AFX_RIVER_H__B6DFCDBB_682B_4179_ABE6_DCD6B5DD9016__INCLUDED_)
#define AFX_RIVER_H__B6DFCDBB_682B_4179_ABE6_DCD6B5DD9016__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Element.h"

class River : public Element 
{
public:
 virtual int GetType();
 River(CMap*,Point);
 virtual char* GetFace();
 River();
 virtual ~River();

};

#endif // !defined(AFX_RIVER_H__B6DFCDBB_682B_4179_ABE6_DCD6B5DD9016__INCLUDED_)

 

 

// Road.cpp: implementation of the Road class.
//
//

#include "stdafx.h"
#include "Road.h"

//
// Construction/Destruction
//

Road::Road()
{

}

Road::~Road()
{

}

char* Road::GetFace()
{
 return "  ";
}

Road::Road(CMap *map, Point point):Element(map,point)
{

}

int Road::GetType()
{
 return 0;
}

 

 

// Road.h: interface for the Road class.
//
//

#if !defined(AFX_ROAD_H__697D0E77_1996_46EB_B7F8_5A25FFCAA609__INCLUDED_)
#define AFX_ROAD_H__697D0E77_1996_46EB_B7F8_5A25FFCAA609__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Element.h"

class Road : public Element 
{
public:
 virtual int GetType();
 Road(CMap*,Point);
 virtual char* GetFace();
 Road();
 virtual ~Road();

};

#endif // !defined(AFX_ROAD_H__697D0E77_1996_46EB_B7F8_5A25FFCAA609__INCLUDED_)

 

 

// stdafx.cpp : source file that includes just the standard includes
// Test10.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

 

 

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__406F7DEA_77AB_4743_AD6F_9C584D869C17__INCLUDED_)
#define AFX_STDAFX_H__406F7DEA_77AB_4743_AD6F_9C584D869C17__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers

#include <stdio.h>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__406F7DEA_77AB_4743_AD6F_9C584D869C17__INCLUDED_)

 

 

// Stone.cpp: implementation of the Stone class.
//
//

#include "stdafx.h"
#include "Stone.h"
//
// Construction/Destruction
//

Stone::Stone()
{

}

Stone::~Stone()
{

}

char* Stone::GetFace()
{
 return "▓";
}

Stone::Stone(CMap *map, Point point):Element(map,point)
{

}

int Stone::GetType()
{
 return 1;
}

 

 

// Stone.h: interface for the Stone class.
//
//

#if !defined(AFX_STONE_H__347D00EA_8309_4711_93F4_98F99F8CD1A2__INCLUDED_)
#define AFX_STONE_H__347D00EA_8309_4711_93F4_98F99F8CD1A2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "point.h"
#include "Element.h"
class Stone : public Element 
{
public:
 virtual int GetType();
 Stone(CMap*,Point);
 virtual char* GetFace();
 Stone();
 virtual ~Stone();

};

#endif // !defined(AFX_STONE_H__347D00EA_8309_4711_93F4_98F99F8CD1A2__INCLUDED_)

 

 

 

// Test10.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "conio.h"
#include "map.h"
#include "consoleout.h"
void main()
{
 CloseCursor();
 CMap *map = new CMap();
 map->LoadMap("d:\\map.txt");
 map->Show();

 while(true)
 {
  char msg = getch();
  if('Q'==msg)
  {
   break;
  }
  map->GetMsg(msg);
 }

 delete map;
 OpenCursor();
}

 

这些代码,正在测试。。。。。。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值