Snake standalone Edition

Source: Internet
Author: User

1 class Food
2 {
3 public:
4 Food (){}
5 Food (COORD _ cd): cd (_ cd ){}
6
7 COORD cd;
8 };
9
10 class snail kenode
11 {
12 public:
13 snail kenode (): Prev (0), Next (0)
14 {
15 cd. X = cd. Y = 0;
16}
17
18 snail kenode (COORD _ cd): cd (_ cd), Prev (0), Next (0 ){}
19 snkenode (int x, int y): Prev (0), Next (0)
20 {
21 cd. X = x;
22 cd. Y = y;
23}
24
25 COORD cd;
26 snkenode * Prev;
27 snkenode * 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 snail kenode * pNode1 = new snail kenode (GAME_WIDTH/2, GAME_HEIGHT/2 );
51 snail kenode * pNode2 = new snail kenode (pNode1-> cd. X + 1, pNode1-> cd. Y );
52 snail kenode * pNode3 = new snail kenode (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 snkenode * pNode = Head-> Next;
64 while (pNode)
65 {
66 snkenode * 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 snail kenode * pNode = new snail kenode (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; // Length, useless
96 DIRECTIONTYPE Type; // direction
97 snake kenode * Head; // snake header Node
98 snkenode * Tail; // snake Tail Node
99 int Speed; // Speed
100 Food food; // Food location
101 int Score; // Score
102}; then we need a function to indicate that the snake has moved one cell to the snake head.
1 static void snail kemove (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 <

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.