Snake game problems

I am trying to write my own snake game(I am learning from http://www.cplusplus.com/articles/3U75fSEw/), but i encountered problems that I canĀ“t just solve. For example: When snake eats apple the piece that is suppose to appear right next to it appears almost on the other side of playing board. Playing board is broken, because sometimes it shows game over even when it should not and many other problems.
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <ctime>
#include <thread>
#include <future>
#include <ncurses.h>

struct piece
{
    char c;
    int x, y;
};

class Snake
{
public:
    piece* piecesofsnake;
    int length;
    Snake(int);
    ~Snake();
};
Snake::Snake(int length)
{
    this->length = length;
    piecesofsnake = new piece[this->length];
}
Snake::~Snake()
{
    delete[] piecesofsnake;
}

int genrandvalue(int valuemin, int valuemax)
{
    srand(time(0));
    for (int i = 0; i < 1000000; i++)
        rand();
    return rand() % (valuemax - valuemin + 1) + valuemin;
}

void drawfield(int fieldsizex, int fieldsizey, const Snake& snake, piece apple)
{
    system("clear");
    for (int i = 0; i < fieldsizex; i++)
        std::cout << "\u2014";
    std::cout << "\n\r";

    for (int i = 0; i < fieldsizey - 2; i++)
    {
        for (int j = 0; j < fieldsizex; j++)
        {
            bool snakepieceon = false;
            for (int id = 0; id < snake.length; id++)
            {
                if (i == snake.piecesofsnake[id].y && j == snake.piecesofsnake[id].x)
                {
                    if (snake.piecesofsnake[id].c == '0')
                    {
                        std::cout << snake.piecesofsnake[id].c;
                        snakepieceon = true;
                    }
                }
            }
            if (snakepieceon == false && j == 0 || j == fieldsizex - 1)
            {
                std::cout << "|";
            }
            else if (snakepieceon == false && i == apple.y && j == apple.x)
            {
                std::cout << apple.c;
            }
            else if (snakepieceon == false)
            {
                std::cout << ' ';
            }
        }
        std::cout << "\n\r";
    }

    for (int i = 0; i < fieldsizex; i++)
        std::cout << "\u2014";
    std::cout << std::endl;
}

void gameover(char& c)
{
    system("clear");
    std::cout << "Game over!" << std::endl;
    c = 'q';
}

void gamelogic(char& c, char previousmovement, int fieldsizex, int fieldsizey, Snake& snake, piece &apple)
{
    for (int i = 0; i < snake.length; i++) //I have no idea what I thought.
    {
        if (snake.piecesofsnake[0].y > 0 &&
            snake.piecesofsnake[0].y < fieldsizey-1 &&
            snake.piecesofsnake[0].x > 0 &&
            snake.piecesofsnake[0].x < fieldsizex-1)
        {
            switch (previousmovement)
            {
            case 'w':
            {
                snake.piecesofsnake[i].y--;
            }
            break;
            case 's':
            {
                snake.piecesofsnake[i].y++;
            }
            break;
            case 'a':
            {
                snake.piecesofsnake[i].x--;
            }
            break;
            case 'd':
            {
                snake.piecesofsnake[i].x++;
            }
            break;
            }
            switch (c)
            {
            case 'w':
            {
                if (previousmovement != 'w' && snake.piecesofsnake[1].y != snake.piecesofsnake[0].y + 1)
                {
                    snake.piecesofsnake[i].y--;
                }
            }
            break;

            case 's':
            {
                if (previousmovement != 's' && snake.piecesofsnake[1].y != snake.piecesofsnake[0].y - 1)
                {
                    snake.piecesofsnake[i].y++;
                }
            }
            break;

            case 'a':
            {
                if (previousmovement != 'a' && snake.piecesofsnake[1].x != snake.piecesofsnake[0].x + 1)
                {
                    snake.piecesofsnake[i].x--;
                }
            }
            break;

            case 'd':
            {
                if (previousmovement != 'w' && snake.piecesofsnake[1].x != snake.piecesofsnake[0].x - 1)
                {
                    snake.piecesofsnake[i].x++;
                }
            }
            break;
            }
        }
        else
        {
            gameover(c);
        }
        
    }

    if ((apple.x == snake.piecesofsnake[0].x) && (apple.y == snake.piecesofsnake[0].y))
    {
        int lastpiece;
        for (int i = snake.length; i >= 0; i--)
        {
            if (snake.piecesofsnake[i].c == '0')
            {
                lastpiece = i;
            }
        }
        snake.piecesofsnake[lastpiece + 1].c = '0';
        switch (previousmovement)
        {
        case 'w':
            snake.piecesofsnake[lastpiece + 1].y = snake.piecesofsnake[lastpiece].y + 1;
            break;
        case 'a':
            snake.piecesofsnake[lastpiece + 1].x = snake.piecesofsnake[lastpiece].x + 1;
            break;
        case 's':
            snake.piecesofsnake[lastpiece + 1].y = snake.piecesofsnake[lastpiece].y - 1;
            break;
        case 'd':
            snake.piecesofsnake[lastpiece + 1].x = snake.piecesofsnake[lastpiece].x - 1;
            break;
        }
        apple.x = genrandvalue(0, fieldsizex);
        apple.y = genrandvalue(0, fieldsizey);
    }
}

int main(int argc, char **argv)
{
    initscr(); //inicialization of ncurses
    noecho(); //getch form ncurses won't show letter on terminal
    cbreak(); //no buffer of letters fot getch()
    nodelay(stdscr, TRUE); //getch won't wait for user input

    int fieldsizex = 160 / 4;
    int fieldsizey = 90 / 4; //I know that characters haven't got same length.
    
    Snake snake((fieldsizex - 2) * (fieldsizey - 2));
    snake.piecesofsnake[0].c = '0';
    snake.piecesofsnake[0].x = fieldsizex / 2;
    snake.piecesofsnake[0].y = fieldsizey / 2;

    piece apple;
    apple.c = 'O';
    apple.x = genrandvalue(0, fieldsizex);
    apple.y = genrandvalue(0, fieldsizey);

    char c;
    char previousmovement;
    while ((c != 'q') && (c != 'Q'))
    {
        c = getch();
        gamelogic(c, previousmovement, fieldsizex, fieldsizey, snake, apple);
        drawfield(fieldsizex, fieldsizey, snake, apple);
        usleep(250000);
        if (c == 'w' || c == 'a' || c == 's' || c == 'd')
            previousmovement = c;
    }
    endwin();
    return 0;
}

P.S. I am sorry for my bad english.
Last edited on
I also don't know how to make movement of whole snake.
the classic snake game moves like this:
draw a new pixel (box, *, whatever) at the next location where the snake's head will move. Delete (clear, set black, whatever) the last pixel on the snake's tail.

something (time? eating something? I can't remember this one) makes the snake get bigger. When he grows, you draw the head update but don't delete the tail pixel, every time it grows. Eventually he becomes so big it is hard not to run into yourself which kills him.

If you start talking about the banana throwing apes, I am out of here.
Last edited on
Topic archived. No new replies allowed.