Dec 5, 2011 at 3:02am UTC
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 Dec 5, 2011 at 5:18pm UTC
Dec 5, 2011 at 1:39pm UTC
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?
Dec 5, 2011 at 5:15pm UTC
there is no body when moving up and left. :(
Dec 5, 2011 at 6:41pm UTC
please help.
Last edited on Dec 6, 2011 at 1:35am UTC
Dec 6, 2011 at 3:30pm UTC
@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. :)
Dec 8, 2011 at 7:49pm UTC
@nethoinkz sorry I have gave you a wrong answers.I liked you that you want to learn by yourself with your logic.