Snake game using graphics.h and 2D arrays

As I'm a starter , I've been trying to make snake game by using arrays and graphics.h library (Well yes it's old , but I must use them to practice for my c++ class) . I'm somehow confused about how to make the snake move properly .When I change the snake's direction , the shape doesn't clear properly .And one other question is how can I make 4 circles for snake's body as default and add a circle to it's back whenever the snake eats a food? I'd be thankful if you give me a solution to fix that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <graphics.h>
#include <time.h>

using namespace std;

int main()
{
    initwindow(700,700,"snake");
    int cx=400; int cy=200;
    int x1[701][701]={0};
    int rx,ry,sx;
    char ch;

    srand(time(NULL));

    rx=rand()%(500-100+1)+100;
    ry=rand()%(500-100+1)+100;
    x1[rx][ry]=1;

    setfillstyle(1,15);
    circle(rx,ry,2);
    floodfill(rx,ry,15);

    for ( ; ; )
    {
        delay(10);

        setcolor(0);
        setfillstyle(1,0);
        circle(cx-11,cy,10);
        floodfill(cx-11,cy,0);
        x1[cx-11][cy]=0;
        cx++;

        setcolor(15);
        setfillstyle(1,15);
        circle(cx-10,cy,10);
        floodfill(cx-10,cy,15);
        x1[cx-10][cy]=2;
        for (int i=1 ; i<=10 ;i++)
        {
            setcolor(15);
            setfillstyle(1,15);
            circle(cx-i,cy,10);
            floodfill(cx-i,cy,15);
            x1[cx-i][cy]=2;
        }

        if (kbhit())
        {
            for( ; ; )
            {
                if(kbhit())
                {
                    ch=getch();
                }

            if (ch=='s')
            {
                cy++;
                sx=1;

            }
            else if (ch=='d')
            {
                cx++;
                sx=2;

            }
            else if (ch=='w')
            {
                cy--;
                sx=3;

            }
            else if (ch=='a')
            {
                cx--;
                sx=4;

            }



            if ( sx==1 )
            {
                delay(10);
                setcolor(0);
                setfillstyle(1,0);
                circle(cx,cy-11,10);
                floodfill(cx,cy-11,0);
                x1[cx][cy-11]=0;
                cy++;

                setcolor(15);
                setfillstyle(1,15);
                circle(cx,cy-10,10);
                floodfill(cx,cy-10,15);
                x1[cx][cy-10]=2;
                for (int i=1 ; i<=10 ;i++)
                {
                    setcolor(15);
                    setfillstyle(1,15);
                    circle(cx,cy-i,10);
                    floodfill(cx,cy-i,15);
                    x1[cx][cy-i]=2;
                }
            }

            else if ( sx==2 )
            {
                delay(10);
                setcolor(0);
                setfillstyle(1,0);
                circle(cx-11,cy,10);
                floodfill(cx-11,cy,0);
                x1[cx-11][cy]=0;
                cx++;

                setcolor(15);
                setfillstyle(1,15);
                circle(cx-10,cy,10);
                floodfill(cx-10,cy,15);
                x1[cx-10][cy]=2;
                for (int i=1 ; i<=10 ;i++)
                {
                    setcolor(15);
                    setfillstyle(1,15);
                    circle(cx-i,cy,10);
                    floodfill(cx-i,cy,15);
                    x1[cx-i][cy]=2;
                }
            }

            else if ( sx==3 )
            {
                delay(10);
                setcolor(0);
                setfillstyle(1,0);
                circle(cx,cy+11,10);
                floodfill(cx,cy+11,0);
                x1[cx][cy+11]=0;
                cy--;

                setcolor(15);
                setfillstyle(1,15);
                circle(cx,cy+10,10);
                floodfill(cx,cy+10,15);
                x1[cx][cy+10]=2;
                for (int i=1 ; i<=10 ;i++)
                {
                    setcolor(15);
                    setfillstyle(1,15);
                    circle(cx,cy+i,10);
                    floodfill(cx,cy+i,15);
                    x1[cx][cy+i]=2;
                }
            }

            else if ( sx==4 )
            {
                delay(10);
                setcolor(0);
                setfillstyle(1,0);
                circle(cx+11,cy,10);
                floodfill(cx+11,cy,0);
                x1[cx+11][cy]=0;
                cx--;

                setcolor(15);
                setfillstyle(1,15);
                circle(cx+10,cy,10);
                floodfill(cx+10,cy,15);
                x1[cx+10][cy]=2;
                for (int i=1 ; i<=10 ;i++)
                {
                    setcolor(15);
                    setfillstyle(1,15);
                    circle(cx+i,cy,10);
                    floodfill(cx+i,cy,15);
                    x1[cx+i][cy]=2;
                }
            }



            }
        }
    }
    getch();
    return 0;
    }
Last edited on
In the snake games I have seen so far they either used an array or list to store the location of the snake elements where location is a struct with x and y coordinate. In this way you can access all the elements of the snake and add new ones.

When you move the snake you need to calculate the new location of each element, erase the all the elements and draw the elements on the new location.

Hope this gives you an idea.
Thanks so much for replying and trying to help Thomas .
Could you please explain a little bit more about what you said?
how many lines and columns does our array need?
Could you please give me an example of calculating the new position of each element for moving the snake?
Hi Molly,

To store the location of a circle you can use a struct like this.
1
2
3
4
typedef struct Location
{
  int X, Y; // center of the circle
}


A snake is basically a collection of circles - the body of the snake + some extra information.
The best would be to use a class but I am not sure if you have learned them already.
how many lines and columns does our array need?

You need only colums - as many as you need. I am not sure what the max length of the snake would be.
1
2
3
  #define MAX_ELEM 10 // or whatever maximum you want.
  int length; // actual number of elements
  Location Snake[MAX_ELEM];


If you start the game with the head of the snake at x=100 and y=100 you can move the snake to the left by subtracting the distance from each element in the array like this.

1
2
3
4
  for (int i=0; i < length; i++)
  {
     Snake[i].X -= width; // width = diameter of the circle
  }


Hope this helps you to get started.

Topic archived. No new replies allowed.