贪吃蛇游戏的简单设计

本文介绍了一个使用C++编写的贪吃蛇游戏的基本设计思路。游戏中通过面向对象的方法定义了结点类来表示蛇的身体和食物,并设计了蛇类来管理蛇的动作和状态。该设计对于学习C++面向对象编程具有一定的参考价值。

 最近整理以前写的一些程序,大多数价值不大,都是些学习时练手的简陋程序,很多还是半成品,不过自己看着倍感亲切啊,呵呵!其中有一个贪吃蛇的小游戏,用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;}
    

};

//**************************************************************************

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值