Need some help in snake.

Need help in snake. I can't seem to make a body for my snake. :(

*unfinished*;
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
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <string>
using namespace std;


int main() {
    char again;
    int length=5, rHead=6, cHead=8;
    int rBody[length], cBody[length]; 
    char stage[21][44], direction;
    rBody[0]=rHead;
    cBody[0]=cHead;
    bool lose=false;
    do {
        int speed;
        do {
            cout << "Set snake delay speed(1-fastest, 100-slowest): ";
            cin >> speed;
        } while(speed>100 || speed<1);
        for (int i=0; i<21; i++) {
            for (int j=0; j<44; j++) {
                stage[i][j]=' ';
            }
        }
        // Set Stage
        while (!lose) {
            
            while (!kbhit()) {
                if (direction=='w')
                    rHead--;
                else if (direction=='a')
                    cHead--;
                else if (direction=='s')
                    rHead++;
                else if (direction=='d')
                    cHead++;
                Sleep(speed);
                system("cls");
                for (int i=0; i<21; i++) {
                    int j;
                    for (j=0; j<44; j++) {
                        bool x=false;
                        for (int z=0; z<length; z++) {
                            if (i==rBody[z] && j==cBody[z]) {
                                rBody[z+1]=rBody[z];
                                cBody[z+1]=cBody[z];
                                stage[rBody[z]][cBody[z]]='#';
                                cout << stage[rBody[z]][cBody[z]];
                                j++;
                                x=true;
                            }
                        }
                        if (x) {
                            j--;
                            continue;
                        }
                        if (i==0 || i==20) {
                            stage[i][j]='-';
                        }
                        else if (j==0 || j==43) {
                            stage[i][j]='|';
                        }
                        else if (i==rHead && j==cHead) {
                            stage[rHead][cHead]='@';
                            rBody[0]=rHead;
                            cBody[0]=cHead;
                        }
                        else {
                            stage[i][j]=' ';
                        }
                        cout << stage[i][j];
                    }
                    cout << endl;
                }
            }
            char move=getch();
            if (move=='w')
                direction='w';
            if (move=='a')
                direction='a';    
            if (move=='s')
                direction='s';  
            if (move=='d')
                direction='d';  
        }
    } while(4>2);
    return 0;
}


*updated code. no body when moving up and left.
Last edited on
help please.
Line 28.. What is kbhit? you haven't defined it anywhere...
Also, line 95.. infinite loop.. Why not give an exit option...
Sorry, but can't understand about the body problem... You got any compiler errors?
You need to add more information to your post. What is it you're having problems with, making the body?

Caprico, kbhit() is a function within conio.h. http://www.cprogramming.com/fod/kbhit.html

And nethoinkz, Caprico is correct, you need to add a break; to exit the loop. Seeing as it's within the settings( entering speed etc), You could add an option to type something to exit, which would change a bool:

1
2
3
4
5
6
7
8
9
10
11
    bool exit = false;

    do
    {

    //code

    if( /*exit typed in*/ )
        exit = true;

    }while( ! exit);
there is no body when moving up and left. :(
This wont even compile for me.

You have:
const int length=5, rHead=6, cHead=8;

And then:
1
2
if (direction=='w')
                    rHead--;


You're trying to change a constant value!


kbhit() is deprecated, you should use: _kbhit()

As for not having a body while going up or left, I don't quite understand your code :/ lol
please help.
Last edited on
@Lynx876, i don't have. constant in my code. please try again.
@Dinesh subedi, wanna learn how to do snake with my own logic. :| just needing some tips and opinions. :)
@nethoinkz sorry I have gave you a wrong answers.I liked you that you want to learn by yourself with your logic.
Topic archived. No new replies allowed.