想编写一个超级小游戏,高手给我一下思路啊!!

最近把C++看到对象的常引用,那些什么继承,多太,虚函数,还没有看到,游戏是这样的:回合制,有4个人物,人物有不同技能,我把游戏简化的说一下,就按2个角色,技能部分要怎么... 最近把C++看到对象的常引用,那些什么继承,多太,虚函数,还没有看到,游戏是这样的:回合制,有4个人物,人物有不同技能,我把游戏简化的说一下,就按2个角色,技能部分要怎么写?我的想法是把技能作为类的一个成员函数来写,但这样的话,成员函数(技能)必须对其他对象中的数据成员进行操作,如1号的雷电术攻击2号,使他的HP减少50,这个貌似我还没有学到呀。还有2个职业需要定义几个类呢?如果定义成1个类,他们技能(成员函数)部分是不同的啊,我给大家看下我写的程序,短短几行居然有90个错误,我无语了,,请高手帮我写个简单点的(按2个角色,每个角色1个技能来写) 展开
 我来答
花式码农
2007-10-22 · TA获得超过1.7万个赞
知道大有可为答主
回答量:4894
采纳率:0%
帮助的人:4728万
展开全部
恰好遇到一个差不多的,给你了。

#include "stdafx.h"
#include <stdio.h>

//)竡挡篶
struct chr
{
int nHp;
int fHp;
int lv;
int w;
int kind;
};

//�办跑计��
HINSTANCE hInst;
HBITMAP bg,sheep,girl,skill,slash,magic,recover,game;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
int pNum,f,txtNum;
bool attack,over;
chr player,monster;
char text[5][100];

//ㄧΑ��
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
void MsgInsert(char*);
void CheckDie(int hp,bool player);

//****�祘Α**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

//磅《�﹍てㄧΑ
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

//笴栏癹伴
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 40)
MyPaint(hdc);
}
}

return msg.wParam;
}

//****)竡の爹�跌怠摸�ㄧΑ*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;

return RegisterClassEx(&wcex);
}

//****�﹍ㄧΑ*************************************
// �﹍�产の┣���兜�篈
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
hInst = hInstance;

hWnd = CreateWindow("canvas", "酶瓜跌怠" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,10,10,640,480,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);

bmp = CreateCompatibleBitmap(hdc,640,480);
SelectObject(mdc,bmp);

bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
sheep = (HBITMAP)LoadImage(NULL,"sheep.bmp",IMAGE_BITMAP,133,220,LR_LOADFROMFILE);
girl = (HBITMAP)LoadImage(NULL,"girl.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE);
skill = (HBITMAP)LoadImage(NULL,"skill.bmp",IMAGE_BITMAP,74,60,LR_LOADFROMFILE);
slash = (HBITMAP)LoadImage(NULL,"slash.bmp",IMAGE_BITMAP,196,162,LR_LOADFROMFILE);
magic = (HBITMAP)LoadImage(NULL,"magic.bmp",IMAGE_BITMAP,200,100,LR_LOADFROMFILE);
recover = (HBITMAP)LoadImage(NULL,"recover.bmp",IMAGE_BITMAP,300,150,LR_LOADFROMFILE);
game = (HBITMAP)LoadImage(NULL,"over.bmp",IMAGE_BITMAP,289,74,LR_LOADFROMFILE);

player.nHp = player.fHp = 50; //砞)�产à〔ネ㏑�の��
player.lv = 2; //砞)�产à〔单�
player.w = 4; //砞)ю阑端甡�舦�

monster.nHp = monster.fHp = 100; //砞)┣�à〔ネ㏑�の��
monster.lv = 1; //砞)┣�à〔单�
monster.w = 1; //砞)ю阑端甡�舦�

txtNum = 0; //陪ボ瘪�计ヘ

MyPaint(hdc);

return TRUE;
}

//****�璹酶瓜ㄧΑ*********************************
// 1.礶�禟瓜籔癸驹瘪�陪ボ
// 2.┣�《��耞の�兜计�矪瞶籔璸衡
void MyPaint(HDC hdc)
{
char str[100];
int i,damage;

//禟�琏春瓜
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

//陪ボ癸驹瘪�
for(i=0;i<txtNum;i++)
TextOut(mdc,0,360+i*18,text[i],strlen(text[i]));

//禟�┣�瓜
if(monster.nHp>0)
{
SelectObject(bufdc,sheep);
BitBlt(mdc,70,180,133,110,bufdc,0,110,SRCAND);
BitBlt(mdc,70,180,133,110,bufdc,0,0,SRCPAINT);
sprintf(str,"%d / %d",monster.nHp,monster.fHp);
TextOut(mdc,100,320,str,strlen(str));
}

//禟��产瓜
if(player.nHp>0)
{
SelectObject(bufdc,girl);
BitBlt(mdc,500,200,60,74,bufdc,pNum*60,74,SRCAND);
BitBlt(mdc,500,200,60,74,bufdc,pNum*60,0,SRCPAINT);
sprintf(str,"%d / %d",player.nHp,player.fHp);
TextOut(mdc,510,320,str,strlen(str));
}

if(over) //禟�笴栏挡�瓜ボ
{
SelectObject(bufdc,game);
BitBlt(mdc,200,200,289,37,bufdc,0,37,SRCAND);
BitBlt(mdc,200,200,289,37,bufdc,0,0,SRCPAINT);
}
else if(!attack) //禟�ю阑��瓜ボ
{
SelectObject(bufdc,skill);
BitBlt(mdc,500,350,74,30,bufdc,0,30,SRCAND);
BitBlt(mdc,500,350,74,30,bufdc,0,0,SRCPAINT);
}
else
{
f++;

//材5~10�礶��陪ボ�产ю阑瓜ボ
if(f>=5 && f<=10)
{
SelectObject(bufdc,slash);
BitBlt(mdc,100,160,98,162,bufdc,98,0,SRCAND);
BitBlt(mdc,100,160,98,162,bufdc,0,0,SRCPAINT);

//材10�礶��璸衡┣��端甡祘��の��陪ボ瘪�
if(f == 10)
{
damage = rand()%10 + player.lv*player.w;
monster.nHp -= (int)damage;

sprintf(str,"�产ю阑...�产癸┣�硑Θ� %d �端甡�",damage);
MsgInsert(str);

CheckDie(monster.nHp,false);
}
}

srand(tPre);

//材15�礶���耞┣�秈《�兜笆�
if(f == 15)
{
if(monster.nHp > 20) //ネ㏑���20
{
if(rand()%10 != 1)
monster.kind = 0;
else
monster.kind = 1;
}
else //ネ㏑���20
{
switch(rand()%5)
{
case 0: //炊硄ю阑
monster.kind = 0;
break;
case 1: //琁�ю阑臸猭
monster.kind = 1;
break;
case 2: //ㄏ荷��ю阑
monster.kind = 2;
break;
case 3: //干〉
monster.kind = 3;
break;
case 4: //発禲
monster.kind = 4;
break;
}
}
}

//材26~30�礶��陪ボ┣�《�瓜ボ
if(f>=26 && f<=30)
{
switch(monster.kind)
{
case 0: //炊硄ю阑
SelectObject(bufdc,slash);
BitBlt(mdc,480,150,98,162,bufdc,98,0,SRCAND);
BitBlt(mdc,480,150,98,162,bufdc,0,0,SRCPAINT);

//材30�礶��璸衡�产�端甡祘��の��陪ボ瘪�
if(f == 30)
{
damage = rand()%10 + monster.lv*monster.w;
player.nHp -= (int)damage;

sprintf(str,"┣�ю阑...┣�癸�产硑Θ� %d �端甡�",damage);
MsgInsert(str);

CheckDie(player.nHp,true);
}
break;
case 1: //琁�ю阑臸猭
SelectObject(bufdc,magic);
BitBlt(mdc,480,190,100,100,bufdc,100,0,SRCAND);
BitBlt(mdc,480,190,100,100,bufdc,0,0,SRCPAINT);

//材30�礶��璸衡�产�端甡祘��の��陪ボ瘪�
if(f == 30)
{
damage = rand()%10 + 3*monster.w;
player.nHp -= (int)damage;

sprintf(str,"┣�臸猭ю阑...┣�癸�产硑Θ� %d �端甡�",damage);
MsgInsert(str);

CheckDie(player.nHp,true);
}
break;
case 2: //ㄏ荷��ю阑
SelectObject(bufdc,slash);
BitBlt(mdc,480,150,98,162,bufdc,98,0,SRCAND);
BitBlt(mdc,480,150,98,162,bufdc,0,0,SRCPAINT);

//材30�礶��璸衡�产�端甡祘��の��陪ボ瘪�
if(f == 30)
{
damage = rand()%10 + monster.lv*monster.w*5;
player.nHp -= (int)damage;

sprintf(str,"┣���ю阑...┣�癸�产硑Θ� %d �端甡�",damage);
MsgInsert(str);

CheckDie(player.nHp,true);
}
break;
case 3: //干〉
SelectObject(bufdc,recover);
BitBlt(mdc,60,160,150,150,bufdc,150,0,SRCAND);
BitBlt(mdc,60,160,150,150,bufdc,0,0,SRCPAINT);

//材30�礶��┣��确ネ㏑��の��陪ボ瘪�
if(f == 30)
{
monster.nHp += 30;

sprintf(str,"┣�臸猭干〉...�确� 30 ネ㏑��",damage);
MsgInsert(str);
}
break;
case 4:
//材30�礶���耞┣�琌�発禲Θ�
if(f == 30)
{
if(rand()%3 == 1) //発禲Θ�诀瞯�1/3
{
over = true;
monster.nHp = 0;

sprintf(str,"┣�発禲...発禲Θ��");
MsgInsert(str);
}
else
{
sprintf(str,"┣�発禲...発禲ア毖�");
MsgInsert(str);
}
}
break;
}
}

if(f == 30) //��挡�
{
attack = false;
f = 0;
}
}

BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

tPre = GetTickCount();

pNum++;
if(pNum == 8)
pNum = 0;
}

//****癸驹瘪�穝糤ㄧΑ*********************************
void MsgInsert(char* str)
{
if(txtNum < 5)
{
sprintf(text[txtNum],str);
txtNum++;
}
else
{
for(int i=0;i<txtNum;i++)
sprintf(text[i],text[i+1]);

sprintf(text[4],str);
}
}

//****�产┣�ネ㏑��耞ㄧΑ*************************
void CheckDie(int hp,bool player)
{
char str[100];

if(hp <= 0)
{
over = true;
if(player)
{
sprintf(str,"�砆┣�ゴ毖�...");
MsgInsert(str);
}
else
{
sprintf(str,"┣�蔫防...");
MsgInsert(str);
}
}
}

//****瘪�矪瞶ㄧΑ***********************************
// �耞�产琌��笷ю阑��
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int x,y;

switch (message)
{
case WM_KEYDOWN: //�龄��瘪�
if(wParam==VK_ESCAPE) //�� Esc 龄
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN: //��菲公オ龄瘪�
if(!attack)
{
x = LOWORD(lParam); //�眔菲公 X 畒夹
y = HIWORD(lParam); //�眔菲公 Y 畒夹

if(x >= 500 && x <= 574 && y >= 350 && y <= 380)
attack = true;
}
break;
case WM_DESTROY: //跌怠挡�瘪�
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
DeleteObject(sheep);
DeleteObject(girl);
DeleteObject(skill);
DeleteObject(slash);
DeleteObject(magic);
DeleteObject(recover);
DeleteObject(game);

ReleaseDC(hWnd,hdc);

PostQuitMessage(0);
break;
default: //ㄤウ瘪�
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
百度网友5f707f9
2007-10-22 · TA获得超过311个赞
知道小有建树答主
回答量:514
采纳率:0%
帮助的人:597万
展开全部
貌似 _鈊_烦_薏乱 - 江湖少侠 七级 给了一个比较完整的代码。我就给楼主一点简单的思路吧。

2个人物,每人1个技能。
首先定义一个person类作为2个人物的共同基类(内部成员有姓名,性别,职业,技能等等),然后分别定义2个人A,B继承person。再定义一个skill类,作为技能类。好了,以下我简单写一下代码:

class skill
{
public:
char skillname[32];
int damage;//伤害值
};

class person
{
public:
char name[32];
char job[32];
skill p;//技能
};

class A : person
{
public: A(){
strcpy(name,"李小龙");
strcpy(p.skillname,"回旋踢");
p.damage = 50;
}
};

class B : person
{
public: B(){
strcpy(name,"霍元甲");
strcpy(p.skillname,"太极拳");
p.damage = 70;
}
};

好了,差不多就这些了,祝你写出好程序:)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式