最近整理以前写的一些程序,大多数价值不大,都是些学习时练手的简陋程序,很多还是半成品,不过自己看着倍感亲切啊,呵呵!其中有一个贪吃蛇的小游戏,用C++写的,不过没用MFC,呵呵,有点非主流啊!不过当时刚开始学windows程序设计,使用SDK编程,貌似还不知道MFC和KFC有啥不同吧...
其实这个设计的很糟糕,不过是我第一次进行相对复杂和完整的面向对象设计吧,还是有纪念意义的。
//*******************************************************************************
/*结点类,基本蛇身结点和食物的模型*/
class Node
{
private:
int m_left,m_top; //结点左上坐标
COLORREF col_fill,col_line; //绘制结点的颜色
public:
void SetLeft(int x)
{
m_left=x;
}
void SetTop(int y)
{
m_top=y;
}
void SetFillColor(COLORREF cl_f)
{
col_fill=cl_f;
}
void SetLineColor(COLORREF cl_l)
{
col_line=cl_l;
}
int GetLeft(){return m_left;}
int GetTop(){return m_top;}
COLORREF GetFillColor(){return col_fill;}
COLORREF GetLineColor(){return col_line;}
};
//****************************************************************
/*蛇类*/
class Snake
{
private:
Node m_hnode,m_node; //头结点对象,蛇身结点对象
Node *m_snakenode[MAXLENGTH]; //蛇身结点数组
int len; //当前蛇身长度
int m_direction; //当前运动方向
public:
void SetHeadNode(int x,int y,COLORREF fillcolor,COLORREF linecolor) //设置头结点属性
{
m_hnode.SetLeft(x);
m_hnode.SetTop(y);
m_hnode.SetFillColor(fillcolor);
m_hnode.SetLineColor(linecolor);
}
void SetNode(COLORREF fillcolor,COLORREF linecolor) //设置蛇身结点属性
{
m_node.SetFillColor(fillcolor);
m_node.SetLineColor(linecolor);
}
void SetDir(int dir) //设置当前运动方向
{
m_direction=dir;
}
void SetLen(int length) //设置蛇身长度
{
=length;
}
Node GetNode(){return m_node;}
Node *GetHeadNode(){return &m_hnode;}
Node* GetSnakeNode(int i){return m_snakenode[i];}
void AddNode() //吃到食物后添加结点
{
Node* pNode=new Node;
m_snakenode[len]=pNode;
len++;
}
int GetLen(){return len;};
int GetDirection(){return m_direction;}
};
//**************************************************************************
本文介绍了一个使用C++编写的贪吃蛇游戏的基本设计思路。游戏中通过面向对象的方法定义了结点类来表示蛇的身体和食物,并设计了蛇类来管理蛇的动作和状态。该设计对于学习C++面向对象编程具有一定的参考价值。
2467

被折叠的 条评论
为什么被折叠?



