当前位置:编程学习 > C/C++ >>

贪吃蛇单机版

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 &

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,