C语言暑期实习 第五天

来源:互联网 发布:淘宝优惠券怎么制作 编辑:程序博客网 时间:2024/06/02 07:30

今天的主要任务是要做一个小游戏,调出一个代码,实现运行时候,界面中的“人”字符可以走动而且不撞墙,

先附上代码:

Map.cpp

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

#include "stdafx.h"
#include "Map.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];
  for(int nI=0;nI<this->Rows*this->Cols;nI++)
  {
   fscanf(fp,"%d,",mapData+nI);
  }
  fclose(fp);
 }
}
void CMap::Show()
{
 for(int nRow=0;nRow<this->Rows;nRow++)
 {
  for(int nCol=0;nCol<this->Cols;nCol++)
  {
   switch(mapData[nRow*this->Cols+nCol])
   {
   case 1:
    printf("▓");
    break;
   default:
    printf("  ");
    break;
   }
  }
  printf("\n");
 }
}

 

-----------------------------------------------------

Map.h

// 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_

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

class CMap 
{
public:
 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_)

-----------------------------

stdafx.cpp

// 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

// 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_)

----------------------------------------

Test10.cpp

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

#include "stdafx.h"
#include "map.h"
void main(int argc, char* argv[])
{
 CMap *map = new CMap();
 map->LoadMap("d:\\map.txt");
 map->Show();
 //......
 delete map;
}