c++ 实现小游戏 夺宝奇兵

#ifndef _GAME_H_
#define _GAME_H_
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <string>
#include <Windows.h>
#include <conio.h>
#define N 10
#define M 5

enum  e_result
{
	error = -1,
	ok
};
class Bug   //怪物类
{
public:
	Bug();
	void updateBugPosition();     //更新怪物的位置
protected:
	int position[N-1][M];     //怪物的位置
	int direction[N-1][M];	  //怪物移动的方向和步长
};

class Raider  //突击者类
{
public:
	Raider();
	int getInput();   //获取突击者移动方向  
protected:
	int x;			  // 突击者的x、y坐标
	int y;
	int option[2];    //Option[1]保存突击手移动的方向
};

class Map:public Bug, public Raider   //地图类
{
public:
	Map();
	void getLevel();				// 获取游戏难度等级						
	void print();					//打印执行一步后的地图
	void result();				    //判断每一步的结果
	void updateTimes();				//更新剩余步数
	int get_ans();					//返回result()的结果

private:
	char input[N];		  //保存用户输入的信息
	int level;			  //地图游戏难度等级
	int times;			  //地图允许运行的最大步数
	int ans;			  //判断能否进行下一步
};
#endif
#include "game.h"
#include <iostream>
#include <process.h>
using namespace std;

//Bug
Bug::Bug()
{							 
	srand((unsigned)time(NULL));				 //设置随机数种子
	for (int i = 0; i < N-1; i++)
	{
		for (int j = 0; j < M; j++)
		{
			position[i][j] = rand() % N;   
			if (rand() % 2 == 1)
			{
				direction[i][j] = j+1;           //初始状态设置为向右移动
			}
			else direction[i][j] = -(j+1);		 //初始状态设置为向左移动
		}
	}
}
/*更新怪物的位置*/
void Bug::updateBugPosition()      
{
	for (int i = 0; i < N - 1; i++)
	{
		for (int j = 0; j < M; j++)
		{
			if (position[i][j] + direction[i][j] < 0)         //超出左边边界
			{
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值