Snake Game movement and tail

I would like for the snake to move in the direction that it was clicked until different up,down,left , right key is push instead of it only going when the key is pressed also how would i make another character or its tail to move behind the snakes head ?


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


#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;

char map [20][31]=


{
    "##############################",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "#                            #",
    "##############################"

};

bool game_running=true;
int y,x;
int j=1;
int i=1;
int main()

{



    while(game_running==true)
{
    system("cls");cout<<" your score is 0"<<endl;

     for (int display=0; display<20; display++)
     {
         cout<<map[display]<<endl;
     }system("pause>null");
     if (GetAsyncKeyState(VK_DOWN))
     {
int j2=1;

         if (map[j2][i]==' ')
         {

map[j][i]= ' ';
j++;
map [j][i]='@';

         }


     }

     if(GetAsyncKeyState(VK_UP))
{

    int j2 = j-1;


    if(map[j2][i] == ' ')

    {

 map[j][i] =' ';
    j--;
     map[j][i]='@';

    }
    }

    if (GetAsyncKeyState(VK_RIGHT))

    {
        int i2=i+1;

        if (map [j][i2]==' ')


           {
              map[j][i]=' ';
              i++;
              map[j][i] ='@';

           }
        }

        if (GetAsyncKeyState(VK_LEFT))

{
int i2=i-1;

            if(map[j][i2]==' ')

                {

   map[j][i]=' ';
     i--;
   map[j][i]='@';

    }
}


if (GetAsyncKeyState(VK_RETURN)){
 return 0;

}

}
}

Last edited on
ugh...I wouldn't suggest console games like this. Though if you really want to you will have to keep track of the snack parts and their locations then you simply will move them in the direction you are moving constantly (probably once a second). I would suggest you create snake with SFML or SDL or even an engine such as unity(though you wouldn't use c++ there).
Topic archived. No new replies allowed.