贪吃蛇单机版

文章作者:w88手机版登录网络 上传时间:2019-11-14

1 class Food
  2 {
  3 public:
  4     Food(){}
  5     Food(COORD _cd) : cd(_cd){}
  6
  7     COORD cd;
  8 };
  9
 10 class SnakeNode
 11 {
 12 public:
 13     SnakeNode() : Prev(0),Next(0)
 14     {
 15         cd.X = cd.Y = 0;
 16     }
 17
 18     SnakeNode(COORD _cd) : cd(_cd),Prev(0),Next(0){}
 19     SnakeNode(int x,int y) : Prev(0),Next(0)
 20     {
 21         cd.X = x;
 22         cd.Y = y;
 23     }
 24
 25     COORD cd;
 26     SnakeNode* Prev;
 27     SnakeNode* Next;
 28 };
 29
 30 class Snake
 31 {
 32 public:
 33     Snake()
 34     {
 35         Initialize();
 36     }
 37
 38     ~Snake()
 39     {
 40         Destroy();
 41     }
 42
 43     void Initialize()
 44     {
 45         Score = 0;
 46         Length = 3;
 47         Type = dtLeft;
 48         Speed = 1000;
 49         Score = 0;
 50         SnakeNode* pNode1 = new SnakeNode(GAME_WIDTH / 2,GAME_HEIGHT / 2);
 51         SnakeNode* pNode2 = new SnakeNode(pNode1->cd.X 1,pNode1->cd.Y);
 52         SnakeNode* pNode3 = new SnakeNode(pNode1->cd.X 2,pNode1->cd.Y);
 53         pNode1->Next = pNode2;
 54         pNode2->Next = pNode3;
 55         pNode3->Prev = pNode2;
 56         pNode2->Prev = pNode1;
 57         Head = pNode1;
 58         Tail = pNode3;
 59     }
 60
 61     void Destroy()
 62     {
 63         SnakeNode* pNode = Head->Next;
 64         while(pNode)
 65         {
 66             SnakeNode* Next = pNode->Next;
 67             delete pNode;
 68             pNode = Next;
 69         }
 70         if(Head)
 71         {
 72             delete Head;
 73             Head = Tail = 0;
 74         }
 75     }
 76
 77     int Insert(int x,int y)
 78     {
 79         SnakeNode* pNode = new SnakeNode(x,y);
 80         pNode->Next = Head;
 81         Head->Prev = pNode;
 82         Head = pNode;
 83         return Length;
 84     }
 85
 86     COORD Pop()
 87     {
 88         COORD cd = Tail->cd;
 89         Tail = Tail->Prev;
 90         delete Tail->Next;
 91         Tail->Next = 0;
 92         return cd;
 93     }
 94
 95     int Length;                // 长度,无用
 96     DIRECTIONTYPE Type;        // 方向
 97     SnakeNode* Head;        // 蛇头节点
 98     SnakeNode* Tail;        // 蛇尾节点
 99     int Speed;                // 速度
100     Food food;                // 食物地方
101     int Score;                // 分数
102 };然后大家须求四个函数表示蛇向蛇头方向移动了生机勃勃格
 1     static void SnakeMove(Snake& sn)
 2     {
 3         COORD cdNew;
 4         switch(sn.Type)
 5         {
 6         case dtTop:
 7             cdNew = sn.Head->cd;
 8             cdNew.Y--;
 9             break;
10         case dtRight:
11             cdNew = sn.Head->cd;
12             cdNew.X ;
13             break;
14         case dtBottom:
15             cdNew = sn.Head->cd;
16             cdNew.Y ;
17             break;
18         case dtLeft:
19             cdNew = sn.Head->cd;
20             cdNew.X--;
21             break;
22         }
23         if(cdNew.X <

class Food 2 { 3 public: 4 Food(){} 5 Food(COORD _cd) : cd(_cd){} 6 7 COORD cd; 8 }; 9 10 class SnakeNode 11 { 12 public: 13 SnakeNode() : Prev(0),Next(0) 14 { 15 cd.X = cd.Y = 0...

本文由w88手机版登录-www.w88zhan.com-优德w88app官方登录发布于w88手机版登录网络,转载请注明出处:贪吃蛇单机版

关键词: