| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 375 人关注过本帖
标题:如何让Hunter 和RABBIT不能重合在一起?
只看楼主 加入收藏
dog52730652
Rank: 2
等 级:论坛游民
帖 子:13
专家分:11
注 册:2010-4-12
结帖率:100%
收藏
 问题点数:0 回复次数:1 
如何让Hunter 和RABBIT不能重合在一起?
main()函数:

#include "Hunter.h"

#define MAP_X             39        //地图X坐标
#define MAP_Y             16        //地图Y坐标
#define RABBIT_NUM        3

 //游戏地图  
char g_Map[MAP_Y][MAP_X] = {        
                               "* * * * * * * * * * * * * * * * * * *",   
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "*                                   *",
                               "* * * * * * * * * * * * * * * * * * *"
                           };

typedef struct _RABBIT
{
    int rabX;
    int rabY;
} RABBIT, * pRABBIT;

//刷新
void Refurbish()
{
    system("cls");
}

void main()
{
    srand((unsigned int)time(NULL));

    CHunter hunter;

    RABBIT  rabbit[5];
    for(int i = 0; i < RABBIT_NUM; i++)
    {
        rabbit[i].rabX = (rand() % (MAP_X - 4) + 1);
        rabbit[i].rabY = (rand() % (MAP_Y - 2) + 1);
    }

    char inPut[255] = "";
    bool active = false;
    while(!active)
    {
        Refurbish();
        //绘制地图
        g_Map[hunter.PutY()][hunter.PutX()] = 2;
        for(int i = 0; i < RABBIT_NUM; i++)
        {
            g_Map[rabbit[i].rabY][rabbit[i].rabX] = 4;
        }

        for(int i = 0; i < MAP_Y; i++)
        {
            for(int j = 0; j < MAP_X; j++)
            {
                cout<<g_Map[i][j];
            }
            cout<<endl;
        }

        cout<<"请按提示进行操作!"<<endl;
        cout<<"W.向上走一步。  S.向下走一步。  A.向左走一步。  D.向右走一步。"<<endl;
        cin>>inPut;

        g_Map[hunter.PutY()][hunter.PutX()] = ' ';    //清除脚印

        if((inPut[0] == 'W' || inPut[0] == 'w') && hunter.PutY() != 1)
        {
            hunter.SetY(-1);
        }
        else if((inPut[0] == 'S'|| inPut[0] == 's') && hunter.PutY() != (MAP_Y - 2))
        {
            hunter.SetY(1);
        }
        else if((inPut[0] == 'A' || inPut[0] == 'a') && hunter.PutX() != 2)
        {
            hunter.SetX(-2);
        }
        else if((inPut[0] == 'D' || inPut[0] == 'd') && hunter.PutX() != (MAP_X - 4))
        {
            hunter.SetX(2);
        }
    }
}

Hunter头文件:

#pragma once

#include <iostream>
#include <time.h>
using namespace std;

class CHunter
{
private:
    int m_bullet;
    int m_quarryCount;
    int m_posX;
    int m_posY;

public:
    CHunter();
    ~CHunter();
    void ShowHunter();
    void Hunt();
    void SetX(int );
    void SetY(int );
    int PutX();
    int PutY();
};
Hunter的CPP文件:

#include "Hunter.h"
CHunter::CHunter()
{
     m_bullet      = 10;
     m_quarryCount = 0;
     m_posX        = 2;
     m_posY        = 1;
}

CHunter::~CHunter()
{

}

void CHunter::ShowHunter()
{
   
}

void CHunter::Hunt()
{
    if(m_bullet > 0)
    {
        if((rand() % 10) < 7)
        {
            cout<<"击中兔子!"<<endl;
            m_quarryCount++;
        }
        else
        {
            cout<<"没有击中!"<<endl;
        }
        m_bullet--;
    }
    else
    {
        cout<<"没有子弹,无法射击"<<endl;
    }
}

void CHunter::SetX(int i)
{
    m_posX += i;
}

void CHunter::SetY(int i)
{
    m_posY += i;
}

int CHunter::PutX()
{
    return m_posX;
}

int CHunter::PutY()
{
    return m_posY;
}
搜索更多相关主题的帖子: 重合 RABBIT Hunter 
2010-04-15 13:14
dog52730652
Rank: 2
等 级:论坛游民
帖 子:13
专家分:11
注 册:2010-4-12
收藏
得分:0 
就是运行后的话  他们各自的图标不能叠加在一起,有个碰撞!
2010-04-15 13:26
快速回复:如何让Hunter 和RABBIT不能重合在一起?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037880 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved