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
|
#include<stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<constream.h>
static int snake[300][2], food[30][2], screen[80][25], i, j, k;
static int headx, heady, foodx, foody, level, tailx, taily, tail;
char dir, key;
main()
{
//clrscr();
randomize();
_setcursortype(_NOCURSOR); //this wouldn't display the cursor.
//start:
crash: clrscr();
tail=0;
dir='r';
// initialize snakes first coordinates
{
headx=random(79)+1;
heady=random(23)+1;
snake[0][0]=headx;
snake[0][1]=heady;
}
// put food
for(i=0; i<30; i++)
do{
foodx=random(78)+1;
foody=random(23)+1;
food[i][0]=foodx;
food[i][1]=foody;
}while(foodx==0&&foody==0) ;
//display food
textcolor(14);
for(i=0; i<30; i++)
{
foodx=food[i][0];
foody=food[i][1];
gotoxy(foodx, foody);
if(food[i][0]!=0)
cout<<"$";
}
main_loop:
{
tailx=snake[tail][0];
taily=snake[tail][1];
headx=snake[0][0];
heady=snake[0][1];
}
if(tail!=0)
{
for(i=tail; i>0; i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
}
}
//erase the last part of tail
if(tailx!=0 && taily!=0)
{
gotoxy(tailx, taily);
cout<<" ";
}
//move snake
chdir:
switch(dir)
{
case 'u':
if(heady>0)
heady-=1;
snake[0][1]=heady;
//else; //snake dies
break;
case 'd':
if(heady<24)
heady+=1;
snake[0][1]=heady;
//else; //snake dies
break;
case 'l':
if(headx>1)
headx-=1;
snake[0][0]=headx;
//else; //snake dies
break;
case 'r':
if(headx<80)
headx+=1;
snake[0][0]=headx;
//else; // snake dies
break;
case 'p':
dir='p';
while(!kbhit());
break;
}// end of switch
// control if snake crashed
/*if(screen[headx][heady]==1)
{
gotoxy(25, 12);
cout<<"CRASHED !...... ";
while(getch()!=32);
goto crash;
}*/
// control if snake crashed to himself
for(i=1; i<tail; i++)
{
if(headx==snake[i][0] && heady==snake[i][1])
{
textcolor(5);
gotoxy(25, 25);
cout<<"CRASHED ! ....... ";
while(getch()!=32);
goto crash;
}
}
// control if snake ate food
// draw snake
//for(i=0; i<=tail; i++)
if(snake[0][0]!=0 && snake[0][1]!=0)
{
gotoxy(snake[0][0], snake[0][1]);
cout<<"#";
}
for(i=0; i<30; i++)
{
foodx=food[i][0];
foody=food[i][1];
if(headx==foodx && heady==foody && food[i][0]!=0)
{
food[i][0]=0;
tail+=1;
//goto chdir;
}
}
delay(100);
if(kbhit())
{
asd:key=getch();
switch(key)
{
case 72:
if(dir!='d')
dir='u';
break;
case 80:
if(dir!='u')
dir='d';
break;
case 75:
if(dir!='r')
dir='l';
break;
case 77:
if(dir!='l')
dir='r';
break;
// pause
case 'p':
dir='p';
break;
case 27:
exit(0);
break;
case 0: goto asd;
}
}
gotoxy(1,1);cout<<"Score"<<tail;
goto main_loop;
//getch();
}
|